add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / index / params / PerDimensionIndexingParams.java
1 package org.apache.lucene.facet.index.params;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.apache.lucene.facet.taxonomy.CategoryPath;
8
9 /**
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements.  See the NOTICE file distributed with
12  * this work for additional information regarding copyright ownership.
13  * The ASF licenses this file to You under the Apache License, Version 2.0
14  * (the "License"); you may not use this file except in compliance with
15  * the License.  You may obtain a copy of the License at
16  *
17  *     http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  */
25
26 /**
27  * A FacetIndexingParams that utilizes different category lists, defined by the
28  * dimension specified CategoryPaths (see
29  * {@link PerDimensionIndexingParams#addCategoryListParams(CategoryPath, CategoryListParams)}
30  * <p>
31  * A 'dimension' is defined as the first or "zero-th" component in a
32  * CategoryPath. For example, if a CategoryPath is defined as
33  * "/Author/American/Mark Twain", then the dimension is "Author".
34  * <p>
35  * This class also uses the 'default' CategoryListParams (as specified by
36  * {@link CategoryListParams#CategoryListParams()} when
37  * {@link #getCategoryListParams(CategoryPath)} is called for a CategoryPath
38  * whose dimension component has not been specifically defined.
39  * 
40  * @lucene.experimental
41  */
42 public class PerDimensionIndexingParams extends DefaultFacetIndexingParams {
43
44   // "Root" or "first component" of a Category Path maps to a
45   // CategoryListParams
46   private final Map<String, CategoryListParams> clParamsMap = new HashMap<String, CategoryListParams>();
47
48   /**
49    * Construct with the default {@link CategoryListParams} as the default
50    * CategoryListParams for unspecified CategoryPaths.
51    */
52   public PerDimensionIndexingParams() {
53     this(new CategoryListParams());
54   }
55
56   /**
57    * Construct with the included categoryListParams as the default
58    * CategoryListParams for unspecified CategoryPaths.
59    * 
60    * @param categoryListParams
61    *            the default categoryListParams to use
62    */
63   public PerDimensionIndexingParams(CategoryListParams categoryListParams) {
64     super(categoryListParams);
65   }
66
67   /**
68    * Get all the categoryListParams, including the default.
69    */
70   @Override
71   public Iterable<CategoryListParams> getAllCategoryListParams() {
72     ArrayList<CategoryListParams> vals = 
73       new ArrayList<CategoryListParams>(clParamsMap.values());
74     for (CategoryListParams clp : super.getAllCategoryListParams()) {
75       vals.add(clp);
76     }
77     return vals;
78   }
79
80   /**
81    * Get the CategoryListParams based on the dimension or "zero-th category"
82    * of the specified CategoryPath.
83    */
84   @Override
85   public CategoryListParams getCategoryListParams(CategoryPath category) {
86     if (category != null) {
87       CategoryListParams clParams = clParamsMap.get(category.getComponent(0));
88       if (clParams != null) {
89         return clParams;
90       }
91     }
92     return super.getCategoryListParams(category);
93   }
94
95   /**
96    * Add a CategoryListParams for a given CategoryPath's dimension or
97    * "zero-th" category.
98    * 
99    * @param category
100    * @param clParams
101    */
102   public void addCategoryListParams(CategoryPath category, CategoryListParams clParams) {
103     clParamsMap.put(category.getComponent(0), clParams);
104   }
105 }