001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.configuration;
019:
020: import java.util.AbstractMap;
021: import java.util.AbstractSet;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: /**
027: * <p>The <code>ConfigurationMap</code> wraps a
028: * configuration-collection
029: * {@link org.apache.commons.configuration.Configuration}
030: * instance to provide a <code>Map</code> interface.</p>
031: *
032: * <p><em>Note:</em> This implementation is incomplete.</p>
033: *
034: * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
035: * @version $Revision: 492216 $, $Date: 2007-01-03 17:51:24 +0100 (Mi, 03 Jan 2007) $
036: * @since 1.0
037: */
038: public class ConfigurationMap extends AbstractMap {
039: /**
040: * The <code>Configuration</code> wrapped by this class.
041: */
042: private Configuration configuration;
043:
044: /**
045: * Creates a new instance of a <code>ConfigurationMap</code>
046: * that wraps the specified <code>Configuration</code>
047: * instance.
048: * @param configuration <code>Configuration</code>
049: * instance.
050: */
051: public ConfigurationMap(Configuration configuration) {
052: this .configuration = configuration;
053: }
054:
055: /**
056: * Returns the wrapped <code>Configuration</code> object.
057: *
058: * @return the wrapped configuration
059: * @since 1.2
060: */
061: public Configuration getConfiguration() {
062: return configuration;
063: }
064:
065: /**
066: * Returns a set with the entries contained in this configuration-based map.
067: *
068: * @return a set with the contained entries
069: * @see java.util.Map#entrySet()
070: */
071: public Set entrySet() {
072: return new ConfigurationSet(configuration);
073: }
074:
075: /**
076: * Stores the value for the specified key. The value is stored in the
077: * underlying configuration.
078: *
079: * @param key the key (will be converted to a string)
080: * @param value the value
081: * @return the old value of this key or <b>null</b> if it is new
082: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
083: */
084: public Object put(Object key, Object value) {
085: String strKey = String.valueOf(key);
086: Object old = configuration.getProperty(strKey);
087: configuration.setProperty(strKey, value);
088: return old;
089: }
090:
091: /**
092: * Returns the value of the specified key. The key is converted to a string
093: * and then passed to the underlying configuration.
094: *
095: * @param key the key
096: * @return the value of this key
097: * @see java.util.Map#get(java.lang.Object)
098: */
099: public Object get(Object key) {
100: return configuration.getProperty(String.valueOf(key));
101: }
102:
103: /**
104: * Set of entries in the map.
105: */
106: static class ConfigurationSet extends AbstractSet {
107: /** The configuration mapped to this entry set. */
108: private Configuration configuration;
109:
110: /**
111: * A Map entry in the ConfigurationMap.
112: */
113: private final class Entry implements Map.Entry {
114: /** The key of the map entry. */
115: private Object key;
116:
117: private Entry(Object key) {
118: this .key = key;
119: }
120:
121: public Object getKey() {
122: return key;
123: }
124:
125: public Object getValue() {
126: return configuration.getProperty((String) key);
127: }
128:
129: public Object setValue(Object value) {
130: Object old = getValue();
131: configuration.setProperty((String) key, value);
132: return old;
133: }
134: }
135:
136: /**
137: * Iterator over the entries in the ConfigurationMap.
138: */
139: private final class ConfigurationSetIterator implements
140: Iterator {
141: /** An iterator over the keys in the configuration. */
142: private Iterator keys;
143:
144: private ConfigurationSetIterator() {
145: keys = configuration.getKeys();
146: }
147:
148: public boolean hasNext() {
149: return keys.hasNext();
150: }
151:
152: public Object next() {
153: return new Entry(keys.next());
154: }
155:
156: public void remove() {
157: keys.remove();
158: }
159: }
160:
161: ConfigurationSet(Configuration configuration) {
162: this .configuration = configuration;
163: }
164:
165: /**
166: * @see java.util.Collection#size()
167: */
168: public int size() {
169: // Ouch. Now _that_ one is expensive...
170: int count = 0;
171: for (Iterator iterator = configuration.getKeys(); iterator
172: .hasNext();) {
173: iterator.next();
174: count++;
175: }
176: return count;
177: }
178:
179: /**
180: * @see java.util.Collection#iterator()
181: */
182: public Iterator iterator() {
183: return new ConfigurationSetIterator();
184: }
185: }
186: }
|