1 package org.apache.lucene.queryParser.core.config;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.util.HashMap;
22 import org.apache.lucene.util.Attribute;
23 import org.apache.lucene.util.AttributeSource;
27 * This class is the base of {@link QueryConfigHandler} and {@link FieldConfig}.
28 * It has operations to set, unset and get configuration values.
31 * Each configuration is is a key->value pair. The key should be an unique
32 * {@link ConfigurationKey} instance and it also holds the value's type.
35 * NOTE: in future this class will no longer extend {@link AttributeSource}
37 * @see ConfigurationKey
39 public abstract class AbstractQueryConfig extends AttributeSource {
41 final private HashMap<ConfigurationKey<?>, Object> configMap = new HashMap<ConfigurationKey<?>, Object>();
43 AbstractQueryConfig() {
44 // although this class is public, it can only be constructed from package
48 * Returns the value held by the given key.
50 * @param <T> the value's type
52 * @param key the key, cannot be <code>null</code>
54 * @return the value held by the given key
56 @SuppressWarnings("unchecked")
57 public <T> T get(ConfigurationKey<T> key) {
60 throw new IllegalArgumentException("key cannot be null!");
63 return (T) this.configMap.get(key);
68 * Returns the value held by the given key or the given default value if the
71 * @param <T> the value's type
73 * @param key the key, cannot be <code>null</code>
74 * @param defaultValue the default value
76 * @return the value held by the given key or the default value
78 @SuppressWarnings("unchecked")
79 public <T> T get(ConfigurationKey<T> key, T defaultValue) {
82 throw new IllegalArgumentException("key cannot be null!");
85 if (this.configMap.containsKey(key)) {
86 return (T) this.configMap.get(key);
94 * Returns true if there is a value set with the given key, otherwise false.
96 * @param <T> @param <T> the value's type
97 * @param key the key, cannot be <code>null</code>
98 * @return true if there is a value set with the given key, otherwise false
100 public <T> boolean has(ConfigurationKey<T> key) {
103 throw new IllegalArgumentException("key cannot be null!");
106 return this.configMap.containsKey(key);
111 * Sets a key and its value.
113 * @param <T> the value's type
114 * @param key the key, cannot be <code>null</code>
117 public <T> void set(ConfigurationKey<T> key, T value) {
120 throw new IllegalArgumentException("key cannot be null!");
127 this.configMap.put(key, value);
132 @SuppressWarnings("deprecation")
134 public <A extends Attribute> A addAttribute(Class<A> attClass) {
136 if (hasAttribute(attClass)) {
137 return getAttribute(attClass);
139 A attr = super.addAttribute(attClass);
141 if (attr instanceof ConfigAttribute) {
142 ((ConfigAttribute) attr).setQueryConfigHandler(this);
151 * Unsets the given key and its value.
153 * @param <T> the value's type
155 * @return true if the key and value was set and removed, otherwise false
157 public <T> boolean unset(ConfigurationKey<T> key) {
160 throw new IllegalArgumentException("key cannot be null!");
163 return this.configMap.remove(key) != null;