Coverage Report - org.apache.commons.configuration.ConfigurationMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationMap
90%
9/10
N/A
1,062
ConfigurationMap$1
N/A
N/A
1,062
ConfigurationMap$ConfigurationSet
100%
11/11
100%
1/1
1,062
ConfigurationMap$ConfigurationSet$ConfigurationSetIterator
100%
8/8
N/A
1,062
ConfigurationMap$ConfigurationSet$Entry
67%
6/9
N/A
1,062
 
 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.AbstractMap;
 21  
 import java.util.AbstractSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * <p>The <code>ConfigurationMap</code> wraps a
 28  
  * configuration-collection
 29  
  * {@link org.apache.commons.configuration.Configuration}
 30  
  * instance to provide a <code>Map</code> interface.</p>
 31  
  *
 32  
  * <p><em>Note:</em> This implementation is incomplete.</p>
 33  
  *
 34  
  * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
 35  
  * @version $Revision: 492216 $, $Date: 2007-01-03 17:51:24 +0100 (Mi, 03 Jan 2007) $
 36  
  * @since 1.0
 37  
  */
 38  
 public class ConfigurationMap extends AbstractMap
 39  
 {
 40  
     /**
 41  
      * The <code>Configuration</code> wrapped by this class.
 42  
      */
 43  
     private Configuration configuration;
 44  
 
 45  
     /**
 46  
      * Creates a new instance of a <code>ConfigurationMap</code>
 47  
      * that wraps the specified <code>Configuration</code>
 48  
      * instance.
 49  
      * @param configuration <code>Configuration</code>
 50  
      * instance.
 51  
      */
 52  
     public ConfigurationMap(Configuration configuration)
 53  80
     {
 54  80
         this.configuration = configuration;
 55  80
     }
 56  
 
 57  
     /**
 58  
      * Returns the wrapped <code>Configuration</code> object.
 59  
      *
 60  
      * @return the wrapped configuration
 61  
      * @since 1.2
 62  
      */
 63  
     public Configuration getConfiguration()
 64  
     {
 65  4014
         return configuration;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Returns a set with the entries contained in this configuration-based map.
 70  
      *
 71  
      * @return a set with the contained entries
 72  
      * @see java.util.Map#entrySet()
 73  
      */
 74  
     public Set entrySet()
 75  
     {
 76  0
         return new ConfigurationSet(configuration);
 77  
     }
 78  
 
 79  
     /**
 80  
      * Stores the value for the specified key. The value is stored in the
 81  
      * underlying configuration.
 82  
      *
 83  
      * @param key the key (will be converted to a string)
 84  
      * @param value the value
 85  
      * @return the old value of this key or <b>null</b> if it is new
 86  
      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
 87  
      */
 88  
     public Object put(Object key, Object value)
 89  
     {
 90  7
         String strKey = String.valueOf(key);
 91  7
         Object old = configuration.getProperty(strKey);
 92  7
         configuration.setProperty(strKey, value);
 93  7
         return old;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Returns the value of the specified key. The key is converted to a string
 98  
      * and then passed to the underlying configuration.
 99  
      *
 100  
      * @param key the key
 101  
      * @return the value of this key
 102  
      * @see java.util.Map#get(java.lang.Object)
 103  
      */
 104  
     public Object get(Object key)
 105  
     {
 106  8
         return configuration.getProperty(String.valueOf(key));
 107  
     }
 108  
 
 109  
     /**
 110  
      * Set of entries in the map.
 111  
      */
 112  8
     static class ConfigurationSet extends AbstractSet
 113  
     {
 114  
         /** The configuration mapped to this entry set. */
 115  
         private Configuration configuration;
 116  
 
 117  
         /**
 118  
          * A Map entry in the ConfigurationMap.
 119  
          */
 120  7
         private final class Entry implements Map.Entry
 121  
         {
 122  
             /** The key of the map entry. */
 123  
             private Object key;
 124  
 
 125  
             private Entry(Object key)
 126  7
             {
 127  7
                 this.key = key;
 128  7
             }
 129  
 
 130  
             public Object getKey()
 131  
             {
 132  56
                 return key;
 133  
             }
 134  
 
 135  
             public Object getValue()
 136  
             {
 137  7
                 return configuration.getProperty((String) key);
 138  
             }
 139  
 
 140  
             public Object setValue(Object value)
 141  
             {
 142  0
                 Object old = getValue();
 143  0
                 configuration.setProperty((String) key, value);
 144  0
                 return old;
 145  
             }
 146  
         }
 147  
 
 148  
         /**
 149  
          * Iterator over the entries in the ConfigurationMap.
 150  
          */
 151  1
         private final class ConfigurationSetIterator implements Iterator
 152  
         {
 153  
             /** An iterator over the keys in the configuration. */
 154  
             private Iterator keys;
 155  
 
 156  
             private ConfigurationSetIterator()
 157  1
             {
 158  1
                 keys = configuration.getKeys();
 159  1
             }
 160  
 
 161  
             public boolean hasNext()
 162  
             {
 163  8
                 return keys.hasNext();
 164  
             }
 165  
 
 166  
             public Object next()
 167  
             {
 168  7
                 return new Entry(keys.next());
 169  
             }
 170  
 
 171  
             public void remove()
 172  
             {
 173  7
                 keys.remove();
 174  7
             }
 175  
         }
 176  
 
 177  
         ConfigurationSet(Configuration configuration)
 178  2
         {
 179  2
             this.configuration = configuration;
 180  2
         }
 181  
 
 182  
         /**
 183  
          * @see java.util.Collection#size()
 184  
          */
 185  
         public int size()
 186  
         {
 187  
             // Ouch. Now _that_ one is expensive...
 188  2
             int count = 0;
 189  2
             for (Iterator iterator = configuration.getKeys(); iterator.hasNext();)
 190  
             {
 191  7
                 iterator.next();
 192  7
                 count++;
 193  7
             }
 194  2
             return count;
 195  
         }
 196  
 
 197  
         /**
 198  
          * @see java.util.Collection#iterator()
 199  
          */
 200  
         public Iterator iterator()
 201  
         {
 202  1
             return new ConfigurationSetIterator();
 203  
         }
 204  
     }
 205  
 }