Package org.apache.slide.search.basic.expression

Source Code of org.apache.slide.search.basic.expression.MergeExpression

/*
* $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/expression/MergeExpression.java,v 1.2.2.1 2004/02/05 16:05:11 mholz Exp $
* $Revision: 1.2.2.1 $
* $Date: 2004/02/05 16:05:11 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.slide.search.basic.expression;

import java.util.Collection;
import java.util.Iterator;

import org.apache.slide.search.InvalidQueryException;
import org.apache.slide.search.SearchException;
import org.apache.slide.search.basic.BasicExpressionFactory;
import org.apache.slide.search.basic.IBasicExpression;
import org.apache.slide.search.basic.IBasicResultSet;
import org.jdom.Element;

/**
* Abstract base class for merge expressions (AND, OR).
*
* @author <a href="mailto:martin.wallmer@softwareag.com">Martin Wallmer</a>
* @version $Revision: 1.2.2.1 $
*/
public abstract class MergeExpression extends GenericBasicExpression {
   
   
    /** all nested expressions */
    protected Collection expressionsToMerge = null;
   
    protected BasicExpressionFactory expressionFactory;
   
    /**
     * Creates a merge expression according to Element e
     *
     * @param e                       jdom element, that describes the expression
     * @param expressionsToMerge      a Collection of IBasicExpressions to merge.
     */
    MergeExpression (Element e, Collection expressionsToMerge) throws InvalidQueryException {
        super (e);
        this.expressionsToMerge = expressionsToMerge;
        if (expressionsToMerge.size() == 0) {
            throw new InvalidQueryException(getMustHaveMergeExpressionsMessage(e.getName()));
        }
    }
   
    /**
     * Returns the expressions to merge.
     *
     * @return     the expressions to merge.
     */
    public Collection getExpressionsToMerge() {
        return expressionsToMerge;
    }
   
    /**
     * Executes the expression.
     *
     * @return   a Set of RequestedResource objects
     *
     * @throws   SearchException
     */
    public IBasicResultSet execute () throws SearchException {
       
        Iterator iterator = expressionsToMerge.iterator();
        if (iterator.hasNext()) {
            resultSet = ((IBasicExpression)iterator.next()).execute();
        }
        while (iterator.hasNext()) {
            IBasicExpression expression = (IBasicExpression)iterator.next();
            merge(expression.execute());
        }
        return resultSet;
    }
   
   
    /**
     * Merges the given <code>set</code> into the result Set of this expression.
     *
     * @param    set  the Set to merge.
     */
    protected abstract void merge (IBasicResultSet set);
   
   
    /**
     * String representation for debugging purposes.
     *
     * @return   this expression as String
     */
    protected String toString (String op) {
        StringBuffer sb = new StringBuffer();
       
        Iterator it = expressionsToMerge.iterator();
       
        while (it.hasNext()) {
            sb.append (((IBasicExpression) it.next()).toString());
            if (it.hasNext())
                sb.append (" ").append (op).append (" ");
        }
        return sb.toString();
    }
   
    /**
     * Returns the message of the InvalidQueryException that is thrown by the
     * constructor the <code>expressionsToMerge</code> set is empty.
     *
     * @param      operationName  the name of the operation (e.g. <code>and</code>)
     *
     * @return     the message of the InvalidQueryException.
     */
    public static String getMustHaveMergeExpressionsMessage(String operationName) {
        return "<" + operationName + "> must have at least on nested expression.";
    }
}

TOP

Related Classes of org.apache.slide.search.basic.expression.MergeExpression

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.