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