Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConfigurationMap |
|
| 1.0625;1,062 | ||||
ConfigurationMap$1 |
|
| 1.0625;1,062 | ||||
ConfigurationMap$ConfigurationSet |
|
| 1.0625;1,062 | ||||
ConfigurationMap$ConfigurationSet$ConfigurationSetIterator |
|
| 1.0625;1,062 | ||||
ConfigurationMap$ConfigurationSet$Entry |
|
| 1.0625;1,062 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
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} wraps a | |
28 | * configuration-collection | |
29 | * {@link org.apache.commons.configuration.Configuration} | |
30 | * instance to provide a {@code Map} 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 $Id: ConfigurationMap.java 1301959 2012-03-17 16:43:18Z oheger $ | |
36 | * @since 1.0 | |
37 | */ | |
38 | public class ConfigurationMap extends AbstractMap<Object, Object> | |
39 | { | |
40 | /** | |
41 | * The {@code Configuration} wrapped by this class. | |
42 | */ | |
43 | private final Configuration configuration; | |
44 | ||
45 | /** | |
46 | * Creates a new instance of a {@code ConfigurationMap} | |
47 | * that wraps the specified {@code Configuration} | |
48 | * instance. | |
49 | * @param configuration {@code Configuration} | |
50 | * instance. | |
51 | */ | |
52 | public ConfigurationMap(Configuration configuration) | |
53 | 92 | { |
54 | 92 | this.configuration = configuration; |
55 | 92 | } |
56 | ||
57 | /** | |
58 | * Returns the wrapped {@code Configuration} object. | |
59 | * | |
60 | * @return the wrapped configuration | |
61 | * @since 1.2 | |
62 | */ | |
63 | public Configuration getConfiguration() | |
64 | { | |
65 | 4504 | 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 | @Override | |
75 | public Set<Map.Entry<Object, Object>> entrySet() | |
76 | { | |
77 | 2 | return new ConfigurationSet(configuration); |
78 | } | |
79 | ||
80 | /** | |
81 | * Stores the value for the specified key. The value is stored in the | |
82 | * underlying configuration. | |
83 | * | |
84 | * @param key the key (will be converted to a string) | |
85 | * @param value the value | |
86 | * @return the old value of this key or <b>null</b> if it is new | |
87 | * @see java.util.Map#put(java.lang.Object, java.lang.Object) | |
88 | */ | |
89 | @Override | |
90 | public Object put(Object key, Object value) | |
91 | { | |
92 | 7 | String strKey = String.valueOf(key); |
93 | 7 | Object old = configuration.getProperty(strKey); |
94 | 7 | configuration.setProperty(strKey, value); |
95 | 7 | return old; |
96 | } | |
97 | ||
98 | /** | |
99 | * Returns the value of the specified key. The key is converted to a string | |
100 | * and then passed to the underlying configuration. | |
101 | * | |
102 | * @param key the key | |
103 | * @return the value of this key | |
104 | * @see java.util.Map#get(java.lang.Object) | |
105 | */ | |
106 | @Override | |
107 | public Object get(Object key) | |
108 | { | |
109 | 8 | return configuration.getProperty(String.valueOf(key)); |
110 | } | |
111 | ||
112 | /** | |
113 | * Set of entries in the map. | |
114 | */ | |
115 | 10 | static class ConfigurationSet extends AbstractSet<Map.Entry<Object, Object>> |
116 | { | |
117 | /** The configuration mapped to this entry set. */ | |
118 | private final Configuration configuration; | |
119 | ||
120 | /** | |
121 | * A Map entry in the ConfigurationMap. | |
122 | */ | |
123 | 11 | private final class Entry implements Map.Entry<Object, Object> |
124 | { | |
125 | /** The key of the map entry. */ | |
126 | private Object key; | |
127 | ||
128 | private Entry(Object key) | |
129 | 11 | { |
130 | 11 | this.key = key; |
131 | 11 | } |
132 | ||
133 | public Object getKey() | |
134 | { | |
135 | 60 | return key; |
136 | } | |
137 | ||
138 | public Object getValue() | |
139 | { | |
140 | 7 | return configuration.getProperty((String) key); |
141 | } | |
142 | ||
143 | public Object setValue(Object value) | |
144 | { | |
145 | 0 | Object old = getValue(); |
146 | 0 | configuration.setProperty((String) key, value); |
147 | 0 | return old; |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * Iterator over the entries in the ConfigurationMap. | |
153 | */ | |
154 | 14 | private final class ConfigurationSetIterator implements Iterator<Map.Entry<Object, Object>> |
155 | { | |
156 | /** An iterator over the keys in the configuration. */ | |
157 | private final Iterator<String> keys; | |
158 | ||
159 | private ConfigurationSetIterator() | |
160 | 3 | { |
161 | 3 | keys = configuration.getKeys(); |
162 | 3 | } |
163 | ||
164 | public boolean hasNext() | |
165 | { | |
166 | 15 | return keys.hasNext(); |
167 | } | |
168 | ||
169 | public Map.Entry<Object, Object> next() | |
170 | { | |
171 | 11 | return new Entry(keys.next()); |
172 | } | |
173 | ||
174 | public void remove() | |
175 | { | |
176 | 7 | keys.remove(); |
177 | 7 | } |
178 | } | |
179 | ||
180 | ConfigurationSet(Configuration configuration) | |
181 | 4 | { |
182 | 4 | this.configuration = configuration; |
183 | 4 | } |
184 | ||
185 | /** | |
186 | * @see java.util.Collection#size() | |
187 | */ | |
188 | @Override | |
189 | public int size() | |
190 | { | |
191 | // Ouch. Now _that_ one is expensive... | |
192 | 2 | int count = 0; |
193 | 2 | for (Iterator<String> iterator = configuration.getKeys(); iterator.hasNext();) |
194 | { | |
195 | 7 | iterator.next(); |
196 | 7 | count++; |
197 | } | |
198 | 2 | return count; |
199 | } | |
200 | ||
201 | /** | |
202 | * @see java.util.Collection#iterator() | |
203 | */ | |
204 | @Override | |
205 | public Iterator<Map.Entry<Object, Object>> iterator() | |
206 | { | |
207 | 3 | return new ConfigurationSetIterator(); |
208 | } | |
209 | } | |
210 | } |