1 package org.apache.lucene.analysis.position;
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.io.IOException;
22 import org.apache.lucene.analysis.TokenFilter;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
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.
30 public final class PositionFilter extends TokenFilter {
32 /** Position increment to assign to all but the first token - default = 0 */
33 private int positionIncrement = 0;
35 /** The first token must have non-zero positionIncrement **/
36 private boolean firstTokenPositioned = false;
38 private PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
41 * Constructs a PositionFilter that assigns a position increment of zero to
42 * all but the first token from the given input stream.
44 * @param input the input stream
46 public PositionFilter(final TokenStream input) {
51 * Constructs a PositionFilter that assigns the given position increment to
52 * all but the first token from the given input stream.
54 * @param input the input stream
55 * @param positionIncrement position increment to assign to all but the first
56 * token from the input stream
58 public PositionFilter(final TokenStream input, final int positionIncrement) {
60 this.positionIncrement = positionIncrement;
64 public final boolean incrementToken() throws IOException {
65 if (input.incrementToken()) {
66 if (firstTokenPositioned) {
67 posIncrAtt.setPositionIncrement(positionIncrement);
69 firstTokenPositioned = true;
78 public void reset() throws IOException {
80 firstTokenPositioned = false;