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.configuration;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  /***
26   * <p>A Map based Configuration.</p>
27   * <p><em>Note:</em>Configuration objects of this type can be read concurrently
28   * by multiple threads. However if one of these threads modifies the object,
29   * synchronization has to be performed manually.</p>
30   *
31   * @author Emmanuel Bourg
32   * @version $Revision: 548098 $, $Date: 2007-06-17 21:34:03 +0200 (So, 17 Jun 2007) $
33   * @since 1.1
34   */
35  public class MapConfiguration extends AbstractConfiguration implements Cloneable
36  {
37      /*** The Map decorated by this configuration. */
38      protected Map map;
39  
40      /***
41       * Create a Configuration decorator around the specified Map. The map is
42       * used to store the configuration properties, any change will also affect
43       * the Map.
44       *
45       * @param map the map
46       */
47      public MapConfiguration(Map map)
48      {
49          this.map = map;
50      }
51  
52      /***
53       * Return the Map decorated by this configuration.
54       *
55       * @return the map this configuration is based onto
56       */
57      public Map getMap()
58      {
59          return map;
60      }
61  
62      public Object getProperty(String key)
63      {
64          Object value = map.get(key);
65          if ((value instanceof String) && (!isDelimiterParsingDisabled()))
66          {
67              List list = PropertyConverter.split((String) value, getListDelimiter());
68              return list.size() > 1 ? list : list.get(0);
69          }
70          else
71          {
72              return value;
73          }
74      }
75  
76      protected void addPropertyDirect(String key, Object value)
77      {
78          Object previousValue = getProperty(key);
79  
80          if (previousValue == null)
81          {
82              map.put(key, value);
83          }
84          else if (previousValue instanceof List)
85          {
86              // the value is added to the existing list
87              ((List) previousValue).add(value);
88          }
89          else
90          {
91              // the previous value is replaced by a list containing the previous value and the new value
92              List list = new ArrayList();
93              list.add(previousValue);
94              list.add(value);
95  
96              map.put(key, list);
97          }
98      }
99  
100     public boolean isEmpty()
101     {
102         return map.isEmpty();
103     }
104 
105     public boolean containsKey(String key)
106     {
107         return map.containsKey(key);
108     }
109 
110     protected void clearPropertyDirect(String key)
111     {
112         map.remove(key);
113     }
114 
115     public Iterator getKeys()
116     {
117         return map.keySet().iterator();
118     }
119 
120     /***
121      * Returns a copy of this object. The returned configuration will contain
122      * the same properties as the original. Event listeners are not cloned.
123      *
124      * @return the copy
125      * @since 1.3
126      */
127     public Object clone()
128     {
129         try
130         {
131             MapConfiguration copy = (MapConfiguration) super.clone();
132             copy.clearConfigurationListeners();
133             copy.map = (Map) ConfigurationUtils.clone(map);
134             return copy;
135         }
136         catch (CloneNotSupportedException cex)
137         {
138             // cannot happen
139             throw new ConfigurationRuntimeException(cex);
140         }
141     }
142 }