01: package org.apache.lucene.document;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 java.util.HashMap;
21: import java.util.List;
22: import java.util.Map;
23:
24: /**
25: * A FieldSelector based on a Map of field names to FieldSelectorResults
26: *
27: * @author Chuck Williams
28: */
29: public class MapFieldSelector implements FieldSelector {
30:
31: Map fieldSelections;
32:
33: /** Create a a MapFieldSelector
34: * @param fieldSelections maps from field names (String) to FieldSelectorResults
35: */
36: public MapFieldSelector(Map fieldSelections) {
37: this .fieldSelections = fieldSelections;
38: }
39:
40: /** Create a a MapFieldSelector
41: * @param fields fields to LOAD. List of Strings. All other fields are NO_LOAD.
42: */
43: public MapFieldSelector(List fields) {
44: fieldSelections = new HashMap(fields.size() * 5 / 3);
45: for (int i = 0; i < fields.size(); i++)
46: fieldSelections
47: .put(fields.get(i), FieldSelectorResult.LOAD);
48: }
49:
50: /** Create a a MapFieldSelector
51: * @param fields fields to LOAD. All other fields are NO_LOAD.
52: */
53: public MapFieldSelector(String[] fields) {
54: fieldSelections = new HashMap(fields.length * 5 / 3);
55: for (int i = 0; i < fields.length; i++)
56: fieldSelections.put(fields[i], FieldSelectorResult.LOAD);
57: }
58:
59: /** Load field according to its associated value in fieldSelections
60: * @param field a field name
61: * @return the fieldSelections value that field maps to or NO_LOAD if none.
62: */
63: public FieldSelectorResult accept(String field) {
64: FieldSelectorResult selection = (FieldSelectorResult) fieldSelections
65: .get(field);
66: return selection != null ? selection
67: : FieldSelectorResult.NO_LOAD;
68: }
69:
70: }
|