ConfigurationMap.java

  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. package org.apache.commons.configuration2;

  18. import java.util.AbstractMap;
  19. import java.util.AbstractSet;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.Objects;
  23. import java.util.Set;

  24. /**
  25.  * <p>
  26.  * The {@code ConfigurationMap} wraps a configuration-collection {@link org.apache.commons.configuration2.Configuration}
  27.  * instance to provide a {@code Map} interface.
  28.  * </p>
  29.  *
  30.  * <p>
  31.  * <em>Note:</em> This implementation is incomplete.
  32.  * </p>
  33.  *
  34.  * @since 1.0
  35.  */
  36. public class ConfigurationMap extends AbstractMap<Object, Object> {
  37.     /**
  38.      * Sets of entries in the map.
  39.      */
  40.     static class ConfigurationSet extends AbstractSet<Map.Entry<Object, Object>> {
  41.         /**
  42.          * Iterator over the entries in the ConfigurationMap.
  43.          */
  44.         private final class ConfigurationSetIterator implements Iterator<Map.Entry<Object, Object>> {
  45.             /** An iterator over the keys in the configuration. */
  46.             private final Iterator<String> keys;

  47.             private ConfigurationSetIterator() {
  48.                 keys = configuration.getKeys();
  49.             }

  50.             @Override
  51.             public boolean hasNext() {
  52.                 return keys.hasNext();
  53.             }

  54.             @Override
  55.             public Map.Entry<Object, Object> next() {
  56.                 return new Entry(keys.next());
  57.             }

  58.             @Override
  59.             public void remove() {
  60.                 keys.remove();
  61.             }
  62.         }

  63.         /**
  64.          * A Map entry in the ConfigurationMap.
  65.          */
  66.         private final class Entry implements Map.Entry<Object, Object> {
  67.             /** The key of the map entry. */
  68.             private final Object key;

  69.             private Entry(final Object key) {
  70.                 this.key = key;
  71.             }

  72.             @Override
  73.             public Object getKey() {
  74.                 return key;
  75.             }

  76.             @Override
  77.             public Object getValue() {
  78.                 return configuration.getProperty((String) key);
  79.             }

  80.             @Override
  81.             public Object setValue(final Object value) {
  82.                 final Object old = getValue();
  83.                 configuration.setProperty((String) key, value);
  84.                 return old;
  85.             }
  86.         }

  87.         /** The configuration mapped to this entry set. */
  88.         private final Configuration configuration;

  89.         ConfigurationSet(final Configuration configuration) {
  90.             this.configuration = configuration;
  91.         }

  92.         /**
  93.          * @see java.util.Collection#iterator()
  94.          */
  95.         @Override
  96.         public Iterator<Map.Entry<Object, Object>> iterator() {
  97.             return new ConfigurationSetIterator();
  98.         }

  99.         /**
  100.          * @see java.util.Collection#size()
  101.          */
  102.         @Override
  103.         public int size() {
  104.             // Ouch. Now _that_ one is expensive...
  105.             int count = 0;
  106.             for (final Iterator<String> iterator = configuration.getKeys(); iterator.hasNext();) {
  107.                 iterator.next();
  108.                 count++;
  109.             }
  110.             return count;
  111.         }
  112.     }

  113.     /**
  114.      * The {@code Configuration} wrapped by this class.
  115.      */
  116.     private final Configuration configuration;

  117.     /**
  118.      * Creates a new instance of a {@code ConfigurationMap} that wraps the specified {@code Configuration} instance.
  119.      *
  120.      * @param configuration {@code Configuration} instance.
  121.      */
  122.     public ConfigurationMap(final Configuration configuration) {
  123.         this.configuration = Objects.requireNonNull(configuration, "configuration");
  124.     }

  125.     /**
  126.      * Returns a set with the entries contained in this configuration-based map.
  127.      *
  128.      * @return a set with the contained entries
  129.      * @see Map#entrySet()
  130.      */
  131.     @Override
  132.     public Set<Map.Entry<Object, Object>> entrySet() {
  133.         return new ConfigurationSet(configuration);
  134.     }

  135.     /**
  136.      * Gets the value of the specified key. The key is converted to a string and then passed to the underlying
  137.      * configuration.
  138.      *
  139.      * @param key the key
  140.      * @return the value of this key
  141.      * @see Map#get(Object)
  142.      */
  143.     @Override
  144.     public Object get(final Object key) {
  145.         return configuration.getProperty(String.valueOf(key));
  146.     }

  147.     /**
  148.      * Gets the wrapped {@code Configuration} object.
  149.      *
  150.      * @return the wrapped configuration
  151.      * @since 1.2
  152.      */
  153.     public Configuration getConfiguration() {
  154.         return configuration;
  155.     }

  156.     /**
  157.      * Stores the value for the specified key. The value is stored in the underlying configuration.
  158.      *
  159.      * @param key the key (will be converted to a string)
  160.      * @param value the value
  161.      * @return the old value of this key or <strong>null</strong> if it is new
  162.      * @see Map#put(Object, Object)
  163.      */
  164.     @Override
  165.     public Object put(final Object key, final Object value) {
  166.         final String strKey = String.valueOf(key);
  167.         final Object old = configuration.getProperty(strKey);
  168.         configuration.setProperty(strKey, value);
  169.         return old;
  170.     }
  171. }