1 package org.apache.lucene.facet.index.attributes;
3 import java.util.Collection;
4 import java.util.HashMap;
7 import org.apache.lucene.util.AttributeImpl;
9 import org.apache.lucene.facet.taxonomy.CategoryPath;
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
19 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 * An implementation of {@link CategoryAttribute}.
31 * @lucene.experimental
33 public final class CategoryAttributeImpl extends AttributeImpl implements
37 * The category path instance.
39 protected CategoryPath categoryPath;
42 * A map of properties associated to the current category path.
44 protected HashMap<Class<? extends CategoryProperty>, CategoryProperty> properties;
47 * Construct an empty CategoryAttributeImpl.
49 public CategoryAttributeImpl() {
54 * Construct a CategoryAttributeImpl with the given CategoryPath.
57 * The category path to use.
59 public CategoryAttributeImpl(CategoryPath categoryPath) {
60 setCategoryPath(categoryPath);
63 public void set(CategoryAttribute other) {
64 ((CategoryAttributeImpl) other).copyTo(this);
68 * Returns the category path value.
70 * @return The category path last assigned to this attribute, or null if
71 * none has been assigned.
73 public CategoryPath getCategoryPath() {
77 public void setCategoryPath(CategoryPath cp) {
81 public void addProperty(CategoryProperty property)
82 throws UnsupportedOperationException {
83 if (properties == null) {
84 properties = new HashMap<Class<? extends CategoryProperty>, CategoryProperty>();
86 CategoryProperty existing = properties.get(property.getClass());
87 if (existing == null) {
88 properties.put(property.getClass(), property);
90 existing.merge(property);
94 public CategoryProperty getProperty(
95 Class<? extends CategoryProperty> propertyClass) {
96 if (properties == null) {
99 return properties.get(propertyClass);
102 public CategoryProperty getProperty(
103 Collection<Class<? extends CategoryProperty>> propertyClasses) {
104 if (properties == null) {
107 for (Class<? extends CategoryProperty> propertyClass : propertyClasses) {
108 CategoryProperty categoryProperty = properties.get(propertyClass);
109 if (categoryProperty != null) {
110 return categoryProperty;
117 public void copyTo(AttributeImpl target) {
118 ((CategoryAttributeImpl) target).categoryPath = this.categoryPath;
119 ((CategoryAttributeImpl) target).properties = this.properties;
122 @SuppressWarnings("unchecked")
124 public CategoryAttribute clone() {
125 CategoryAttributeImpl ca = (CategoryAttributeImpl) super.clone();
126 if (categoryPath != null) {
127 ca.categoryPath = (CategoryPath) categoryPath.clone();
129 if (properties != null && !properties.isEmpty()) {
130 ca.properties = (HashMap<Class<? extends CategoryProperty>, CategoryProperty>) properties
137 public void clear() {
142 public void clearProperties() {
143 if (properties != null) {
149 public boolean equals(Object o) {
153 if (!(o instanceof CategoryAttributeImpl)) {
156 CategoryAttributeImpl other = (CategoryAttributeImpl) o;
157 if (categoryPath == null) {
158 return (other.categoryPath == null);
160 if (!categoryPath.equals(other.categoryPath)) {
163 if (properties == null || properties.isEmpty()) {
164 return (other.properties == null || other.properties.isEmpty());
166 return properties.equals(other.properties);
170 public int hashCode() {
171 if (categoryPath == null) {
174 int hashCode = categoryPath.hashCode();
175 if (properties != null && !properties.isEmpty()) {
176 hashCode ^= properties.hashCode();
181 public Set<Class<? extends CategoryProperty>> getPropertyClasses() {
182 if (properties == null || properties.isEmpty()) {
185 return properties.keySet();
188 public void remove(Class<? extends CategoryProperty> propertyClass) {
189 properties.remove(propertyClass);