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: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
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 * @see java.util.Map#entrySet()
70 */
71 public Set entrySet()
72 {
73 return new ConfigurationSet(configuration);
74 }
75
76 /***
77 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
78 */
79 public Object put(Object key, Object value)
80 {
81 String strKey = String.valueOf(key);
82 Object old = configuration.getProperty(strKey);
83 configuration.setProperty(strKey, value);
84 return old;
85 }
86
87 /***
88 * @see java.util.Map#get(java.lang.Object)
89 */
90 public Object get(Object key)
91 {
92 return configuration.getProperty(String.valueOf(key));
93 }
94
95 /***
96 * Set of entries in the map.
97 */
98 static class ConfigurationSet extends AbstractSet
99 {
100 /*** The configuration mapped to this entry set. */
101 private Configuration configuration;
102
103 /***
104 * A Map entry in the ConfigurationMap.
105 */
106 private final class Entry implements Map.Entry
107 {
108 /*** The key of the map entry. */
109 private Object key;
110
111 private Entry(Object key)
112 {
113 this.key = key;
114 }
115
116 public Object getKey()
117 {
118 return key;
119 }
120
121 public Object getValue()
122 {
123 return configuration.getProperty((String) key);
124 }
125
126 public Object setValue(Object value)
127 {
128 Object old = getValue();
129 configuration.setProperty((String) key, value);
130 return old;
131 }
132 }
133
134 /***
135 * Iterator over the entries in the ConfigurationMap.
136 */
137 private final class ConfigurationSetIterator implements Iterator
138 {
139 /*** An iterator over the keys in the configuration. */
140 private Iterator keys;
141
142 private ConfigurationSetIterator()
143 {
144 keys = configuration.getKeys();
145 }
146
147 public boolean hasNext()
148 {
149 return keys.hasNext();
150 }
151
152 public Object next()
153 {
154 return new Entry(keys.next());
155 }
156
157 public void remove()
158 {
159 keys.remove();
160 }
161 }
162
163 ConfigurationSet(Configuration configuration)
164 {
165 this.configuration = configuration;
166 }
167
168 /***
169 * @see java.util.Collection#size()
170 */
171 public int size()
172 {
173
174 int count = 0;
175 for (Iterator iterator = configuration.getKeys(); iterator.hasNext();)
176 {
177 iterator.next();
178 count++;
179 }
180 return count;
181 }
182
183 /***
184 * @see java.util.Collection#iterator()
185 */
186 public Iterator iterator()
187 {
188 return new ConfigurationSetIterator();
189 }
190 }
191 }