    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        /**
    041         * The <code>Configuration</code> wrapped by this class.
    042         */
    043        private Configuration configuration;
    044
    045        /**
    046         * Creates a new instance of a <code>ConfigurationMap</code>
    047         * that wraps the specified <code>Configuration</code>
    048         * instance.
    049         * @param configuration <code>Configuration</code>
    050         * instance.
    051         */
    052        public ConfigurationMap(Configuration configuration)
    053        {
    054            this.configuration = configuration;
    055        }
    056
    057        /**
    058         * Returns the wrapped <code>Configuration</code> object.
    059         *
    060         * @return the wrapped configuration
    061         * @since 1.2
    062         */
    063        public Configuration getConfiguration()
    064        {
    065            return configuration;
    066        }
    067
    068        /**
    069         * Returns a set with the entries contained in this configuration-based map.
    070         *
    071         * @return a set with the contained entries
    072         * @see java.util.Map#entrySet()
    073         */
    074        public Set entrySet()
    075        {
    076            return new ConfigurationSet(configuration);
    077        }
    078
    079        /**
    080         * Stores the value for the specified key. The value is stored in the
    081         * underlying configuration.
    082         *
    083         * @param key the key (will be converted to a string)
    084         * @param value the value
    085         * @return the old value of this key or <b>null</b> if it is new
    086         * @see java.util.Map#put(java.lang.Object, java.lang.Object)
    087         */
    088        public Object put(Object key, Object value)
    089        {
    090            String strKey = String.valueOf(key);
    091            Object old = configuration.getProperty(strKey);
    092            configuration.setProperty(strKey, value);
    093            return old;
    094        }
    095
    096        /**
    097         * Returns the value of the specified key. The key is converted to a string
    098         * and then passed to the underlying configuration.
    099         *
    100         * @param key the key
    101         * @return the value of this key
    102         * @see java.util.Map#get(java.lang.Object)
    103         */
    104        public Object get(Object key)
    105        {
    106            return configuration.getProperty(String.valueOf(key));
    107        }
    108
    109        /**
    110         * Set of entries in the map.
    111         */
    112        static class ConfigurationSet extends AbstractSet
    113        {
    114            /** The configuration mapped to this entry set. */
    115            private Configuration configuration;
    116
    117            /**
    118             * A Map entry in the ConfigurationMap.
    119             */
    120            private final class Entry implements Map.Entry
    121            {
    122                /** The key of the map entry. */
    123                private Object key;
    124
    125                private Entry(Object key)
    126                {
    127                    this.key = key;
    128                }
    129
    130                public Object getKey()
    131                {
    132                    return key;
    133                }
    134
    135                public Object getValue()
    136                {
    137                    return configuration.getProperty((String) key);
    138                }
    139
    140                public Object setValue(Object value)
    141                {
    142                    Object old = getValue();
    143                    configuration.setProperty((String) key, value);
    144                    return old;
    145                }
    146            }
    147
    148            /**
    149             * Iterator over the entries in the ConfigurationMap.
    150             */
    151            private final class ConfigurationSetIterator implements Iterator
    152            {
    153                /** An iterator over the keys in the configuration. */
    154                private Iterator keys;
    155
    156                private ConfigurationSetIterator()
    157                {
    158                    keys = configuration.getKeys();
    159                }
    160
    161                public boolean hasNext()
    162                {
    163                    return keys.hasNext();
    164                }
    165
    166                public Object next()
    167                {
    168                    return new Entry(keys.next());
    169                }
    170
    171                public void remove()
    172                {
    173                    keys.remove();
    174                }
    175            }
    176
    177            ConfigurationSet(Configuration configuration)
    178            {
    179                this.configuration = configuration;
    180            }
    181
    182            /**
    183             * @see java.util.Collection#size()
    184             */
    185            public int size()
    186            {
    187                // Ouch. Now _that_ one is expensive...
    188                int count = 0;
    189                for (Iterator iterator = configuration.getKeys(); iterator.hasNext();)
    190                {
    191                    iterator.next();
    192                    count++;
    193                }
    194                return count;
    195            }
    196
    197            /**
    198             * @see java.util.Collection#iterator()
    199             */
    200            public Iterator iterator()
    201            {
    202                return new ConfigurationSetIterator();
    203            }
    204        }
    205    }