View Javadoc

1   /*
2    * Copyright 2001-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  import org.apache.commons.collections.map.LinkedMap;
25  
26  /***
27   * Basic configuration classe. Stores the configuration data but does not
28   * provide any load or save functions. If you want to load your Configuration
29   * from a file use PropertiesConfiguration or XmlConfiguration.
30   *
31   * This class extends normal Java properties by adding the possibility
32   * to use the same key many times concatenating the value strings
33   * instead of overwriting them.
34   *
35   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
36   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
37   * @author <a href="mailto:daveb@miceda-data">Dave Bryson</a>
38   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
39   * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
40   * @author <a href="mailto:kjohnson@transparent.com">Kent Johnson</a>
41   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
42   * @author <a href="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
43   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
44   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
45   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46   * @author <a href="mailto:ksh@scand.com">Konstantin Shaposhnikov</a>
47   * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
48   * @version $Id: BaseConfiguration.java 239602 2005-08-24 11:15:27Z ebourg $
49   */
50  public class BaseConfiguration extends AbstractConfiguration
51  {
52      /*** stores the configuration key-value pairs */
53      private Map store = new LinkedMap();
54  
55      /***
56       * Adds a key/value pair to the map.  This routine does no magic morphing.
57       * It ensures the keylist is maintained
58       *
59       * @param key key to use for mapping
60       * @param value object to store
61       */
62      protected void addPropertyDirect(String key, Object value)
63      {
64          Object previousValue = getProperty(key);
65  
66          if (previousValue == null)
67          {
68              store.put(key, value);
69          }
70          else if (previousValue instanceof List)
71          {
72              // the value is added to the existing list
73              ((List) previousValue).add(value);
74          }
75          else
76          {
77              // the previous value is replaced by a list containing the previous value and the new value
78              List list = new ArrayList();
79              list.add(previousValue);
80              list.add(value);
81  
82              store.put(key, list);
83          }
84      }
85  
86      /***
87       * Read property from underlying map.
88       *
89       * @param key key to use for mapping
90       *
91       * @return object associated with the given configuration key.
92       */
93      public Object getProperty(String key)
94      {
95          return store.get(key);
96      }
97  
98      /***
99       * Check if the configuration is empty
100      *
101      * @return <code>true</code> if Configuration is empty,
102      * <code>false</code> otherwise.
103      */
104     public boolean isEmpty()
105     {
106         return store.isEmpty();
107     }
108 
109     /***
110      * check if the configuration contains the key
111      *
112      * @param key the configuration key
113      *
114      * @return <code>true</code> if Configuration contain given key,
115      * <code>false</code> otherwise.
116      */
117     public boolean containsKey(String key)
118     {
119         return store.containsKey(key);
120     }
121 
122     /***
123      * Clear a property in the configuration.
124      *
125      * @param key the key to remove along with corresponding value.
126      */
127     public void clearProperty(String key)
128     {
129         if (containsKey(key))
130         {
131             store.remove(key);
132         }
133     }
134 
135     /***
136      * {@inheritDoc}
137      */
138     public void clear()
139     {
140         store.clear();
141     }
142 
143     /***
144      * Get the list of the keys contained in the configuration
145      * repository.
146      *
147      * @return An Iterator.
148      */
149     public Iterator getKeys()
150     {
151         return store.keySet().iterator();
152     }
153 }