/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.configuration;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

The ConfigurationMap wraps a configuration-collection Configuration instance to provide a Map interface.

Note: This implementation is incomplete.

Author:Ricardo Gladwell
Version:$Id: ConfigurationMap.java 1301959 2012-03-17 16:43:18Z oheger $
Since:1.0
/** * <p>The {@code ConfigurationMap} wraps a * configuration-collection * {@link org.apache.commons.configuration.Configuration} * instance to provide a {@code Map} interface.</p> * * <p><em>Note:</em> This implementation is incomplete.</p> * * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a> * @version $Id: ConfigurationMap.java 1301959 2012-03-17 16:43:18Z oheger $ * @since 1.0 */
public class ConfigurationMap extends AbstractMap<Object, Object> {
The Configuration wrapped by this class.
/** * The {@code Configuration} wrapped by this class. */
private final Configuration configuration;
Creates a new instance of a ConfigurationMap that wraps the specified Configuration instance.
Params:
  • configuration – Configuration instance.
/** * Creates a new instance of a {@code ConfigurationMap} * that wraps the specified {@code Configuration} * instance. * @param configuration {@code Configuration} * instance. */
public ConfigurationMap(Configuration configuration) { this.configuration = configuration; }
Returns the wrapped Configuration object.
Returns:the wrapped configuration
Since:1.2
/** * Returns the wrapped {@code Configuration} object. * * @return the wrapped configuration * @since 1.2 */
public Configuration getConfiguration() { return configuration; }
Returns a set with the entries contained in this configuration-based map.
See Also:
Returns:a set with the contained entries
/** * Returns a set with the entries contained in this configuration-based map. * * @return a set with the contained entries * @see java.util.Map#entrySet() */
@Override public Set<Map.Entry<Object, Object>> entrySet() { return new ConfigurationSet(configuration); }
Stores the value for the specified key. The value is stored in the underlying configuration.
Params:
  • key – the key (will be converted to a string)
  • value – the value
See Also:
Returns:the old value of this key or null if it is new
/** * Stores the value for the specified key. The value is stored in the * underlying configuration. * * @param key the key (will be converted to a string) * @param value the value * @return the old value of this key or <b>null</b> if it is new * @see java.util.Map#put(java.lang.Object, java.lang.Object) */
@Override public Object put(Object key, Object value) { String strKey = String.valueOf(key); Object old = configuration.getProperty(strKey); configuration.setProperty(strKey, value); return old; }
Returns the value of the specified key. The key is converted to a string and then passed to the underlying configuration.
Params:
  • key – the key
See Also:
Returns:the value of this key
/** * Returns the value of the specified key. The key is converted to a string * and then passed to the underlying configuration. * * @param key the key * @return the value of this key * @see java.util.Map#get(java.lang.Object) */
@Override public Object get(Object key) { return configuration.getProperty(String.valueOf(key)); }
Set of entries in the map.
/** * Set of entries in the map. */
static class ConfigurationSet extends AbstractSet<Map.Entry<Object, Object>> {
The configuration mapped to this entry set.
/** The configuration mapped to this entry set. */
private final Configuration configuration;
A Map entry in the ConfigurationMap.
/** * A Map entry in the ConfigurationMap. */
private final class Entry implements Map.Entry<Object, Object> {
The key of the map entry.
/** The key of the map entry. */
private Object key; private Entry(Object key) { this.key = key; } public Object getKey() { return key; } public Object getValue() { return configuration.getProperty((String) key); } public Object setValue(Object value) { Object old = getValue(); configuration.setProperty((String) key, value); return old; } }
Iterator over the entries in the ConfigurationMap.
/** * Iterator over the entries in the ConfigurationMap. */
private final class ConfigurationSetIterator implements Iterator<Map.Entry<Object, Object>> {
An iterator over the keys in the configuration.
/** An iterator over the keys in the configuration. */
private final Iterator<String> keys; private ConfigurationSetIterator() { keys = configuration.getKeys(); } public boolean hasNext() { return keys.hasNext(); } public Map.Entry<Object, Object> next() { return new Entry(keys.next()); } public void remove() { keys.remove(); } } ConfigurationSet(Configuration configuration) { this.configuration = configuration; }
See Also:
  • size.size()
/** * @see java.util.Collection#size() */
@Override public int size() { // Ouch. Now _that_ one is expensive... int count = 0; for (Iterator<String> iterator = configuration.getKeys(); iterator.hasNext();) { iterator.next(); count++; } return count; }
See Also:
  • iterator.iterator()
/** * @see java.util.Collection#iterator() */
@Override public Iterator<Map.Entry<Object, Object>> iterator() { return new ConfigurationSetIterator(); } } }