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