add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / processors / QueryNodeProcessorPipeline.java
1 package org.apache.lucene.queryParser.core.processors;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.ListIterator;
25
26 import org.apache.lucene.queryParser.core.QueryNodeException;
27 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
28 import org.apache.lucene.queryParser.core.nodes.QueryNode;
29
30 /**
31  * A {@link QueryNodeProcessorPipeline} class should be used to build a query
32  * node processor pipeline.
33  * 
34  * When a query node tree is processed using this class, it passes the query
35  * node tree to each processor on the pipeline and the result from each
36  * processor is passed to the next one, always following the order the
37  * processors were on the pipeline.
38  * 
39  * When a {@link QueryConfigHandler} object is set on a
40  * {@link QueryNodeProcessorPipeline}, it also takes care of setting this
41  * {@link QueryConfigHandler} on all processor on pipeline.
42  * 
43  */
44 public class QueryNodeProcessorPipeline implements QueryNodeProcessor,
45     List<QueryNodeProcessor> {
46
47   private LinkedList<QueryNodeProcessor> processors = new LinkedList<QueryNodeProcessor>();
48
49   private QueryConfigHandler queryConfig;
50
51   /**
52    * Constructs an empty query node processor pipeline.
53    */
54   public QueryNodeProcessorPipeline() {
55     // empty constructor
56   }
57
58   /**
59    * Constructs with a {@link QueryConfigHandler} object.
60    */
61   public QueryNodeProcessorPipeline(QueryConfigHandler queryConfigHandler) {
62     this.queryConfig = queryConfigHandler;
63   }
64
65   /**
66    * For reference about this method check:
67    * {@link QueryNodeProcessor#getQueryConfigHandler()}.
68    * 
69    * @return QueryConfigHandler the query configuration handler to be set.
70    * 
71    * @see QueryNodeProcessor#setQueryConfigHandler(QueryConfigHandler)
72    * @see QueryConfigHandler
73    */
74   public QueryConfigHandler getQueryConfigHandler() {
75     return this.queryConfig;
76   }
77
78   /**
79    * For reference about this method check:
80    * {@link QueryNodeProcessor#process(QueryNode)}.
81    * 
82    * @param queryTree the query node tree to be processed
83    * 
84    * @throws QueryNodeException if something goes wrong during the query node
85    *         processing
86    * 
87    * @see QueryNode
88    */
89   public QueryNode process(QueryNode queryTree) throws QueryNodeException {
90
91     for (QueryNodeProcessor processor : this.processors) {
92       queryTree = processor.process(queryTree);
93     }
94
95     return queryTree;
96
97   }
98
99   /**
100    * Adds a processor to the pipeline, it's always added to the end of the
101    * pipeline.
102    * 
103    * @deprecated this class now conforms to {@link List} interface, so use
104    *             {@link #add(QueryNodeProcessor)} instead
105    * 
106    * @param processor the processor to be added
107    */
108   @Deprecated
109   public void addProcessor(QueryNodeProcessor processor) {
110     this.processors.add(processor);
111
112     processor.setQueryConfigHandler(this.queryConfig);
113
114   }
115
116   /**
117    * For reference about this method check:
118    * {@link QueryNodeProcessor#setQueryConfigHandler(QueryConfigHandler)}.
119    * 
120    * @param queryConfigHandler the query configuration handler to be set.
121    * 
122    * @see QueryNodeProcessor#getQueryConfigHandler()
123    * @see QueryConfigHandler
124    */
125   public void setQueryConfigHandler(QueryConfigHandler queryConfigHandler) {
126     this.queryConfig = queryConfigHandler;
127
128     for (QueryNodeProcessor processor : this.processors) {
129       processor.setQueryConfigHandler(this.queryConfig);
130     }
131
132   }
133
134   /**
135    * @see List#add(Object)
136    */
137   public boolean add(QueryNodeProcessor processor) {
138     boolean added = this.processors.add(processor);
139
140     if (added) {
141       processor.setQueryConfigHandler(this.queryConfig);
142     }
143
144     return added;
145
146   }
147
148   /**
149    * @see List#add(int, Object)
150    */
151   public void add(int index, QueryNodeProcessor processor) {
152     this.processors.add(index, processor);
153     processor.setQueryConfigHandler(this.queryConfig);
154
155   }
156
157   /**
158    * @see List#addAll(Collection)
159    */
160   public boolean addAll(Collection<? extends QueryNodeProcessor> c) {
161     boolean anyAdded = this.processors.addAll(c);
162
163     for (QueryNodeProcessor processor : c) {
164       processor.setQueryConfigHandler(this.queryConfig);
165     }
166
167     return anyAdded;
168
169   }
170
171   /**
172    * @see List#addAll(int, Collection)
173    */
174   public boolean addAll(int index, Collection<? extends QueryNodeProcessor> c) {
175     boolean anyAdded = this.processors.addAll(index, c);
176
177     for (QueryNodeProcessor processor : c) {
178       processor.setQueryConfigHandler(this.queryConfig);
179     }
180
181     return anyAdded;
182     
183   }
184
185   /**
186    * @see List#clear()
187    */
188   public void clear() {
189     this.processors.clear();
190   }
191
192   /**
193    * @see List#contains(Object)
194    */
195   public boolean contains(Object o) {
196     return this.processors.contains(o);
197   }
198
199   /**
200    * @see List#containsAll(Collection)
201    */
202   public boolean containsAll(Collection<?> c) {
203     return this.processors.containsAll(c);
204   }
205
206   /**
207    * @see List#get(int)
208    */
209   public QueryNodeProcessor get(int index) {
210     return this.processors.get(index);
211   }
212
213   /**
214    * @see List#indexOf(Object)
215    */
216   public int indexOf(Object o) {
217     return this.processors.indexOf(o);
218   }
219
220   /**
221    * @see List#isEmpty()
222    */
223   public boolean isEmpty() {
224     return this.processors.isEmpty();
225   }
226
227   /**
228    * @see List#iterator()
229    */
230   public Iterator<QueryNodeProcessor> iterator() {
231     return this.processors.iterator();
232   }
233
234   /**
235    * @see List#lastIndexOf(Object)
236    */
237   public int lastIndexOf(Object o) {
238     return this.processors.lastIndexOf(o);
239   }
240
241   /**
242    * @see List#listIterator()
243    */
244   public ListIterator<QueryNodeProcessor> listIterator() {
245     return this.processors.listIterator();
246   }
247
248   /**
249    * @see List#listIterator(int)
250    */
251   public ListIterator<QueryNodeProcessor> listIterator(int index) {
252     return this.processors.listIterator(index);
253   }
254
255   /**
256    * @see List#remove(Object)
257    */
258   public boolean remove(Object o) {
259     return this.processors.remove(o);
260   }
261
262   /**
263    * @see List#remove(int)
264    */
265   public QueryNodeProcessor remove(int index) {
266     return this.processors.remove(index);
267   }
268
269   /**
270    * @see List#removeAll(Collection)
271    */
272   public boolean removeAll(Collection<?> c) {
273     return this.processors.removeAll(c);
274   }
275
276   /**
277    * @see List#retainAll(Collection)
278    */
279   public boolean retainAll(Collection<?> c) {
280     return this.processors.retainAll(c);
281   }
282
283   /**
284    * @see List#set(int, Object)
285    */
286   public QueryNodeProcessor set(int index, QueryNodeProcessor processor) {
287     QueryNodeProcessor oldProcessor = this.processors.set(index, processor);
288     
289     if (oldProcessor != processor) {
290       processor.setQueryConfigHandler(this.queryConfig);
291     }
292     
293     return oldProcessor;
294     
295   }
296
297   /**
298    * @see List#size()
299    */
300   public int size() {
301     return this.processors.size();
302   }
303
304   /**
305    * @see List#subList(int, int)
306    */
307   public List<QueryNodeProcessor> subList(int fromIndex, int toIndex) {
308     return this.processors.subList(fromIndex, toIndex);
309   }
310
311   /**
312    * @see List#toArray(Object[])
313    */
314   public <T> T[] toArray(T[] array) {
315     return this.processors.toArray(array);
316   }
317
318   /**
319    * @see List#toArray()
320    */
321   public Object[] toArray() {
322     return this.processors.toArray();
323   }
324
325 }