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
SingleTokenTokenStream.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 using System.Diagnostics;
19 using Lucene.Net.Analysis.Tokenattributes;
20 using Attribute = Lucene.Net.Util.Attribute;
21 
22 namespace Lucene.Net.Analysis.Miscellaneous
23 {
24  /// <summary>
25  /// A TokenStream containing a single token.
26  /// </summary>
27  public sealed class SingleTokenTokenStream : TokenStream
28  {
29  private bool _exhausted;
30 
31  // The token needs to be immutable, so work with clones!
32  private Token _singleToken;
33  private readonly Attribute _tokenAtt;
34 
36  : base(Token.TOKEN_ATTRIBUTE_FACTORY)
37  {
38  Debug.Assert(token != null, "Token was null!");
39  _singleToken = (Token) token.Clone();
40 
41  _tokenAtt = (Attribute)AddAttribute<ITermAttribute>();
42 
43  Debug.Assert(_tokenAtt is Token);
44  }
45 
46  public override sealed bool IncrementToken()
47  {
48  if (_exhausted)
49  return false;
50 
51  ClearAttributes();
52  _singleToken.CopyTo(_tokenAtt);
53  _exhausted = true;
54 
55  return true;
56  }
57 
58  public override void Reset()
59  {
60  _exhausted = false;
61  }
62 
63  protected override void Dispose(bool disposing)
64  {
65  // Do nothing
66  }
67 
68  public Token GetToken()
69  {
70  return (Token) _singleToken.Clone();
71  }
72 
73  public void SetToken(Token token)
74  {
75  _singleToken = (Token) token.Clone();
76  }
77  }
78 }