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
28
29
30
31
32
33
34
35
36
37
38 public class ConfigurationMap extends AbstractMap<Object, Object>
39 {
40
41
42
43 private final Configuration configuration;
44
45
46
47
48
49
50
51
52 public ConfigurationMap(Configuration configuration)
53 {
54 this.configuration = configuration;
55 }
56
57
58
59
60
61
62
63 public Configuration getConfiguration()
64 {
65 return configuration;
66 }
67
68
69
70
71
72
73
74 @Override
75 public Set<Map.Entry<Object, Object>> entrySet()
76 {
77 return new ConfigurationSet(configuration);
78 }
79
80
81
82
83
84
85
86
87
88
89 @Override
90 public Object put(Object key, Object value)
91 {
92 String strKey = String.valueOf(key);
93 Object old = configuration.getProperty(strKey);
94 configuration.setProperty(strKey, value);
95 return old;
96 }
97
98
99
100
101
102
103
104
105
106 @Override
107 public Object get(Object key)
108 {
109 return configuration.getProperty(String.valueOf(key));
110 }
111
112
113
114
115 static class ConfigurationSet extends AbstractSet<Map.Entry<Object, Object>>
116 {
117
118 private final Configuration configuration;
119
120
121
122
123 private final class Entry implements Map.Entry<Object, Object>
124 {
125
126 private Object key;
127
128 private Entry(Object key)
129 {
130 this.key = key;
131 }
132
133 public Object getKey()
134 {
135 return key;
136 }
137
138 public Object getValue()
139 {
140 return configuration.getProperty((String) key);
141 }
142
143 public Object setValue(Object value)
144 {
145 Object old = getValue();
146 configuration.setProperty((String) key, value);
147 return old;
148 }
149 }
150
151
152
153
154 private final class ConfigurationSetIterator implements Iterator<Map.Entry<Object, Object>>
155 {
156
157 private final Iterator<String> keys;
158
159 private ConfigurationSetIterator()
160 {
161 keys = configuration.getKeys();
162 }
163
164 public boolean hasNext()
165 {
166 return keys.hasNext();
167 }
168
169 public Map.Entry<Object, Object> next()
170 {
171 return new Entry(keys.next());
172 }
173
174 public void remove()
175 {
176 keys.remove();
177 }
178 }
179
180 ConfigurationSet(Configuration configuration)
181 {
182 this.configuration = configuration;
183 }
184
185
186
187
188 @Override
189 public int size()
190 {
191
192 int count = 0;
193 for (Iterator<String> iterator = configuration.getKeys(); iterator.hasNext();)
194 {
195 iterator.next();
196 count++;
197 }
198 return count;
199 }
200
201
202
203
204 @Override
205 public Iterator<Map.Entry<Object, Object>> iterator()
206 {
207 return new ConfigurationSetIterator();
208 }
209 }
210 }