Coverage Report - org.apache.commons.configuration.MapConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
MapConfiguration
97%
28/29
100%
4/4
1,778
 
 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  
  * A Map based Configuration.
 27  
  *
 28  
  * @author Emmanuel Bourg
 29  
  * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
 30  
  * @since 1.1
 31  
  */
 32  
 public class MapConfiguration extends AbstractConfiguration implements Cloneable
 33  
 {
 34  
     /** The Map decorated by this configuration. */
 35  
     protected Map map;
 36  
 
 37  
     /**
 38  
      * Create a Configuration decorator around the specified Map. The map is
 39  
      * used to store the configuration properties, any change will also affect
 40  
      * the Map.
 41  
      *
 42  
      * @param map the map
 43  
      */
 44  
     public MapConfiguration(Map map)
 45  29
     {
 46  29
         this.map = map;
 47  29
     }
 48  
 
 49  
     /**
 50  
      * Return the Map decorated by this configuration.
 51  
      *
 52  
      * @return the map this configuration is based onto
 53  
      */
 54  
     public Map getMap()
 55  
     {
 56  1
         return map;
 57  
     }
 58  
 
 59  
     public Object getProperty(String key)
 60  
     {
 61  547
         Object value = map.get(key);
 62  547
         if ((value instanceof String) && (!isDelimiterParsingDisabled()))
 63  
         {
 64  529
             List list = PropertyConverter.split((String) value, getListDelimiter());
 65  529
             return list.size() > 1 ? list : value;
 66  
         }
 67  
         else
 68  
         {
 69  18
             return value;
 70  
         }
 71  
     }
 72  
 
 73  
     protected void addPropertyDirect(String key, Object value)
 74  
     {
 75  16
         Object previousValue = getProperty(key);
 76  
 
 77  16
         if (previousValue == null)
 78  
         {
 79  14
             map.put(key, value);
 80  
         }
 81  2
         else if (previousValue instanceof List)
 82  
         {
 83  
             // the value is added to the existing list
 84  1
             ((List) previousValue).add(value);
 85  
         }
 86  
         else
 87  
         {
 88  
             // the previous value is replaced by a list containing the previous value and the new value
 89  1
             List list = new ArrayList();
 90  1
             list.add(previousValue);
 91  1
             list.add(value);
 92  
 
 93  1
             map.put(key, list);
 94  
         }
 95  16
     }
 96  
 
 97  
     public boolean isEmpty()
 98  
     {
 99  2
         return map.isEmpty();
 100  
     }
 101  
 
 102  
     public boolean containsKey(String key)
 103  
     {
 104  8
         return map.containsKey(key);
 105  
     }
 106  
 
 107  
     protected void clearPropertyDirect(String key)
 108  
     {
 109  6
         map.remove(key);
 110  6
     }
 111  
 
 112  
     public Iterator getKeys()
 113  
     {
 114  14
         return map.keySet().iterator();
 115  
     }
 116  
 
 117  
     /**
 118  
      * Returns a copy of this object. The returned configuration will contain
 119  
      * the same properties as the original. Event listeners are not cloned.
 120  
      *
 121  
      * @return the copy
 122  
      * @since 1.3
 123  
      */
 124  
     public Object clone()
 125  
     {
 126  
         try
 127  
         {
 128  2
             MapConfiguration copy = (MapConfiguration) super.clone();
 129  2
             copy.clearConfigurationListeners();
 130  2
             copy.map = (Map) ConfigurationUtils.clone(map);
 131  2
             return copy;
 132  
         }
 133  
         catch (CloneNotSupportedException cex)
 134  
         {
 135  
             // cannot happen
 136  0
             throw new ConfigurationRuntimeException(cex);
 137  
         }
 138  
     }
 139  
 }