pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / index / attributes / CategoryAttributeImpl.java
1 package org.apache.lucene.facet.index.attributes;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Set;
6
7 import org.apache.lucene.util.AttributeImpl;
8
9 import org.apache.lucene.facet.taxonomy.CategoryPath;
10
11 /**
12  * Licensed to the Apache Software Foundation (ASF) under one or more
13  * contributor license agreements.  See the NOTICE file distributed with
14  * this work for additional information regarding copyright ownership.
15  * The ASF licenses this file to You under the Apache License, Version 2.0
16  * (the "License"); you may not use this file except in compliance with
17  * the License.  You may obtain a copy of the License at
18  *
19  *     http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */
27
28 /**
29  * An implementation of {@link CategoryAttribute}.
30  * 
31  * @lucene.experimental
32  */
33 public final class CategoryAttributeImpl extends AttributeImpl implements
34     CategoryAttribute {
35
36   /**
37    * The category path instance.
38    */
39   protected CategoryPath categoryPath;
40
41   /**
42    * A map of properties associated to the current category path.
43    */
44   protected HashMap<Class<? extends CategoryProperty>, CategoryProperty> properties;
45
46   /**
47    * Construct an empty CategoryAttributeImpl.
48    */
49   public CategoryAttributeImpl() {
50     // do nothing
51   }
52
53   /**
54    * Construct a CategoryAttributeImpl with the given CategoryPath.
55    * 
56    * @param categoryPath
57    *            The category path to use.
58    */
59   public CategoryAttributeImpl(CategoryPath categoryPath) {
60     setCategoryPath(categoryPath);
61   }
62
63   public void set(CategoryAttribute other) {
64     ((CategoryAttributeImpl) other).copyTo(this);
65   }
66
67   /**
68    * Returns the category path value.
69    * 
70    * @return The category path last assigned to this attribute, or null if
71    *         none has been assigned.
72    */
73   public CategoryPath getCategoryPath() {
74     return categoryPath;
75   }
76
77   public void setCategoryPath(CategoryPath cp) {
78     categoryPath = cp;
79   }
80
81   public void addProperty(CategoryProperty property)
82       throws UnsupportedOperationException {
83     if (properties == null) {
84       properties = new HashMap<Class<? extends CategoryProperty>, CategoryProperty>();
85     }
86     CategoryProperty existing = properties.get(property.getClass());
87     if (existing == null) {
88       properties.put(property.getClass(), property);
89     } else {
90       existing.merge(property);
91     }
92   }
93
94   public CategoryProperty getProperty(
95       Class<? extends CategoryProperty> propertyClass) {
96     if (properties == null) {
97       return null;
98     }
99     return properties.get(propertyClass);
100   }
101
102   public CategoryProperty getProperty(
103       Collection<Class<? extends CategoryProperty>> propertyClasses) {
104     if (properties == null) {
105       return null;
106     }
107     for (Class<? extends CategoryProperty> propertyClass : propertyClasses) {
108       CategoryProperty categoryProperty = properties.get(propertyClass);
109       if (categoryProperty != null) {
110         return categoryProperty;
111       }
112     }
113     return null;
114   }
115
116   @Override
117   public void copyTo(AttributeImpl target) {
118     ((CategoryAttributeImpl) target).categoryPath = this.categoryPath;
119     ((CategoryAttributeImpl) target).properties = this.properties;
120   }
121
122   @SuppressWarnings("unchecked")
123   @Override
124   public CategoryAttribute clone() {
125     CategoryAttributeImpl ca = (CategoryAttributeImpl) super.clone();
126     if (categoryPath != null) {
127       ca.categoryPath = (CategoryPath) categoryPath.clone();
128     }
129     if (properties != null && !properties.isEmpty()) {
130       ca.properties = (HashMap<Class<? extends CategoryProperty>, CategoryProperty>) properties
131           .clone();
132     }
133     return ca;
134   }
135
136   @Override
137   public void clear() {
138     categoryPath = null;
139     clearProperties();
140   }
141
142   public void clearProperties() {
143     if (properties != null) {
144       properties.clear();
145     }
146   }
147
148   @Override
149   public boolean equals(Object o) {
150     if (o == this) {
151       return true;
152     }
153     if (!(o instanceof CategoryAttributeImpl)) {
154       return false;
155     }
156     CategoryAttributeImpl other = (CategoryAttributeImpl) o;
157     if (categoryPath == null) {
158       return (other.categoryPath == null);
159     }
160     if (!categoryPath.equals(other.categoryPath)) {
161       return false;
162     }
163     if (properties == null || properties.isEmpty()) {
164       return (other.properties == null || other.properties.isEmpty());
165     }
166     return properties.equals(other.properties);
167   }
168
169   @Override
170   public int hashCode() {
171     if (categoryPath == null) {
172       return 0;
173     }
174     int hashCode = categoryPath.hashCode();
175     if (properties != null && !properties.isEmpty()) {
176       hashCode ^= properties.hashCode();
177     }
178     return hashCode;
179   }
180
181   public Set<Class<? extends CategoryProperty>> getPropertyClasses() {
182     if (properties == null || properties.isEmpty()) {
183       return null;
184     }
185     return properties.keySet();
186   }
187
188   public void remove(Class<? extends CategoryProperty> propertyClass) {
189     properties.remove(propertyClass);
190   }
191
192 }