1 package org.apache.lucene.facet.enhancements.association;
3 import java.util.HashSet;
6 import org.apache.lucene.analysis.TokenStream;
8 import org.apache.lucene.facet.enhancements.CategoryEnhancement;
9 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
10 import org.apache.lucene.facet.index.attributes.CategoryAttribute;
11 import org.apache.lucene.facet.index.attributes.CategoryProperty;
12 import org.apache.lucene.facet.index.streaming.CategoryListTokenizer;
13 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
14 import org.apache.lucene.util.Vint8;
15 import org.apache.lucene.util.Vint8.Position;
18 * Licensed to the Apache Software Foundation (ASF) under one or more
19 * contributor license agreements. See the NOTICE file distributed with
20 * this work for additional information regarding copyright ownership.
21 * The ASF licenses this file to You under the Apache License, Version 2.0
22 * (the "License"); you may not use this file except in compliance with
23 * the License. You may obtain a copy of the License at
25 * http://www.apache.org/licenses/LICENSE-2.0
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
35 * A {@link CategoryEnhancement} for adding associations data to the index
36 * (categories with {@link AssociationProperty}s).
38 * @lucene.experimental
40 public class AssociationEnhancement implements CategoryEnhancement {
42 static final String CATEGORY_LIST_TERM_TEXT = "CATEGORY_ASSOCIATION_LIST";
44 /** Property Classes which extend AssociationProperty */
45 private static final HashSet<Class<? extends CategoryProperty>> ASSOCIATION_PROPERTY_CLASSES;
47 /** Property Classes which do not extend AssociationProperty */
48 private static final HashSet<Class<? extends CategoryProperty>> NON_ASSOCIATION_PROPERTY_CLASSES;
51 ASSOCIATION_PROPERTY_CLASSES = new HashSet<Class<? extends CategoryProperty>>();
52 NON_ASSOCIATION_PROPERTY_CLASSES = new HashSet<Class<? extends CategoryProperty>>();
56 * For a given class which extends a CategoryProperty, answers whether it is
57 * an instance of AssociationProperty (AP) or not. <br>
58 * This method is a cheaper replacement for a call to
59 * <code>instanceof</code>. It has two HashSets - one for classes which are
60 * an extension to AP and one for the classes which are not. Whenever a
61 * property class is introduced:
63 * <li>if it is known as a property class extending AP (contained in the
64 * validHashSet)- returns true</li>
65 * <li>if it is known as a property class NOT extending AP - returns false</li>
67 * If it was not matched against both sets, it calls 'instanceof' to find
68 * out if it extends AP, puts it in the matching Set and returning true or
69 * false accordingly</li>
72 * NOTE: 'instanceof' is only called once per a Class (not instance) of a
73 * property. And as there are few properties (currently 4 concrete
74 * implementations) the two sets would be rather small
76 public static boolean isAssociationProperty(Class<? extends CategoryProperty> clazz) {
77 if (ASSOCIATION_PROPERTY_CLASSES.contains(clazz)) {
81 if (NON_ASSOCIATION_PROPERTY_CLASSES.contains(clazz)) {
85 if (AssociationProperty.class.isAssignableFrom(clazz)) {
86 ASSOCIATION_PROPERTY_CLASSES.add(clazz);
90 NON_ASSOCIATION_PROPERTY_CLASSES.add(clazz);
94 public boolean generatesCategoryList() {
98 public String getCategoryListTermText() {
99 return CATEGORY_LIST_TERM_TEXT;
102 public CategoryListTokenizer getCategoryListTokenizer(
103 TokenStream tokenizer, EnhancementsIndexingParams indexingParams,
104 TaxonomyWriter taxonomyWriter) {
105 return new AssociationListTokenizer(tokenizer, indexingParams, this);
108 public byte[] getCategoryTokenBytes(CategoryAttribute categoryAttribute) {
110 AssociationProperty property = getAssociationProperty(categoryAttribute);
112 if (property == null) {
116 int association = property.getAssociation();
117 int bytesNeeded = Vint8.bytesNeeded(association);
118 byte[] buffer = new byte[bytesNeeded];
119 Vint8.encode(association, buffer, 0);
123 public static AssociationProperty getAssociationProperty(
124 CategoryAttribute categoryAttribute) {
125 AssociationProperty property = null;
126 Set<Class<? extends CategoryProperty>> propertyClasses = categoryAttribute
127 .getPropertyClasses();
128 if (propertyClasses == null) {
131 for (Class<? extends CategoryProperty> clazz : propertyClasses) {
132 if (isAssociationProperty(clazz)) {
133 property = (AssociationProperty) categoryAttribute
141 public Object extractCategoryTokenData(byte[] buffer, int offset, int length) {
145 Integer i = Integer.valueOf(Vint8.decode(buffer, new Position(offset)));
149 public Class<? extends CategoryProperty> getRetainableProperty() {