add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / miscellaneous / PrefixAndSuffixAwareTokenFilter.java
1 package org.apache.lucene.analysis.miscellaneous;
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 org.apache.lucene.analysis.Token;
21 import org.apache.lucene.analysis.TokenStream;
22
23 import java.io.IOException;
24
25 /**
26  * Links two {@link PrefixAwareTokenFilter}.
27  * <p/>
28  * <b>NOTE:</b> This filter might not behave correctly if used with custom Attributes, i.e. Attributes other than
29  * the ones located in org.apache.lucene.analysis.tokenattributes. 
30  */
31 public class PrefixAndSuffixAwareTokenFilter extends TokenStream {
32
33   private PrefixAwareTokenFilter suffix;
34
35   public PrefixAndSuffixAwareTokenFilter(TokenStream prefix, TokenStream input, TokenStream suffix) {
36     super(suffix);
37     prefix = new PrefixAwareTokenFilter(prefix, input) {
38       @Override
39       public Token updateSuffixToken(Token suffixToken, Token lastInputToken) {
40         return PrefixAndSuffixAwareTokenFilter.this.updateInputToken(suffixToken, lastInputToken);
41       }
42     };
43     this.suffix = new PrefixAwareTokenFilter(prefix, suffix) {
44       @Override
45       public Token updateSuffixToken(Token suffixToken, Token lastInputToken) {
46         return PrefixAndSuffixAwareTokenFilter.this.updateSuffixToken(suffixToken, lastInputToken);
47       }
48     };
49   }
50
51   public Token updateInputToken(Token inputToken, Token lastPrefixToken) {
52     inputToken.setStartOffset(lastPrefixToken.endOffset() + inputToken.startOffset());
53     inputToken.setEndOffset(lastPrefixToken.endOffset() + inputToken.endOffset());
54     return inputToken;
55   }
56
57   public Token updateSuffixToken(Token suffixToken, Token lastInputToken) {
58     suffixToken.setStartOffset(lastInputToken.endOffset() + suffixToken.startOffset());
59     suffixToken.setEndOffset(lastInputToken.endOffset() + suffixToken.endOffset());
60     return suffixToken;
61   }
62
63
64   @Override
65   public final boolean incrementToken() throws IOException {
66     return suffix.incrementToken();
67   }
68
69   @Override
70   public void reset() throws IOException {
71     suffix.reset();
72   }
73
74
75   @Override
76   public void close() throws IOException {
77     suffix.close();
78   }
79
80   @Override
81   public void end() throws IOException {
82     suffix.end();
83   }
84 }