View Javadoc

1   /*
2    * Copyright 2004-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  /***
25   * A Map based Configuration.
26   *
27   * @author Emmanuel Bourg
28   * @version $Revision$, $Date: 2005-10-09 20:27:12 +0200 (Sun, 09 Oct 2005) $
29   * @since 1.1
30   */
31  public class MapConfiguration extends AbstractConfiguration
32  {
33      /*** The Map decorated by this configuration. */
34      protected Map map;
35  
36      /***
37       * Create a Configuration decorator around the specified Map. The map is
38       * used to store the configuration properties, any change will also affect
39       * the Map.
40       *
41       * @param map the map
42       */
43      public MapConfiguration(Map map)
44      {
45          this.map = map;
46      }
47  
48      /***
49       * Return the Map decorated by this configuration.
50       *
51       * @return the map this configuration is based onto
52       */
53      public Map getMap()
54      {
55          return map;
56      }
57  
58      public Object getProperty(String key)
59      {
60          Object value = map.get(key);
61          if (value instanceof String)
62          {
63              List list = PropertyConverter.split((String) value, getDelimiter());
64              return list.size() > 1 ? list : value;
65          }
66          else
67          {
68              return value;
69          }
70      }
71  
72      protected void addPropertyDirect(String key, Object value)
73      {
74          Object previousValue = getProperty(key);
75  
76          if (previousValue == null)
77          {
78              map.put(key, value);
79          }
80          else if (previousValue instanceof List)
81          {
82              // the value is added to the existing list
83              ((List) previousValue).add(value);
84          }
85          else
86          {
87              // the previous value is replaced by a list containing the previous value and the new value
88              List list = new ArrayList();
89              list.add(previousValue);
90              list.add(value);
91  
92              map.put(key, list);
93          }
94      }
95  
96      public boolean isEmpty()
97      {
98          return map.isEmpty();
99      }
100 
101     public boolean containsKey(String key)
102     {
103         return map.containsKey(key);
104     }
105 
106     public void clearProperty(String key)
107     {
108         map.remove(key);
109     }
110 
111     public Iterator getKeys()
112     {
113         return map.keySet().iterator();
114     }
115 }