01: package org.apache.lucene.index;
02:
03: import java.util.*;
04:
05: /**
06: * Copyright 2007 The Apache Software Foundation
07: * <p/>
08: * Licensed under the Apache License, Version 2.0 (the "License");
09: * 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 fieldToTerms = new HashMap();
28: private SortedSet currentSet;
29: private String currentField;
30: private Comparator comparator;
31:
32: /**
33: *
34: * @param comparator A Comparator for sorting {@link TermVectorEntry}s
35: */
36: public FieldSortedTermVectorMapper(Comparator comparator) {
37: this (false, false, comparator);
38: }
39:
40: public FieldSortedTermVectorMapper(boolean ignoringPositions,
41: boolean ignoringOffsets, Comparator comparator) {
42: super (ignoringPositions, ignoringOffsets);
43: this .comparator = comparator;
44: }
45:
46: public void map(String term, int frequency,
47: TermVectorOffsetInfo[] offsets, int[] positions) {
48: TermVectorEntry entry = new TermVectorEntry(currentField, term,
49: frequency, offsets, positions);
50: currentSet.add(entry);
51: }
52:
53: public void setExpectations(String field, int numTerms,
54: boolean storeOffsets, boolean storePositions) {
55: currentSet = new TreeSet(comparator);
56: currentField = field;
57: fieldToTerms.put(field, currentSet);
58: }
59:
60: /**
61: * Get the mapping between fields and terms, sorted by the comparator
62: *
63: * @return A map between field names and {@link java.util.SortedSet}s per field. SortedSet entries are {@link TermVectorEntry}
64: */
65: public Map getFieldToTerms() {
66: return fieldToTerms;
67: }
68:
69: public Comparator getComparator() {
70: return comparator;
71: }
72: }
|