Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BaseConfiguration |
|
| 1.75;1,75 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.commons.configuration; | |
19 | ||
20 | import java.util.ArrayList; | |
21 | import java.util.Iterator; | |
22 | import java.util.List; | |
23 | import java.util.Map; | |
24 | ||
25 | import org.apache.commons.collections.map.LinkedMap; | |
26 | ||
27 | /** | |
28 | * Basic configuration classe. Stores the configuration data but does not | |
29 | * provide any load or save functions. If you want to load your Configuration | |
30 | * from a file use PropertiesConfiguration or XmlConfiguration. | |
31 | * | |
32 | * This class extends normal Java properties by adding the possibility | |
33 | * to use the same key many times concatenating the value strings | |
34 | * instead of overwriting them. | |
35 | * | |
36 | * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a> | |
37 | * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> | |
38 | * @author <a href="mailto:daveb@miceda-data">Dave Bryson</a> | |
39 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> | |
40 | * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a> | |
41 | * @author <a href="mailto:kjohnson@transparent.com">Kent Johnson</a> | |
42 | * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> | |
43 | * @author <a href="mailto:ipriha@surfeu.fi">Ilkka Priha</a> | |
44 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> | |
45 | * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> | |
46 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> | |
47 | * @author <a href="mailto:ksh@scand.com">Konstantin Shaposhnikov</a> | |
48 | * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a> | |
49 | * @version $Id: BaseConfiguration.java 439648 2006-09-02 20:42:10Z oheger $ | |
50 | */ | |
51 | 2174 | public class BaseConfiguration extends AbstractConfiguration implements Cloneable |
52 | { | |
53 | /** stores the configuration key-value pairs */ | |
54 | 1087 | private Map store = new LinkedMap(); |
55 | ||
56 | /** | |
57 | * Adds a key/value pair to the map. This routine does no magic morphing. | |
58 | * It ensures the keylist is maintained | |
59 | * | |
60 | * @param key key to use for mapping | |
61 | * @param value object to store | |
62 | */ | |
63 | protected void addPropertyDirect(String key, Object value) | |
64 | { | |
65 | 31656 | Object previousValue = getProperty(key); |
66 | ||
67 | 31656 | if (previousValue == null) |
68 | { | |
69 | 20566 | store.put(key, value); |
70 | } | |
71 | 11090 | else if (previousValue instanceof List) |
72 | { | |
73 | // the value is added to the existing list | |
74 | 3068 | ((List) previousValue).add(value); |
75 | } | |
76 | else | |
77 | { | |
78 | // the previous value is replaced by a list containing the previous value and the new value | |
79 | 8022 | List list = new ArrayList(); |
80 | 8022 | list.add(previousValue); |
81 | 8022 | list.add(value); |
82 | ||
83 | 8022 | store.put(key, list); |
84 | } | |
85 | 31656 | } |
86 | ||
87 | /** | |
88 | * Read property from underlying map. | |
89 | * | |
90 | * @param key key to use for mapping | |
91 | * | |
92 | * @return object associated with the given configuration key. | |
93 | */ | |
94 | public Object getProperty(String key) | |
95 | { | |
96 | 33917 | return store.get(key); |
97 | } | |
98 | ||
99 | /** | |
100 | * Check if the configuration is empty | |
101 | * | |
102 | * @return <code>true</code> if Configuration is empty, | |
103 | * <code>false</code> otherwise. | |
104 | */ | |
105 | public boolean isEmpty() | |
106 | { | |
107 | 11 | return store.isEmpty(); |
108 | } | |
109 | ||
110 | /** | |
111 | * check if the configuration contains the key | |
112 | * | |
113 | * @param key the configuration key | |
114 | * | |
115 | * @return <code>true</code> if Configuration contain given key, | |
116 | * <code>false</code> otherwise. | |
117 | */ | |
118 | public boolean containsKey(String key) | |
119 | { | |
120 | 1470 | return store.containsKey(key); |
121 | } | |
122 | ||
123 | /** | |
124 | * Clear a property in the configuration. | |
125 | * | |
126 | * @param key the key to remove along with corresponding value. | |
127 | */ | |
128 | protected void clearPropertyDirect(String key) | |
129 | { | |
130 | 810 | if (containsKey(key)) |
131 | { | |
132 | 56 | store.remove(key); |
133 | } | |
134 | 810 | } |
135 | ||
136 | /** | |
137 | * {@inheritDoc} | |
138 | */ | |
139 | public void clear() | |
140 | { | |
141 | 169 | fireEvent(EVENT_CLEAR, null, null, true); |
142 | 169 | store.clear(); |
143 | 169 | fireEvent(EVENT_CLEAR, null, null, false); |
144 | 169 | } |
145 | ||
146 | /** | |
147 | * Get the list of the keys contained in the configuration | |
148 | * repository. | |
149 | * | |
150 | * @return An Iterator. | |
151 | */ | |
152 | public Iterator getKeys() | |
153 | { | |
154 | 150 | return store.keySet().iterator(); |
155 | } | |
156 | ||
157 | /** | |
158 | * Creates a copy of this object. This implementation will create a deep | |
159 | * clone, i.e. the map that stores the properties is cloned, too. So changes | |
160 | * performed at the copy won't affect the original and vice versa. | |
161 | * | |
162 | * @return the copy | |
163 | * @since 1.3 | |
164 | */ | |
165 | public Object clone() | |
166 | { | |
167 | try | |
168 | { | |
169 | 9 | BaseConfiguration copy = (BaseConfiguration) super.clone(); |
170 | 9 | copy.store = (Map) ConfigurationUtils.clone(store); |
171 | 9 | copy.clearConfigurationListeners(); |
172 | 9 | return copy; |
173 | } | |
174 | catch (CloneNotSupportedException cex) | |
175 | { | |
176 | // should not happen | |
177 | 0 | throw new ConfigurationRuntimeException(cex); |
178 | } | |
179 | } | |
180 | } |