add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / document / MapFieldSelector.java
1 package org.apache.lucene.document;
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 java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 /**
26  * A {@link FieldSelector} based on a Map of field names to {@link FieldSelectorResult}s
27  *
28  */
29 public class MapFieldSelector implements FieldSelector {
30     
31     Map<String,FieldSelectorResult> fieldSelections;
32     
33     /** Create a a MapFieldSelector
34      * @param fieldSelections maps from field names (String) to {@link FieldSelectorResult}s
35      */
36     public MapFieldSelector(Map<String,FieldSelectorResult> 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<String> fields) {
44         fieldSelections = new HashMap<String,FieldSelectorResult>(fields.size()*5/3);
45         for (final String field : fields)
46             fieldSelections.put(field, FieldSelectorResult.LOAD);
47     }
48     
49     /** Create a a MapFieldSelector
50      * @param fields fields to LOAD.  All other fields are NO_LOAD.
51      */
52     public MapFieldSelector(String... fields) {
53       this(Arrays.asList(fields));
54     }
55
56
57     
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.
61      */
62     public FieldSelectorResult accept(String field) {
63         FieldSelectorResult selection = fieldSelections.get(field);
64         return selection!=null ? selection : FieldSelectorResult.NO_LOAD;
65     }
66     
67 }