1 16 17 package org.apache.commons.configuration; 18 19 import java.util.AbstractMap ; 20 import java.util.AbstractSet ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 37 public class ConfigurationMap extends AbstractMap 38 { 39 42 Configuration configuration = null; 43 44 51 public ConfigurationMap(Configuration configuration) 52 { 53 this.configuration = configuration; 54 } 55 56 59 public Set entrySet() 60 { 61 return new ConfigurationSet(configuration); 62 } 63 64 67 public Object put(Object key, Object value) 68 { 69 String strKey = String.valueOf(key); 70 Object old = configuration.getProperty(strKey); 71 configuration.setProperty(strKey, value); 72 return old; 73 } 74 75 78 public Object get(Object key) 79 { 80 return configuration.getProperty(String.valueOf(key)); 81 } 82 83 static class ConfigurationSet extends AbstractSet 84 { 85 private Configuration configuration = null; 86 87 private class Entry implements Map.Entry 88 { 89 private Object key = null; 90 91 private Entry(Object key) 92 { 93 this.key = key; 94 } 95 96 public Object getKey() 97 { 98 return key; 99 } 100 101 public Object getValue() 102 { 103 return configuration.getProperty((String ) key); 104 } 105 106 public Object setValue(Object value) 107 { 108 Object old = getValue(); 109 configuration.setProperty((String ) key, value); 110 return old; 111 } 112 113 } 114 115 private class ConfigurationSetIterator implements Iterator 116 { 117 private Iterator keys; 118 119 private ConfigurationSetIterator() 120 { 121 keys = configuration.getKeys(); 122 } 123 124 public boolean hasNext() 125 { 126 return keys.hasNext(); 127 } 128 129 public Object next() 130 { 131 return new Entry(keys.next()); 132 } 133 134 public void remove() 135 { 136 keys.remove(); 137 } 138 139 } 140 141 ConfigurationSet(Configuration configuration) 142 { 143 this.configuration = configuration; 144 } 145 146 149 public int size() 150 { 151 int count = 0; 153 for (Iterator iterator = configuration.getKeys(); iterator.hasNext(); ) 154 { 155 iterator.next(); 156 count++; 157 } 158 return count; 159 } 160 161 164 public Iterator iterator() 165 { 166 return new ConfigurationSetIterator(); 167 } 168 } 169 } 170 | Popular Tags |