~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/facet/src/java/org/apache/lucene/facet/enhancements/association/AssociationEnhancement.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.facet.enhancements.association;
2
 
 
3
 
import java.util.HashSet;
4
 
import java.util.Set;
5
 
 
6
 
import org.apache.lucene.analysis.TokenStream;
7
 
 
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;
16
 
 
17
 
/**
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
24
 
 *
25
 
 *     http://www.apache.org/licenses/LICENSE-2.0
26
 
 *
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.
32
 
 */
33
 
 
34
 
/**
35
 
 * A {@link CategoryEnhancement} for adding associations data to the index
36
 
 * (categories with {@link AssociationProperty}s).
37
 
 * 
38
 
 * @lucene.experimental
39
 
 */
40
 
public class AssociationEnhancement implements CategoryEnhancement {
41
 
 
42
 
  static final String CATEGORY_LIST_TERM_TEXT = "CATEGORY_ASSOCIATION_LIST";
43
 
 
44
 
  /** Property Classes which extend AssociationProperty */
45
 
  private static final HashSet<Class<? extends CategoryProperty>> ASSOCIATION_PROPERTY_CLASSES;
46
 
 
47
 
  /** Property Classes which do not extend AssociationProperty */
48
 
  private static final HashSet<Class<? extends CategoryProperty>> NON_ASSOCIATION_PROPERTY_CLASSES;
49
 
  
50
 
  static {
51
 
    ASSOCIATION_PROPERTY_CLASSES = new HashSet<Class<? extends CategoryProperty>>();
52
 
    NON_ASSOCIATION_PROPERTY_CLASSES = new HashSet<Class<? extends CategoryProperty>>();
53
 
  }
54
 
 
55
 
  /**
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:
62
 
   * <ul>
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>
66
 
   * <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>
70
 
   *</ul>
71
 
   * 
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
75
 
   */
76
 
  public static boolean isAssociationProperty(Class<? extends CategoryProperty> clazz) {
77
 
    if (ASSOCIATION_PROPERTY_CLASSES.contains(clazz)) {
78
 
      return true;
79
 
    }
80
 
    
81
 
    if (NON_ASSOCIATION_PROPERTY_CLASSES.contains(clazz)) {
82
 
      return false;
83
 
    }
84
 
    
85
 
    if (AssociationProperty.class.isAssignableFrom(clazz)) {
86
 
      ASSOCIATION_PROPERTY_CLASSES.add(clazz);
87
 
      return true;
88
 
    }
89
 
    
90
 
    NON_ASSOCIATION_PROPERTY_CLASSES.add(clazz);
91
 
    return false;
92
 
  }
93
 
  
94
 
  public boolean generatesCategoryList() {
95
 
    return true;
96
 
  }
97
 
 
98
 
  public String getCategoryListTermText() {
99
 
    return CATEGORY_LIST_TERM_TEXT;
100
 
  }
101
 
 
102
 
  public CategoryListTokenizer getCategoryListTokenizer(
103
 
      TokenStream tokenizer, EnhancementsIndexingParams indexingParams,
104
 
      TaxonomyWriter taxonomyWriter) {
105
 
    return new AssociationListTokenizer(tokenizer, indexingParams, this);
106
 
  }
107
 
 
108
 
  public byte[] getCategoryTokenBytes(CategoryAttribute categoryAttribute) {
109
 
    
110
 
    AssociationProperty property = getAssociationProperty(categoryAttribute);
111
 
    
112
 
    if (property == null) {
113
 
      return null;
114
 
    }
115
 
    
116
 
    int association = property.getAssociation();
117
 
    int bytesNeeded = Vint8.bytesNeeded(association);
118
 
    byte[] buffer = new byte[bytesNeeded];
119
 
    Vint8.encode(association, buffer, 0);
120
 
    return buffer;
121
 
  }
122
 
 
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) {
129
 
      return null;
130
 
    }
131
 
    for (Class<? extends CategoryProperty> clazz : propertyClasses) {
132
 
      if (isAssociationProperty(clazz)) {
133
 
        property = (AssociationProperty) categoryAttribute
134
 
            .getProperty(clazz);
135
 
        break;
136
 
      }
137
 
    }
138
 
    return property;
139
 
  }
140
 
 
141
 
  public Object extractCategoryTokenData(byte[] buffer, int offset, int length) {
142
 
    if (length == 0) {
143
 
      return null;
144
 
    }
145
 
    Integer i = Integer.valueOf(Vint8.decode(buffer, new Position(offset)));
146
 
    return i;
147
 
  }
148
 
 
149
 
  public Class<? extends CategoryProperty> getRetainableProperty() {
150
 
    return null;
151
 
  }
152
 
 
153
 
}