pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / index / params / DefaultFacetIndexingParams.java
1 package org.apache.lucene.facet.index.params;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.lucene.facet.index.categorypolicy.DefaultOrdinalPolicy;
7 import org.apache.lucene.facet.index.categorypolicy.DefaultPathPolicy;
8 import org.apache.lucene.facet.index.categorypolicy.OrdinalPolicy;
9 import org.apache.lucene.facet.index.categorypolicy.PathPolicy;
10 import org.apache.lucene.facet.taxonomy.CategoryPath;
11
12 /**
13  * Licensed to the Apache Software Foundation (ASF) under one or more
14  * contributor license agreements.  See the NOTICE file distributed with
15  * this work for additional information regarding copyright ownership.
16  * The ASF licenses this file to You under the Apache License, Version 2.0
17  * (the "License"); you may not use this file except in compliance with
18  * the License.  You may obtain a copy of the License at
19  *
20  *     http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS,
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28
29 /**
30  * Default implementation for {@link FacetIndexingParams}.
31  * <p>
32  * Getters for <em>partition-size</em>, {@link OrdinalPolicy} and
33  * {@link PathPolicy} are all final, and so the proper way to modify them when
34  * extending this class is through {@link #fixedPartitionSize()},
35  * {@link #fixedOrdinalPolicy()} or {@link #fixedPathPolicy()} accordingly.
36  * 
37  * @lucene.experimental
38  */
39 public class DefaultFacetIndexingParams implements FacetIndexingParams {
40
41   /**
42    * delimiter between a categories in a path, e.g. Products FACET_DELIM
43    * Consumer FACET_DELIM Tv. This should be a character not found in any path
44    * component
45    */
46   public static final char DEFAULT_FACET_DELIM_CHAR = '\uF749';
47
48   private final CategoryListParams clpParams;
49   private final OrdinalPolicy ordinalPolicy;
50   private final PathPolicy pathPolicy;
51   private final int partitionSize;
52
53   public DefaultFacetIndexingParams() {
54     this(new CategoryListParams());
55   }
56
57   public DefaultFacetIndexingParams(CategoryListParams categoryListParams) {
58     clpParams = categoryListParams;
59     ordinalPolicy = fixedOrdinalPolicy();
60     pathPolicy = fixedPathPolicy();
61     partitionSize = fixedPartitionSize();
62   }
63
64   public CategoryListParams getCategoryListParams(CategoryPath category) {
65     return clpParams;
66   }
67
68   public int drillDownTermText(CategoryPath path, char[] buffer) {
69     return path.copyToCharArray(buffer, 0, -1, getFacetDelimChar());
70   }
71
72   /**
73    * "fixed" partition size. 
74    * @see #getPartitionSize()
75    */
76   protected int fixedPartitionSize() {
77     return Integer.MAX_VALUE;
78   }
79   
80   /**
81    * "fixed" ordinal policy. 
82    * @see #getOrdinalPolicy()
83    */
84   protected OrdinalPolicy fixedOrdinalPolicy() {
85     return new DefaultOrdinalPolicy();
86   }
87   
88   /**
89    * "fixed" path policy. 
90    * @see #getPathPolicy()
91    */
92   protected PathPolicy fixedPathPolicy() {
93     return new DefaultPathPolicy();
94   }
95   
96   public final int getPartitionSize() {
97     return partitionSize;
98   }
99
100   /*
101    * (non-Javadoc)
102    * 
103    * @see
104    * org.apache.lucene.facet.index.params.FacetIndexingParams#getAllCategoryListParams
105    * ()
106    */
107   public Iterable<CategoryListParams> getAllCategoryListParams() {
108     List<CategoryListParams> res = new ArrayList<CategoryListParams>();
109     res.add(clpParams);
110     return res;
111   }
112
113   public final OrdinalPolicy getOrdinalPolicy() {
114     return ordinalPolicy;
115   }
116
117   public final PathPolicy getPathPolicy() {
118     return pathPolicy;
119   }
120
121   /* (non-Javadoc)
122    * @see java.lang.Object#hashCode()
123    */
124   @Override
125   public int hashCode() {
126     final int prime = 31;
127     int result = 1;
128     result = prime * result
129         + ((clpParams == null) ? 0 : clpParams.hashCode());
130     result = prime * result
131         + ((ordinalPolicy == null) ? 0 : ordinalPolicy.hashCode());
132     result = prime * result + partitionSize;
133     result = prime * result
134         + ((pathPolicy == null) ? 0 : pathPolicy.hashCode());
135     
136     for (CategoryListParams clp: getAllCategoryListParams()) {
137       result ^= clp.hashCode();
138     }
139     
140     return result;
141   }
142
143   /* (non-Javadoc)
144    * @see java.lang.Object#equals(java.lang.Object)
145    */
146   @Override
147   public boolean equals(Object obj) {
148     if (this == obj) {
149       return true;
150     }
151     if (obj == null) {
152       return false;
153     }
154     if (!(obj instanceof DefaultFacetIndexingParams)) {
155       return false;
156     }
157     DefaultFacetIndexingParams other = (DefaultFacetIndexingParams) obj;
158     if (clpParams == null) {
159       if (other.clpParams != null) {
160         return false;
161       }
162     } else if (!clpParams.equals(other.clpParams)) {
163       return false;
164     }
165     if (ordinalPolicy == null) {
166       if (other.ordinalPolicy != null) {
167         return false;
168       }
169     } else if (!ordinalPolicy.equals(other.ordinalPolicy)) {
170       return false;
171     }
172     if (partitionSize != other.partitionSize) {
173       return false;
174     }
175     if (pathPolicy == null) {
176       if (other.pathPolicy != null) {
177         return false;
178       }
179     } else if (!pathPolicy.equals(other.pathPolicy)) {
180       return false;
181     }
182     
183     Iterable<CategoryListParams> cLs = getAllCategoryListParams();
184     Iterable<CategoryListParams> otherCLs = other.getAllCategoryListParams();
185     
186     return cLs.equals(otherCLs);
187   }
188
189   /**
190    * Use {@link #DEFAULT_FACET_DELIM_CHAR} as the delimiter.
191    */
192   public char getFacetDelimChar() {
193     return DEFAULT_FACET_DELIM_CHAR;
194   }
195
196 }