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 * @todo This implementation is incomplete.
32 *
33 * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
34 * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 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 Configuration configuration = null;
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 * @see java.util.Map#entrySet()
58 */
59 public Set entrySet()
60 {
61 return new ConfigurationSet(configuration);
62 }
63
64 /***
65 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
66 */
67 public Object put(Object key, Object value)
68 {
69 String strKey = String.valueOf(key);
70 Object old = configuration.getProperty(strKey);
71 configuration.setProperty(strKey, value);
72 return old;
73 }
74
75 /***
76 * @see java.util.Map#get(java.lang.Object)
77 */
78 public Object get(Object key)
79 {
80 return configuration.getProperty(String.valueOf(key));
81 }
82
83 static class ConfigurationSet extends AbstractSet
84 {
85 private Configuration configuration = null;
86
87 private class Entry implements Map.Entry
88 {
89 private Object key = null;
90
91 private Entry(Object key)
92 {
93 this.key = key;
94 }
95
96 public Object getKey()
97 {
98 return key;
99 }
100
101 public Object getValue()
102 {
103 return configuration.getProperty((String) key);
104 }
105
106 public Object setValue(Object value)
107 {
108 Object old = getValue();
109 configuration.setProperty((String) key, value);
110 return old;
111 }
112
113 }
114
115 private class ConfigurationSetIterator implements Iterator
116 {
117 private Iterator keys;
118
119 private ConfigurationSetIterator()
120 {
121 keys = configuration.getKeys();
122 }
123
124 public boolean hasNext()
125 {
126 return keys.hasNext();
127 }
128
129 public Object next()
130 {
131 return new Entry(keys.next());
132 }
133
134 public void remove()
135 {
136 keys.remove();
137 }
138
139 }
140
141 ConfigurationSet(Configuration configuration)
142 {
143 this.configuration = configuration;
144 }
145
146 /***
147 * @see java.util.Collection#size()
148 */
149 public int size()
150 {
151
152 int count = 0;
153 for (Iterator iterator = configuration.getKeys(); iterator.hasNext(); )
154 {
155 iterator.next();
156 count++;
157 }
158 return count;
159 }
160
161 /***
162 * @see java.util.Collection#iterator()
163 */
164 public Iterator iterator()
165 {
166 return new ConfigurationSetIterator();
167 }
168 }
169 }