1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 /***
25 * A Map based Configuration.
26 *
27 * @author Emmanuel Bourg
28 * @version $Revision$, $Date: 2005-10-09 20:27:12 +0200 (Sun, 09 Oct 2005) $
29 * @since 1.1
30 */
31 public class MapConfiguration extends AbstractConfiguration
32 {
33 /*** The Map decorated by this configuration. */
34 protected Map map;
35
36 /***
37 * Create a Configuration decorator around the specified Map. The map is
38 * used to store the configuration properties, any change will also affect
39 * the Map.
40 *
41 * @param map the map
42 */
43 public MapConfiguration(Map map)
44 {
45 this.map = map;
46 }
47
48 /***
49 * Return the Map decorated by this configuration.
50 *
51 * @return the map this configuration is based onto
52 */
53 public Map getMap()
54 {
55 return map;
56 }
57
58 public Object getProperty(String key)
59 {
60 Object value = map.get(key);
61 if (value instanceof String)
62 {
63 List list = PropertyConverter.split((String) value, getDelimiter());
64 return list.size() > 1 ? list : value;
65 }
66 else
67 {
68 return value;
69 }
70 }
71
72 protected void addPropertyDirect(String key, Object value)
73 {
74 Object previousValue = getProperty(key);
75
76 if (previousValue == null)
77 {
78 map.put(key, value);
79 }
80 else if (previousValue instanceof List)
81 {
82
83 ((List) previousValue).add(value);
84 }
85 else
86 {
87
88 List list = new ArrayList();
89 list.add(previousValue);
90 list.add(value);
91
92 map.put(key, list);
93 }
94 }
95
96 public boolean isEmpty()
97 {
98 return map.isEmpty();
99 }
100
101 public boolean containsKey(String key)
102 {
103 return map.containsKey(key);
104 }
105
106 public void clearProperty(String key)
107 {
108 map.remove(key);
109 }
110
111 public Iterator getKeys()
112 {
113 return map.keySet().iterator();
114 }
115 }