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.AbstractMap;
20 import java.util.AbstractSet;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Set;
24
25 /***
26 * <p>The <code>ConfigurationMap</code> wraps a
27 * configuration-collection
28 * {@link org.apache.commons.configuration.Configuration}
29 * instance to provide a <code>Map</code> interface.</p>
30 *
31 * <p><em>Note:</em> This implementation is incomplete.</p>
32 *
33 * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
34 * @version $Revision$, $Date: 2005-12-06 04:10:27 +0100 (Tue, 06 Dec 2005) $
35 * @since 1.0
36 */
37 public class ConfigurationMap extends AbstractMap
38 {
39 /***
40 * The <code>Configuration</code> wrapped by this class.
41 */
42 private Configuration configuration;
43
44 /***
45 * Creates a new instance of a <code>ConfigurationMap</code>
46 * that wraps the specified <code>Configuration</code>
47 * instance.
48 * @param configuration <code>Configuration</code>
49 * instance.
50 */
51 public ConfigurationMap(Configuration configuration)
52 {
53 this.configuration = configuration;
54 }
55
56 /***
57 * Returns the wrapped <code>Configuration</code> object.
58 *
59 * @return the wrapped configuration
60 * @since 1.2
61 */
62 public Configuration getConfiguration()
63 {
64 return configuration;
65 }
66
67 /***
68 * @see java.util.Map#entrySet()
69 */
70 public Set entrySet()
71 {
72 return new ConfigurationSet(configuration);
73 }
74
75 /***
76 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
77 */
78 public Object put(Object key, Object value)
79 {
80 String strKey = String.valueOf(key);
81 Object old = configuration.getProperty(strKey);
82 configuration.setProperty(strKey, value);
83 return old;
84 }
85
86 /***
87 * @see java.util.Map#get(java.lang.Object)
88 */
89 public Object get(Object key)
90 {
91 return configuration.getProperty(String.valueOf(key));
92 }
93
94 /***
95 * Set of entries in the map.
96 */
97 static class ConfigurationSet extends AbstractSet
98 {
99 /*** The configuration mapped to this entry set. */
100 private Configuration configuration;
101
102 /***
103 * A Map entry in the ConfigurationMap.
104 */
105 private final class Entry implements Map.Entry
106 {
107 /*** The key of the map entry. */
108 private Object key;
109
110 private Entry(Object key)
111 {
112 this.key = key;
113 }
114
115 public Object getKey()
116 {
117 return key;
118 }
119
120 public Object getValue()
121 {
122 return configuration.getProperty((String) key);
123 }
124
125 public Object setValue(Object value)
126 {
127 Object old = getValue();
128 configuration.setProperty((String) key, value);
129 return old;
130 }
131 }
132
133 /***
134 * Iterator over the entries in the ConfigurationMap.
135 */
136 private final class ConfigurationSetIterator implements Iterator
137 {
138 /*** An iterator over the keys in the configuration. */
139 private Iterator keys;
140
141 private ConfigurationSetIterator()
142 {
143 keys = configuration.getKeys();
144 }
145
146 public boolean hasNext()
147 {
148 return keys.hasNext();
149 }
150
151 public Object next()
152 {
153 return new Entry(keys.next());
154 }
155
156 public void remove()
157 {
158 keys.remove();
159 }
160 }
161
162 ConfigurationSet(Configuration configuration)
163 {
164 this.configuration = configuration;
165 }
166
167 /***
168 * @see java.util.Collection#size()
169 */
170 public int size()
171 {
172
173 int count = 0;
174 for (Iterator iterator = configuration.getKeys(); iterator.hasNext();)
175 {
176 iterator.next();
177 count++;
178 }
179 return count;
180 }
181
182 /***
183 * @see java.util.Collection#iterator()
184 */
185 public Iterator iterator()
186 {
187 return new ConfigurationSetIterator();
188 }
189 }
190 }