add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / analysis / CachingTokenFilter.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 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import org.apache.lucene.util.AttributeSource;
26
27 /**
28  * This class can be used if the token attributes of a TokenStream
29  * are intended to be consumed more than once. It caches
30  * all token attribute states locally in a List.
31  * 
32  * <P>CachingTokenFilter implements the optional method
33  * {@link TokenStream#reset()}, which repositions the
34  * stream to the first Token. 
35  */
36 public final class CachingTokenFilter extends TokenFilter {
37   private List<AttributeSource.State> cache = null;
38   private Iterator<AttributeSource.State> iterator = null; 
39   private AttributeSource.State finalState;
40   
41   public CachingTokenFilter(TokenStream input) {
42     super(input);
43   }
44   
45   @Override
46   public final boolean incrementToken() throws IOException {
47     if (cache == null) {
48       // fill cache lazily
49       cache = new LinkedList<AttributeSource.State>();
50       fillCache();
51       iterator = cache.iterator();
52     }
53     
54     if (!iterator.hasNext()) {
55       // the cache is exhausted, return false
56       return false;
57     }
58     // Since the TokenFilter can be reset, the tokens need to be preserved as immutable.
59     restoreState(iterator.next());
60     return true;
61   }
62   
63   @Override
64   public final void end() throws IOException {
65     if (finalState != null) {
66       restoreState(finalState);
67     }
68   }
69
70   @Override
71   public void reset() throws IOException {
72     if(cache != null) {
73       iterator = cache.iterator();
74     }
75   }
76   
77   private void fillCache() throws IOException {
78     while(input.incrementToken()) {
79       cache.add(captureState());
80     }
81     // capture final state
82     input.end();
83     finalState = captureState();
84   }
85
86 }