1 package org.apache.lucene.queryParser.core.processors;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
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;
31 * A {@link QueryNodeProcessorPipeline} class should be used to build a query
32 * node processor pipeline.
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.
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.
44 public class QueryNodeProcessorPipeline implements QueryNodeProcessor,
45 List<QueryNodeProcessor> {
47 private LinkedList<QueryNodeProcessor> processors = new LinkedList<QueryNodeProcessor>();
49 private QueryConfigHandler queryConfig;
52 * Constructs an empty query node processor pipeline.
54 public QueryNodeProcessorPipeline() {
59 * Constructs with a {@link QueryConfigHandler} object.
61 public QueryNodeProcessorPipeline(QueryConfigHandler queryConfigHandler) {
62 this.queryConfig = queryConfigHandler;
66 * For reference about this method check:
67 * {@link QueryNodeProcessor#getQueryConfigHandler()}.
69 * @return QueryConfigHandler the query configuration handler to be set.
71 * @see QueryNodeProcessor#setQueryConfigHandler(QueryConfigHandler)
72 * @see QueryConfigHandler
74 public QueryConfigHandler getQueryConfigHandler() {
75 return this.queryConfig;
79 * For reference about this method check:
80 * {@link QueryNodeProcessor#process(QueryNode)}.
82 * @param queryTree the query node tree to be processed
84 * @throws QueryNodeException if something goes wrong during the query node
89 public QueryNode process(QueryNode queryTree) throws QueryNodeException {
91 for (QueryNodeProcessor processor : this.processors) {
92 queryTree = processor.process(queryTree);
100 * Adds a processor to the pipeline, it's always added to the end of the
103 * @deprecated this class now conforms to {@link List} interface, so use
104 * {@link #add(QueryNodeProcessor)} instead
106 * @param processor the processor to be added
109 public void addProcessor(QueryNodeProcessor processor) {
110 this.processors.add(processor);
112 processor.setQueryConfigHandler(this.queryConfig);
117 * For reference about this method check:
118 * {@link QueryNodeProcessor#setQueryConfigHandler(QueryConfigHandler)}.
120 * @param queryConfigHandler the query configuration handler to be set.
122 * @see QueryNodeProcessor#getQueryConfigHandler()
123 * @see QueryConfigHandler
125 public void setQueryConfigHandler(QueryConfigHandler queryConfigHandler) {
126 this.queryConfig = queryConfigHandler;
128 for (QueryNodeProcessor processor : this.processors) {
129 processor.setQueryConfigHandler(this.queryConfig);
135 * @see List#add(Object)
137 public boolean add(QueryNodeProcessor processor) {
138 boolean added = this.processors.add(processor);
141 processor.setQueryConfigHandler(this.queryConfig);
149 * @see List#add(int, Object)
151 public void add(int index, QueryNodeProcessor processor) {
152 this.processors.add(index, processor);
153 processor.setQueryConfigHandler(this.queryConfig);
158 * @see List#addAll(Collection)
160 public boolean addAll(Collection<? extends QueryNodeProcessor> c) {
161 boolean anyAdded = this.processors.addAll(c);
163 for (QueryNodeProcessor processor : c) {
164 processor.setQueryConfigHandler(this.queryConfig);
172 * @see List#addAll(int, Collection)
174 public boolean addAll(int index, Collection<? extends QueryNodeProcessor> c) {
175 boolean anyAdded = this.processors.addAll(index, c);
177 for (QueryNodeProcessor processor : c) {
178 processor.setQueryConfigHandler(this.queryConfig);
188 public void clear() {
189 this.processors.clear();
193 * @see List#contains(Object)
195 public boolean contains(Object o) {
196 return this.processors.contains(o);
200 * @see List#containsAll(Collection)
202 public boolean containsAll(Collection<?> c) {
203 return this.processors.containsAll(c);
209 public QueryNodeProcessor get(int index) {
210 return this.processors.get(index);
214 * @see List#indexOf(Object)
216 public int indexOf(Object o) {
217 return this.processors.indexOf(o);
221 * @see List#isEmpty()
223 public boolean isEmpty() {
224 return this.processors.isEmpty();
228 * @see List#iterator()
230 public Iterator<QueryNodeProcessor> iterator() {
231 return this.processors.iterator();
235 * @see List#lastIndexOf(Object)
237 public int lastIndexOf(Object o) {
238 return this.processors.lastIndexOf(o);
242 * @see List#listIterator()
244 public ListIterator<QueryNodeProcessor> listIterator() {
245 return this.processors.listIterator();
249 * @see List#listIterator(int)
251 public ListIterator<QueryNodeProcessor> listIterator(int index) {
252 return this.processors.listIterator(index);
256 * @see List#remove(Object)
258 public boolean remove(Object o) {
259 return this.processors.remove(o);
263 * @see List#remove(int)
265 public QueryNodeProcessor remove(int index) {
266 return this.processors.remove(index);
270 * @see List#removeAll(Collection)
272 public boolean removeAll(Collection<?> c) {
273 return this.processors.removeAll(c);
277 * @see List#retainAll(Collection)
279 public boolean retainAll(Collection<?> c) {
280 return this.processors.retainAll(c);
284 * @see List#set(int, Object)
286 public QueryNodeProcessor set(int index, QueryNodeProcessor processor) {
287 QueryNodeProcessor oldProcessor = this.processors.set(index, processor);
289 if (oldProcessor != processor) {
290 processor.setQueryConfigHandler(this.queryConfig);
301 return this.processors.size();
305 * @see List#subList(int, int)
307 public List<QueryNodeProcessor> subList(int fromIndex, int toIndex) {
308 return this.processors.subList(fromIndex, toIndex);
312 * @see List#toArray(Object[])
314 public <T> T[] toArray(T[] array) {
315 return this.processors.toArray(array);
319 * @see List#toArray()
321 public Object[] toArray() {
322 return this.processors.toArray();