View Javadoc
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.configuration2;
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.Objects;
25  import java.util.Set;
26  
27  /**
28   * <p>
29   * The {@code ConfigurationMap} wraps a configuration-collection {@link org.apache.commons.configuration2.Configuration}
30   * instance to provide a {@code Map} interface.
31   * </p>
32   *
33   * <p>
34   * <em>Note:</em> This implementation is incomplete.
35   * </p>
36   *
37   * @since 1.0
38   */
39  public class ConfigurationMap extends AbstractMap<Object, Object> {
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} that wraps the specified {@code Configuration} instance.
47       *
48       * @param configuration {@code Configuration} instance.
49       */
50      public ConfigurationMap(final Configuration configuration) {
51          this.configuration = Objects.requireNonNull(configuration, "configuration");
52      }
53  
54      /**
55       * Gets the wrapped {@code Configuration} object.
56       *
57       * @return the wrapped configuration
58       * @since 1.2
59       */
60      public Configuration getConfiguration() {
61          return configuration;
62      }
63  
64      /**
65       * Returns a set with the entries contained in this configuration-based map.
66       *
67       * @return a set with the contained entries
68       * @see java.util.Map#entrySet()
69       */
70      @Override
71      public Set<Map.Entry<Object, Object>> entrySet() {
72          return new ConfigurationSet(configuration);
73      }
74  
75      /**
76       * Stores the value for the specified key. The value is stored in the underlying configuration.
77       *
78       * @param key the key (will be converted to a string)
79       * @param value the value
80       * @return the old value of this key or <b>null</b> if it is new
81       * @see java.util.Map#put(Object, Object)
82       */
83      @Override
84      public Object put(final Object key, final Object value) {
85          final String strKey = String.valueOf(key);
86          final Object old = configuration.getProperty(strKey);
87          configuration.setProperty(strKey, value);
88          return old;
89      }
90  
91      /**
92       * Gets the value of the specified key. The key is converted to a string and then passed to the underlying
93       * configuration.
94       *
95       * @param key the key
96       * @return the value of this key
97       * @see java.util.Map#get(Object)
98       */
99      @Override
100     public Object get(final Object key) {
101         return configuration.getProperty(String.valueOf(key));
102     }
103 
104     /**
105      * Sets of entries in the map.
106      */
107     static class ConfigurationSet extends AbstractSet<Map.Entry<Object, Object>> {
108         /** The configuration mapped to this entry set. */
109         private final Configuration configuration;
110 
111         /**
112          * A Map entry in the ConfigurationMap.
113          */
114         private final class Entry implements Map.Entry<Object, Object> {
115             /** The key of the map entry. */
116             private final Object key;
117 
118             private Entry(final Object key) {
119                 this.key = key;
120             }
121 
122             @Override
123             public Object getKey() {
124                 return key;
125             }
126 
127             @Override
128             public Object getValue() {
129                 return configuration.getProperty((String) key);
130             }
131 
132             @Override
133             public Object setValue(final Object value) {
134                 final Object old = getValue();
135                 configuration.setProperty((String) key, value);
136                 return old;
137             }
138         }
139 
140         /**
141          * Iterator over the entries in the ConfigurationMap.
142          */
143         private final class ConfigurationSetIterator implements Iterator<Map.Entry<Object, Object>> {
144             /** An iterator over the keys in the configuration. */
145             private final Iterator<String> keys;
146 
147             private ConfigurationSetIterator() {
148                 keys = configuration.getKeys();
149             }
150 
151             @Override
152             public boolean hasNext() {
153                 return keys.hasNext();
154             }
155 
156             @Override
157             public Map.Entry<Object, Object> next() {
158                 return new Entry(keys.next());
159             }
160 
161             @Override
162             public void remove() {
163                 keys.remove();
164             }
165         }
166 
167         ConfigurationSet(final Configuration configuration) {
168             this.configuration = configuration;
169         }
170 
171         /**
172          * @see java.util.Collection#size()
173          */
174         @Override
175         public int size() {
176             // Ouch. Now _that_ one is expensive...
177             int count = 0;
178             for (final Iterator<String> iterator = configuration.getKeys(); iterator.hasNext();) {
179                 iterator.next();
180                 count++;
181             }
182             return count;
183         }
184 
185         /**
186          * @see java.util.Collection#iterator()
187          */
188         @Override
189         public Iterator<Map.Entry<Object, Object>> iterator() {
190             return new ConfigurationSetIterator();
191         }
192     }
193 }