Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CachingTokenFilter.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 namespace Lucene.Net.Analysis
19 {
20 
21  /// <summary> This class can be used if the token attributes of a TokenStream
22  /// are intended to be consumed more than once. It caches
23  /// all token attribute states locally in a List.
24  ///
25  /// <p/>CachingTokenFilter implements the optional method
26  /// <see cref="TokenStream.Reset()" />, which repositions the
27  /// stream to the first Token.
28  /// </summary>
29  public sealed class CachingTokenFilter : TokenFilter
30  {
31  private System.Collections.Generic.LinkedList<State> cache = null;
32  private System.Collections.Generic.IEnumerator<State> iterator = null;
33  private State finalState;
34 
35  public CachingTokenFilter(TokenStream input):base(input)
36  {
37  }
38 
39  public override bool IncrementToken()
40  {
41  if (cache == null)
42  {
43  // fill cache lazily
44  cache = new System.Collections.Generic.LinkedList<State>();
45  FillCache();
46  iterator = cache.GetEnumerator();
47  }
48 
49  if (!iterator.MoveNext())
50  {
51  // the cache is exhausted, return false
52  return false;
53  }
54  // Since the TokenFilter can be reset, the tokens need to be preserved as immutable.
55  RestoreState(iterator.Current);
56  return true;
57  }
58 
59  public override void End()
60  {
61  if (finalState != null)
62  {
63  RestoreState(finalState);
64  }
65  }
66 
67  public override void Reset()
68  {
69  if (cache != null)
70  {
71  iterator = cache.GetEnumerator();
72  }
73  }
74 
75  private void FillCache()
76  {
77  while (input.IncrementToken())
78  {
79  cache.AddLast(CaptureState());
80  }
81  // capture final state
82  input.End();
83  finalState = CaptureState();
84  }
85  }
86 }