LuceneSearchQueryResult.java :  » Content-Management-System » hippo » org » apache » slide » search » Java Open Source

Java Open Source » Content Management System » hippo 
hippo » org » apache » slide » search » LuceneSearchQueryResult.java
/*
 * $Header$
 * $Revision: 1749 $
 * $Date: 2006-04-04 08:41:40 -0700 $
 *
 * ====================================================================
 *
 * 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;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

/**
 * Aggregates a set containing the result items of a query. This set is either
 * a HashSet (if no ordering was requested) or a TreeSet (if orderby was
 * specified) May also contain a status and a response description
 * (For example: 507 partial result)
 *
 *
 * @version $Revision: 1749 $
 */
public class LuceneSearchQueryResult extends SearchQueryResult {


  private int limit;
    private Vector container = new Vector();
    private Comparator comparator;

    /**
     * Method add
     *
     * @param    subResultSet        a  SearchQueryResult
     *
     */
    public void add (SearchQueryResult subResultSet) {
        merge(subResultSet);
    }
    
    /**
     * Merge method
     * 
     * Merges two SearchQueryResults, which are ordered
     *
     */
    public void merge(SearchQueryResult subResult)
    {
      Vector newContainer = new Vector();
      
      Iterator newIt = subResult.iterator();
      Iterator conIt = container.iterator();
      
      Object newObj = null;
      Object conObj = null;
      
      if(conIt.hasNext())
      conObj = conIt.next();
    else
      conObj = null;
      
      if(newIt.hasNext())
      newObj = newIt.next();
    else
      newObj = null;
      
      while(newObj!=null && conObj!=null && ( limit <0 || newContainer.size()<=limit ))
      {
        if(comparator.compare(newObj,conObj)<=0) // newObj is less
        {
          newContainer.add(newObj);
          
          if(newIt.hasNext())
            newObj = newIt.next();
          else
            newObj = null;
        }
        else
        {
          newContainer.add(conObj);
          
          if(conIt.hasNext())
            conObj = conIt.next();
          else
            conObj = null;
        }
      }
      
      while(newObj!=null && ( limit <0 || newContainer.size()<=limit ))
      {
        newContainer.add(newObj);
        
        if(newIt.hasNext())
        newObj = newIt.next();
      else
        newObj = null;
      }
      while(conObj!=null && ( limit <0 || newContainer.size()<=limit ))
      {
        newContainer.add(conObj);
        
        if(conIt.hasNext())
        conObj = conIt.next();
      else
        conObj = null;
      }
      
      //System.err.println("LuceneSearchQueryResult.merge: was: "+container.size()+" is: "+newContainer.size()+" limit: "+limit);
      
      container = newContainer;
      
      // make sure
      if(limit>=0 && container.size() > limit)
          container.setSize(limit);
    }

    /**
     * Constructs an empty unorderred SearchQueryResult
     *
     */
    public LuceneSearchQueryResult () {
        this (null, null, -1);
    }

    /**
     * Constructs an unordered SearchQueryResult
     *
     * @param result    the set containing the result items
     */
    public LuceneSearchQueryResult (Set result) {
        this (result, null, -1);
    }

    /**
     * Constructs an empty orderred LuceneSearchQueryResult
     *
     */
    public LuceneSearchQueryResult (Comparator comparator) {
        this (null, comparator, -1);
    }
    
    /**
     * Constructs an empty orderred LuceneSearchQueryResult
     *
     */
    public LuceneSearchQueryResult (Set result, Comparator comparator) {
        this (result, comparator, -1);
    }

    /**
     * Constructs a LuceneSearchQueryResult
     *
     * @param result    the set containing the result items already sorted
     * @param comparator for ordering (for merging, only)
     */
    public LuceneSearchQueryResult (Set result, Comparator comparator, int limit) {
        super();
        
        this.limit = limit;
        this.comparator = comparator;
        
        if(result!=null)
          container.addAll(result);
        
        if(limit>=0 && container.size() > limit)
          container.setSize(limit);
    }

    /**
     * Method iterator
     *
     * @return   iterator for iterating the result, RequestedResource
     *
     */
    public Iterator iterator () {
        return container.iterator();
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.