1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 /***
26 * <p>A Map based Configuration.</p>
27 * <p><em>Note:</em>Configuration objects of this type can be read concurrently
28 * by multiple threads. However if one of these threads modifies the object,
29 * synchronization has to be performed manually.</p>
30 *
31 * @author Emmanuel Bourg
32 * @version $Revision: 548098 $, $Date: 2007-06-17 21:34:03 +0200 (So, 17 Jun 2007) $
33 * @since 1.1
34 */
35 public class MapConfiguration extends AbstractConfiguration implements Cloneable
36 {
37 /*** The Map decorated by this configuration. */
38 protected Map map;
39
40 /***
41 * Create a Configuration decorator around the specified Map. The map is
42 * used to store the configuration properties, any change will also affect
43 * the Map.
44 *
45 * @param map the map
46 */
47 public MapConfiguration(Map map)
48 {
49 this.map = map;
50 }
51
52 /***
53 * Return the Map decorated by this configuration.
54 *
55 * @return the map this configuration is based onto
56 */
57 public Map getMap()
58 {
59 return map;
60 }
61
62 public Object getProperty(String key)
63 {
64 Object value = map.get(key);
65 if ((value instanceof String) && (!isDelimiterParsingDisabled()))
66 {
67 List list = PropertyConverter.split((String) value, getListDelimiter());
68 return list.size() > 1 ? list : list.get(0);
69 }
70 else
71 {
72 return value;
73 }
74 }
75
76 protected void addPropertyDirect(String key, Object value)
77 {
78 Object previousValue = getProperty(key);
79
80 if (previousValue == null)
81 {
82 map.put(key, value);
83 }
84 else if (previousValue instanceof List)
85 {
86
87 ((List) previousValue).add(value);
88 }
89 else
90 {
91
92 List list = new ArrayList();
93 list.add(previousValue);
94 list.add(value);
95
96 map.put(key, list);
97 }
98 }
99
100 public boolean isEmpty()
101 {
102 return map.isEmpty();
103 }
104
105 public boolean containsKey(String key)
106 {
107 return map.containsKey(key);
108 }
109
110 protected void clearPropertyDirect(String key)
111 {
112 map.remove(key);
113 }
114
115 public Iterator getKeys()
116 {
117 return map.keySet().iterator();
118 }
119
120 /***
121 * Returns a copy of this object. The returned configuration will contain
122 * the same properties as the original. Event listeners are not cloned.
123 *
124 * @return the copy
125 * @since 1.3
126 */
127 public Object clone()
128 {
129 try
130 {
131 MapConfiguration copy = (MapConfiguration) super.clone();
132 copy.clearConfigurationListeners();
133 copy.map = (Map) ConfigurationUtils.clone(map);
134 return copy;
135 }
136 catch (CloneNotSupportedException cex)
137 {
138
139 throw new ConfigurationRuntimeException(cex);
140 }
141 }
142 }