add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / analysis / PerFieldAnalyzerWrapper.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 org.apache.lucene.document.Fieldable;
21
22 import java.io.Reader;
23 import java.io.IOException;
24 import java.util.Map;
25 import java.util.HashMap;
26
27 /**
28  * This analyzer is used to facilitate scenarios where different
29  * fields require different analysis techniques.  Use {@link #addAnalyzer}
30  * to add a non-default analyzer on a field name basis.
31  * 
32  * <p>Example usage:
33  * 
34  * <pre>
35  *   PerFieldAnalyzerWrapper aWrapper =
36  *      new PerFieldAnalyzerWrapper(new StandardAnalyzer());
37  *   aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
38  *   aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
39  * </pre>
40  * 
41  * <p>In this example, StandardAnalyzer will be used for all fields except "firstname"
42  * and "lastname", for which KeywordAnalyzer will be used.
43  * 
44  * <p>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
45  * and query parsing.
46  */
47 public final class PerFieldAnalyzerWrapper extends Analyzer {
48   private Analyzer defaultAnalyzer;
49   private Map<String,Analyzer> analyzerMap = new HashMap<String,Analyzer>();
50
51
52   /**
53    * Constructs with default analyzer.
54    *
55    * @param defaultAnalyzer Any fields not specifically
56    * defined to use a different analyzer will use the one provided here.
57    */
58   public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer) {
59     this(defaultAnalyzer, null);
60   }
61   
62   /**
63    * Constructs with default analyzer and a map of analyzers to use for 
64    * specific fields.
65    *
66    * @param defaultAnalyzer Any fields not specifically
67    * defined to use a different analyzer will use the one provided here.
68    * @param fieldAnalyzers a Map (String field name to the Analyzer) to be 
69    * used for those fields 
70    */
71   public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, 
72       Map<String,Analyzer> fieldAnalyzers) {
73     this.defaultAnalyzer = defaultAnalyzer;
74     if (fieldAnalyzers != null) {
75       analyzerMap.putAll(fieldAnalyzers);
76     }
77   }
78   
79
80   /**
81    * Defines an analyzer to use for the specified field.
82    *
83    * @param fieldName field name requiring a non-default analyzer
84    * @param analyzer non-default analyzer to use for field
85    */
86   public void addAnalyzer(String fieldName, Analyzer analyzer) {
87     analyzerMap.put(fieldName, analyzer);
88   }
89
90   @Override
91   public TokenStream tokenStream(String fieldName, Reader reader) {
92     Analyzer analyzer = analyzerMap.get(fieldName);
93     if (analyzer == null) {
94       analyzer = defaultAnalyzer;
95     }
96
97     return analyzer.tokenStream(fieldName, reader);
98   }
99   
100   @Override
101   public TokenStream reusableTokenStream(String fieldName, Reader reader) throws IOException {
102     Analyzer analyzer = analyzerMap.get(fieldName);
103     if (analyzer == null)
104       analyzer = defaultAnalyzer;
105
106     return analyzer.reusableTokenStream(fieldName, reader);
107   }
108   
109   /** Return the positionIncrementGap from the analyzer assigned to fieldName */
110   @Override
111   public int getPositionIncrementGap(String fieldName) {
112     Analyzer analyzer = analyzerMap.get(fieldName);
113     if (analyzer == null)
114       analyzer = defaultAnalyzer;
115     return analyzer.getPositionIncrementGap(fieldName);
116   }
117
118   /** Return the offsetGap from the analyzer assigned to field */
119   @Override
120   public int getOffsetGap(Fieldable field) {
121     Analyzer analyzer = analyzerMap.get(field.name());
122     if (analyzer == null)
123       analyzer = defaultAnalyzer;
124     return analyzer.getOffsetGap(field);
125   }
126   
127   @Override
128   public String toString() {
129     return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")";
130   }
131 }