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    *     https://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.ArrayList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
28  
29  /**
30   * Basic configuration class. Stores the configuration data but does not provide any load or save functions. If you want
31   * to load your Configuration from a file use PropertiesConfiguration or XmlConfiguration.
32   *
33   * This class extends normal Java properties by adding the possibility to use the same key many times concatenating the
34   * value strings instead of overwriting them.
35   */
36  public class BaseConfiguration extends AbstractConfiguration implements Cloneable {
37  
38      /**
39       * Stores the configuration key-value pairs.
40       */
41      private Map<String, Object> store = new LinkedHashMap<>();
42  
43      /**
44       * Constructs a new instance.
45       */
46      public BaseConfiguration() {
47          // empty
48      }
49  
50      /**
51       * Adds a key/value pair to the map. This routine does no magic morphing. It ensures the keylist is maintained
52       *
53       * @param key key to use for mapping
54       * @param value object to store
55       */
56      @Override
57      protected void addPropertyDirect(final String key, final Object value) {
58          final Object previousValue = getPropertyInternal(key);
59          if (previousValue == null) {
60              store.put(key, value);
61          } else if (previousValue instanceof List) {
62              // safe to case because we have created the lists ourselves
63              @SuppressWarnings("unchecked")
64              final List<Object> valueList = (List<Object>) previousValue;
65              // the value is added to the existing list
66              valueList.add(value);
67          } else {
68              // the previous value is replaced by a list containing the previous value and the new value
69              final List<Object> list = new ArrayList<>();
70              list.add(previousValue);
71              list.add(value);
72              store.put(key, list);
73          }
74      }
75  
76      @Override
77      protected void clearInternal() {
78          store.clear();
79      }
80  
81      /**
82       * Clear a property in the configuration.
83       *
84       * @param key the key to remove along with corresponding value.
85       */
86      @Override
87      protected void clearPropertyDirect(final String key) {
88          store.remove(key);
89      }
90  
91      /**
92       * Creates a copy of this object. This implementation will create a deep clone, i.e. the map that stores the properties
93       * is cloned, too. So changes performed at the copy won't affect the original and vice versa.
94       *
95       * @return the copy
96       * @since 1.3
97       */
98      @Override
99      public Object clone() {
100         try {
101             final BaseConfiguration copy = (BaseConfiguration) super.clone();
102             cloneStore(copy);
103             copy.cloneInterpolator(this);
104             return copy;
105         } catch (final CloneNotSupportedException cex) {
106             // should not happen
107             throw new ConfigurationRuntimeException(cex);
108         }
109     }
110 
111     /**
112      * Clones the internal map with the data of this configuration.
113      *
114      * @param copy the copy created by the {@code clone()} method
115      * @throws CloneNotSupportedException if the map cannot be cloned
116      */
117     private void cloneStore(final BaseConfiguration copy) throws CloneNotSupportedException {
118         // This is safe because the type of the map is known
119         copy.store = ConfigurationUtils.clone(store);
120         // Handle collections in the map; they have to be cloned, too
121         store.forEach((k, v) -> {
122             if (v instanceof Collection) {
123                 // This is safe because the collections were created by ourselves
124                 @SuppressWarnings("unchecked")
125                 final Collection<String> strList = (Collection<String>) v;
126                 copy.store.put(k, new ArrayList<>(strList));
127             }
128         });
129     }
130 
131     /**
132      * check if the configuration contains the key
133      *
134      * @param key the configuration key
135      * @return {@code true} if Configuration contain given key, {@code false} otherwise.
136      */
137     @Override
138     protected boolean containsKeyInternal(final String key) {
139         return store.containsKey(key);
140     }
141 
142     /**
143      * Tests whether this configuration contains one or more matches to this value. This operation stops at first
144      * match but may be more expensive than the containsKey method.
145      * @since 2.11.0
146      */
147     @Override
148     protected boolean containsValueInternal(final Object value) {
149         return store.containsValue(value);
150     }
151 
152     /**
153      * Gets the list of the keys contained in the configuration repository.
154      *
155      * @return An Iterator.
156      */
157     @Override
158     protected Iterator<String> getKeysInternal() {
159         return store.keySet().iterator();
160     }
161 
162     /**
163      * Reads property from underlying map.
164      *
165      * @param key key to use for mapping
166      * @return object associated with the given configuration key.
167      */
168     @Override
169     protected Object getPropertyInternal(final String key) {
170         return store.get(key);
171     }
172 
173     /**
174      * Check if the configuration is empty
175      *
176      * @return {@code true} if Configuration is empty, {@code false} otherwise.
177      */
178     @Override
179     protected boolean isEmptyInternal() {
180         return store.isEmpty();
181     }
182 
183     /**
184      * {@inheritDoc} This implementation obtains the size directly from the map used as data store. So this is a rather
185      * efficient implementation.
186      */
187     @Override
188     protected int sizeInternal() {
189         return store.size();
190     }
191 }