add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / position / PositionFilter.java
1 package org.apache.lucene.analysis.position;
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.TokenFilter;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
25
26 /** Set the positionIncrement of all tokens to the "positionIncrement",
27  * except the first return token which retains its original positionIncrement value.
28  * The default positionIncrement value is zero.
29  */
30 public final class PositionFilter extends TokenFilter {
31
32   /** Position increment to assign to all but the first token - default = 0 */
33   private int positionIncrement = 0;
34   
35   /** The first token must have non-zero positionIncrement **/
36   private boolean firstTokenPositioned = false;
37   
38   private PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
39
40   /**
41    * Constructs a PositionFilter that assigns a position increment of zero to
42    * all but the first token from the given input stream.
43    * 
44    * @param input the input stream
45    */
46   public PositionFilter(final TokenStream input) {
47     super(input);
48   }
49
50   /**
51    * Constructs a PositionFilter that assigns the given position increment to
52    * all but the first token from the given input stream.
53    * 
54    * @param input the input stream
55    * @param positionIncrement position increment to assign to all but the first
56    *  token from the input stream
57    */
58   public PositionFilter(final TokenStream input, final int positionIncrement) {
59     this(input);
60     this.positionIncrement = positionIncrement;
61   }
62
63   @Override
64   public final boolean incrementToken() throws IOException {
65     if (input.incrementToken()) {
66       if (firstTokenPositioned) {
67         posIncrAtt.setPositionIncrement(positionIncrement);
68       } else {
69         firstTokenPositioned = true;
70       }
71       return true;
72     } else {
73       return false;
74     }
75   }
76
77   @Override
78   public void reset() throws IOException {
79     super.reset();
80     firstTokenPositioned = false;
81   }
82 }