Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
PerFieldAnalyzerWrapper.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.Collections.Generic;
19 using Lucene.Net.Support;
20 
21 namespace Lucene.Net.Analysis
22 {
23 
44  {
45  private readonly Analyzer defaultAnalyzer;
46  private readonly IDictionary<string, Analyzer> analyzerMap = new HashMap<string, Analyzer>();
47 
48 
55  public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer)
56  : this(defaultAnalyzer, null)
57  {
58  }
59 
70  public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, IEnumerable<KeyValuePair<string, Analyzer>> fieldAnalyzers)
71  {
72  this.defaultAnalyzer = defaultAnalyzer;
73  if (fieldAnalyzers != null)
74  {
75  foreach(var entry in fieldAnalyzers)
76  analyzerMap[entry.Key] = entry.Value;
77  }
78  SetOverridesTokenStreamMethod<PerFieldAnalyzerWrapper>();
79  }
80 
81 
89  public virtual void AddAnalyzer(System.String fieldName, Analyzer analyzer)
90  {
91  analyzerMap[fieldName] = analyzer;
92  }
93 
94  public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
95  {
96  var analyzer = analyzerMap[fieldName] ?? defaultAnalyzer;
97 
98  return analyzer.TokenStream(fieldName, reader);
99  }
100 
101  public override TokenStream ReusableTokenStream(string fieldName, System.IO.TextReader reader)
102  {
103  if (overridesTokenStreamMethod)
104  {
105  // LUCENE-1678: force fallback to tokenStream() if we
106  // have been subclassed and that subclass overrides
107  // tokenStream but not reusableTokenStream
108  return TokenStream(fieldName, reader);
109  }
110  var analyzer = analyzerMap[fieldName] ?? defaultAnalyzer;
111 
112  return analyzer.ReusableTokenStream(fieldName, reader);
113  }
114 
116  public override int GetPositionIncrementGap(string fieldName)
117  {
118  var analyzer = analyzerMap[fieldName] ?? defaultAnalyzer;
119  return analyzer.GetPositionIncrementGap(fieldName);
120  }
121 
123  public override int GetOffsetGap(Documents.IFieldable field)
124  {
125  Analyzer analyzer = analyzerMap[field.Name] ?? defaultAnalyzer;
126  return analyzer.GetOffsetGap(field);
127  }
128 
129  public override System.String ToString()
130  {
131  // {{Aroush-2.9}} will 'analyzerMap.ToString()' work in the same way as Java's java.util.HashMap.toString()?
132  return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")";
133  }
134  }
135 }