001: /*
002: * $Header$
003: * $Revision: 1749 $
004: * $Date: 2006-04-04 08:41:40 -0700 $
005: *
006: * ====================================================================
007: *
008: * Copyright 1999-2002 The Apache Software Foundation
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: *
022: */
023:
024: package org.apache.slide.search;
025:
026: import java.util.Comparator;
027: import java.util.Iterator;
028: import java.util.Set;
029: import java.util.Vector;
030:
031: /**
032: * Aggregates a set containing the result items of a query. This set is either
033: * a HashSet (if no ordering was requested) or a TreeSet (if orderby was
034: * specified) May also contain a status and a response description
035: * (For example: 507 partial result)
036: *
037: *
038: * @version $Revision: 1749 $
039: */
040: public class LuceneSearchQueryResult extends SearchQueryResult {
041:
042: private int limit;
043: private Vector container = new Vector();
044: private Comparator comparator;
045:
046: /**
047: * Method add
048: *
049: * @param subResultSet a SearchQueryResult
050: *
051: */
052: public void add(SearchQueryResult subResultSet) {
053: merge(subResultSet);
054: }
055:
056: /**
057: * Merge method
058: *
059: * Merges two SearchQueryResults, which are ordered
060: *
061: */
062: public void merge(SearchQueryResult subResult) {
063: Vector newContainer = new Vector();
064:
065: Iterator newIt = subResult.iterator();
066: Iterator conIt = container.iterator();
067:
068: Object newObj = null;
069: Object conObj = null;
070:
071: if (conIt.hasNext())
072: conObj = conIt.next();
073: else
074: conObj = null;
075:
076: if (newIt.hasNext())
077: newObj = newIt.next();
078: else
079: newObj = null;
080:
081: while (newObj != null && conObj != null
082: && (limit < 0 || newContainer.size() <= limit)) {
083: if (comparator.compare(newObj, conObj) <= 0) // newObj is less
084: {
085: newContainer.add(newObj);
086:
087: if (newIt.hasNext())
088: newObj = newIt.next();
089: else
090: newObj = null;
091: } else {
092: newContainer.add(conObj);
093:
094: if (conIt.hasNext())
095: conObj = conIt.next();
096: else
097: conObj = null;
098: }
099: }
100:
101: while (newObj != null
102: && (limit < 0 || newContainer.size() <= limit)) {
103: newContainer.add(newObj);
104:
105: if (newIt.hasNext())
106: newObj = newIt.next();
107: else
108: newObj = null;
109: }
110: while (conObj != null
111: && (limit < 0 || newContainer.size() <= limit)) {
112: newContainer.add(conObj);
113:
114: if (conIt.hasNext())
115: conObj = conIt.next();
116: else
117: conObj = null;
118: }
119:
120: //System.err.println("LuceneSearchQueryResult.merge: was: "+container.size()+" is: "+newContainer.size()+" limit: "+limit);
121:
122: container = newContainer;
123:
124: // make sure
125: if (limit >= 0 && container.size() > limit)
126: container.setSize(limit);
127: }
128:
129: /**
130: * Constructs an empty unorderred SearchQueryResult
131: *
132: */
133: public LuceneSearchQueryResult() {
134: this (null, null, -1);
135: }
136:
137: /**
138: * Constructs an unordered SearchQueryResult
139: *
140: * @param result the set containing the result items
141: */
142: public LuceneSearchQueryResult(Set result) {
143: this (result, null, -1);
144: }
145:
146: /**
147: * Constructs an empty orderred LuceneSearchQueryResult
148: *
149: */
150: public LuceneSearchQueryResult(Comparator comparator) {
151: this (null, comparator, -1);
152: }
153:
154: /**
155: * Constructs an empty orderred LuceneSearchQueryResult
156: *
157: */
158: public LuceneSearchQueryResult(Set result, Comparator comparator) {
159: this (result, comparator, -1);
160: }
161:
162: /**
163: * Constructs a LuceneSearchQueryResult
164: *
165: * @param result the set containing the result items already sorted
166: * @param comparator for ordering (for merging, only)
167: */
168: public LuceneSearchQueryResult(Set result, Comparator comparator,
169: int limit) {
170: super ();
171:
172: this .limit = limit;
173: this .comparator = comparator;
174:
175: if (result != null)
176: container.addAll(result);
177:
178: if (limit >= 0 && container.size() > limit)
179: container.setSize(limit);
180: }
181:
182: /**
183: * Method iterator
184: *
185: * @return iterator for iterating the result, RequestedResource
186: *
187: */
188: public Iterator iterator() {
189: return container.iterator();
190: }
191:
192: }
|