add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / analysis / FilteringTokenFilter.java
1 package org.apache.lucene.analysis;
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.io.IOException;
21
22 import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
23 import org.apache.lucene.queryParser.QueryParser; // for javadoc
24
25 /**
26  * Abstract base class for TokenFilters that may remove tokens.
27  * You have to implement {@link #accept} and return a boolean if the current
28  * token should be preserved. {@link #incrementToken} uses this method
29  * to decide if a token should be passed to the caller.
30  */
31 public abstract class FilteringTokenFilter extends TokenFilter {
32
33   private final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
34   private boolean enablePositionIncrements; // no init needed, as ctor enforces setting value!
35
36   public FilteringTokenFilter(boolean enablePositionIncrements, TokenStream input){
37     super(input);
38     this.enablePositionIncrements = enablePositionIncrements;
39   }
40
41   /** Override this method and return if the current input token should be returned by {@link #incrementToken}. */
42   protected abstract boolean accept() throws IOException;
43
44   @Override
45   public final boolean incrementToken() throws IOException {
46     if (enablePositionIncrements) {
47       int skippedPositions = 0;
48       while (input.incrementToken()) {
49         if (accept()) {
50           if (skippedPositions != 0) {
51             posIncrAtt.setPositionIncrement(posIncrAtt.getPositionIncrement() + skippedPositions);
52           }
53           return true;
54         }
55         skippedPositions += posIncrAtt.getPositionIncrement();
56       }
57     } else {
58       while (input.incrementToken()) {
59         if (accept()) {
60           return true;
61         }
62       }
63     }
64     // reached EOS -- return false
65     return false;
66   }
67
68   /**
69    * @see #setEnablePositionIncrements(boolean)
70    */
71   public boolean getEnablePositionIncrements() {
72     return enablePositionIncrements;
73   }
74
75   /**
76    * If <code>true</code>, this TokenFilter will preserve
77    * positions of the incoming tokens (ie, accumulate and
78    * set position increments of the removed tokens).
79    * Generally, <code>true</code> is best as it does not
80    * lose information (positions of the original tokens)
81    * during indexing.
82    * 
83    * <p> When set, when a token is stopped
84    * (omitted), the position increment of the following
85    * token is incremented.
86    *
87    * <p> <b>NOTE</b>: be sure to also
88    * set {@link QueryParser#setEnablePositionIncrements} if
89    * you use QueryParser to create queries.
90    */
91   public void setEnablePositionIncrements(boolean enable) {
92     this.enablePositionIncrements = enable;
93   }
94 }