pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / config / AbstractQueryConfig.java
1 package org.apache.lucene.queryParser.core.config;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.util.HashMap;
21
22 import org.apache.lucene.util.Attribute;
23 import org.apache.lucene.util.AttributeSource;
24
25 /**
26  * <p>
27  * This class is the base of {@link QueryConfigHandler} and {@link FieldConfig}.
28  * It has operations to set, unset and get configuration values.
29  * </p>
30  * <p>
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.
33  * </p>
34  * 
35  * NOTE: in future this class will no longer extend {@link AttributeSource}
36  * 
37  * @see ConfigurationKey
38  */
39 public abstract class AbstractQueryConfig extends AttributeSource {
40   
41   final private HashMap<ConfigurationKey<?>, Object> configMap = new HashMap<ConfigurationKey<?>, Object>();
42   
43   AbstractQueryConfig() {
44     // although this class is public, it can only be constructed from package
45   }
46   
47   /**
48    * Returns the value held by the given key.
49    * 
50    * @param <T> the value's type
51    * 
52    * @param key the key, cannot be <code>null</code>
53    * 
54    * @return the value held by the given key
55    */
56   @SuppressWarnings("unchecked")
57   public <T> T get(ConfigurationKey<T> key) {
58     
59     if (key == null) {
60       throw new IllegalArgumentException("key cannot be null!");
61     }
62     
63     return (T) this.configMap.get(key);
64     
65   }
66
67   /**
68    * Returns the value held by the given key or the given default value if the
69    * key is not found.
70    * 
71    * @param <T> the value's type
72    * 
73    * @param key the key, cannot be <code>null</code>
74    * @param defaultValue the default value
75    * 
76    * @return the value held by the given key or the default value
77    */
78   @SuppressWarnings("unchecked")
79   public <T> T get(ConfigurationKey<T> key, T defaultValue) {
80     
81     if (key == null) {
82       throw new IllegalArgumentException("key cannot be null!");
83     }
84     
85     if (this.configMap.containsKey(key)) {
86       return (T) this.configMap.get(key);
87     } else {
88       return defaultValue;
89     }
90     
91   }
92
93   /**
94    * Returns true if there is a value set with the given key, otherwise false.
95    * 
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
99    */
100   public <T> boolean has(ConfigurationKey<T> key) {
101     
102     if (key == null) {
103       throw new IllegalArgumentException("key cannot be null!");
104     }
105     
106     return this.configMap.containsKey(key);
107     
108   }
109   
110   /**
111    * Sets a key and its value.
112    * 
113    * @param <T> the value's type
114    * @param key the key, cannot be <code>null</code>
115    * @param value
116    */
117   public <T> void set(ConfigurationKey<T> key, T value) {
118     
119     if (key == null) {
120       throw new IllegalArgumentException("key cannot be null!");
121     }
122     
123     if (value == null) {
124       unset(key);
125       
126     } else {
127       this.configMap.put(key, value);
128     }
129     
130   }
131   
132   @SuppressWarnings("deprecation")
133   @Override
134   public <A extends Attribute> A addAttribute(Class<A> attClass) {
135     
136     if (hasAttribute(attClass)) {
137       return getAttribute(attClass);
138     } else {
139       A attr = super.addAttribute(attClass);
140       
141       if (attr instanceof ConfigAttribute) {
142         ((ConfigAttribute) attr).setQueryConfigHandler(this);
143       }
144       
145       return attr;
146     }
147     
148   }
149
150   /**
151    * Unsets the given key and its value.
152    * 
153    * @param <T> the value's type
154    * @param key the key
155    * @return true if the key and value was set and removed, otherwise false
156    */
157   public <T> boolean unset(ConfigurationKey<T> key) {
158     
159     if (key == null) {
160       throw new IllegalArgumentException("key cannot be null!");
161     }
162     
163     return this.configMap.remove(key) != null;
164     
165   }
166   
167 }