1 package org.apache.lucene.document;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
26 * A {@link FieldSelector} based on a Map of field names to {@link FieldSelectorResult}s
29 public class MapFieldSelector implements FieldSelector {
31 Map<String,FieldSelectorResult> fieldSelections;
33 /** Create a a MapFieldSelector
34 * @param fieldSelections maps from field names (String) to {@link FieldSelectorResult}s
36 public MapFieldSelector(Map<String,FieldSelectorResult> fieldSelections) {
37 this.fieldSelections = fieldSelections;
40 /** Create a a MapFieldSelector
41 * @param fields fields to LOAD. List of Strings. All other fields are NO_LOAD.
43 public MapFieldSelector(List<String> fields) {
44 fieldSelections = new HashMap<String,FieldSelectorResult>(fields.size()*5/3);
45 for (final String field : fields)
46 fieldSelections.put(field, FieldSelectorResult.LOAD);
49 /** Create a a MapFieldSelector
50 * @param fields fields to LOAD. All other fields are NO_LOAD.
52 public MapFieldSelector(String... fields) {
53 this(Arrays.asList(fields));
58 /** Load field according to its associated value in fieldSelections
59 * @param field a field name
60 * @return the fieldSelections value that field maps to or NO_LOAD if none.
62 public FieldSelectorResult accept(String field) {
63 FieldSelectorResult selection = fieldSelections.get(field);
64 return selection!=null ? selection : FieldSelectorResult.NO_LOAD;