pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / FieldSortedTermVectorMapper.java
1 package org.apache.lucene.index;
2
3 import java.util.*;
4
5 /**
6  * Copyright 2007 The Apache Software Foundation
7  * <p/>
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * <p/>
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * <p/>
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 /**
22  * For each Field, store a sorted collection of {@link TermVectorEntry}s
23  * <p/>
24  * This is not thread-safe.
25  */
26 public class FieldSortedTermVectorMapper extends TermVectorMapper{
27   private Map<String,SortedSet<TermVectorEntry>> fieldToTerms = new HashMap<String,SortedSet<TermVectorEntry>>();
28   private SortedSet<TermVectorEntry> currentSet;
29   private String currentField;
30   private Comparator<TermVectorEntry> comparator;
31
32   /**
33    *
34    * @param comparator A Comparator for sorting {@link TermVectorEntry}s
35    */
36   public FieldSortedTermVectorMapper(Comparator<TermVectorEntry> comparator) {
37     this(false, false, comparator);
38   }
39
40
41   public FieldSortedTermVectorMapper(boolean ignoringPositions, boolean ignoringOffsets, Comparator<TermVectorEntry> comparator) {
42     super(ignoringPositions, ignoringOffsets);
43     this.comparator = comparator;
44   }
45
46   @Override
47   public void map(String term, int frequency, TermVectorOffsetInfo[] offsets, int[] positions) {
48     TermVectorEntry entry = new TermVectorEntry(currentField, term, frequency, offsets, positions);
49     currentSet.add(entry);
50   }
51
52   @Override
53   public void setExpectations(String field, int numTerms, boolean storeOffsets, boolean storePositions) {
54     currentSet = new TreeSet<TermVectorEntry>(comparator);
55     currentField = field;
56     fieldToTerms.put(field, currentSet);
57   }
58
59   /**
60    * Get the mapping between fields and terms, sorted by the comparator
61    *
62    * @return A map between field names and {@link java.util.SortedSet}s per field.  SortedSet entries are {@link TermVectorEntry}
63    */
64   public Map<String,SortedSet<TermVectorEntry>> getFieldToTerms() {
65     return fieldToTerms;
66   }
67
68
69   public Comparator<TermVectorEntry> getComparator() {
70     return comparator;
71   }
72 }