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 /***
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 * @todo This implementation is incomplete.
33 *
34 * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
35 */
36
37 public class ConfigurationMap
38 extends AbstractMap
39 {
40
41 /***
42 * The <code>Configuration</code> wrapped by this class.
43 */
44 Configuration configuration = null;
45
46 /***
47 * Creates a new instance of a <code>ConfigurationMap</code>
48 * that wraps the specified <code>Configuration</code>
49 * instance.
50 * @param configuration <code>Configuration</code>
51 * instance.
52 */
53 public ConfigurationMap(Configuration configuration)
54 {
55 this.configuration = configuration;
56 }
57
58 /***
59 * @see java.util.Map#entrySet()
60 */
61 public Set entrySet()
62 {
63 return new ConfigurationSet(configuration);
64 }
65
66 /***
67 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
68 */
69 public Object put(Object key, Object value)
70 {
71 String strKey = String.valueOf(key);
72 Object old = configuration.getProperty(strKey);
73 configuration.setProperty(strKey, value);
74 return old;
75 }
76
77 /***
78 * @see java.util.Map#get(java.lang.Object)
79 */
80 public Object get(Object key)
81 {
82 return configuration.getProperty(String.valueOf(key));
83 }
84
85 static class ConfigurationSet
86 extends AbstractSet
87 {
88 private Configuration configuration = null;
89
90 private class Entry
91 implements Map.Entry
92 {
93 private Object key = null;
94
95 private Entry(Object key)
96 {
97 this.key = key;
98 }
99
100 public Object getKey()
101 {
102 return key;
103 }
104
105 public Object getValue()
106 {
107 return configuration.getProperty((String) key);
108 }
109
110 public Object setValue(Object value)
111 {
112 Object old = getValue();
113 configuration.setProperty((String) key, value);
114 return old;
115 }
116
117 }
118
119 private class ConfigurationSetIterator
120 implements Iterator
121 {
122 private Iterator keys;
123
124 private ConfigurationSetIterator()
125 {
126 keys = configuration.getKeys();
127 }
128
129 public boolean hasNext()
130 {
131 return keys.hasNext();
132 }
133
134 public Object next()
135 {
136 return new Entry(keys.next());
137 }
138
139 public void remove()
140 {
141 keys.remove();
142 }
143
144 }
145
146 ConfigurationSet(Configuration configuration)
147 {
148 this.configuration = configuration;
149 }
150
151 /***
152 * @see java.util.Collection#size()
153 */
154 public int size()
155 {
156
157 int count = 0;
158 for (Iterator iterator = configuration.getKeys(); iterator.hasNext(); )
159 {
160 iterator.next();
161 count++;
162 }
163 return count;
164 }
165
166 /***
167 * @see java.util.Collection#iterator()
168 */
169 public Iterator iterator()
170 {
171 return new ConfigurationSetIterator();
172 }
173 }
174 }