1 19 package bak.pcj.adapter; 20 21 import bak.pcj.Adapter; 22 import bak.pcj.BooleanIterator; 23 import bak.pcj.BooleanCollection; 24 import bak.pcj.map.ObjectKeyBooleanMap; 25 import bak.pcj.map.AbstractObjectKeyBooleanMap; 26 import bak.pcj.map.ObjectKeyBooleanMapIterator; 27 import bak.pcj.map.MapDefaults; 28 import bak.pcj.map.NoSuchMappingException; 29 import bak.pcj.set.BooleanSet; 30 import bak.pcj.util.Exceptions; 31 32 import java.util.Map ; 33 import java.util.Set ; 34 import java.util.Iterator ; 35 36 71 public class MapToObjectKeyBooleanMapAdapter extends AbstractObjectKeyBooleanMap implements ObjectKeyBooleanMap { 72 73 74 protected Map map; 75 76 77 protected Boolean lastValue; 78 79 94 public MapToObjectKeyBooleanMapAdapter(Map map) { 95 if (map == null) 96 Exceptions.nullArgument("map"); 97 this.map = map; 98 lastValue = null; 99 } 100 101 126 public MapToObjectKeyBooleanMapAdapter(Map map, boolean validate) { 127 if (map == null) 128 Exceptions.nullArgument("map"); 129 this.map = map; 130 lastValue = null; 131 if (validate) 132 evalidate(); 133 } 134 135 public void clear() 136 { map.clear(); } 137 138 public boolean containsKey(Object key) { 139 lastValue = (Boolean )map.get(key); 140 return lastValue != null; 141 } 142 143 public boolean containsValue(boolean value) 144 { return map.containsValue(new Boolean (value)); } 145 146 public ObjectKeyBooleanMapIterator entries() { 147 return new ObjectKeyBooleanMapIterator() { 148 Iterator i = map.entrySet().iterator(); 149 Map.Entry lastEntry = null; 150 151 public boolean hasNext() 152 { return i.hasNext(); } 153 154 public void next() 155 { lastEntry = (Map.Entry)i.next(); } 156 157 public Object getKey() { 158 if (lastEntry == null) 159 Exceptions.noElementToGet(); 160 return lastEntry.getKey(); 161 } 162 163 public boolean getValue() { 164 if (lastEntry == null) 165 Exceptions.noElementToGet(); 166 return ((Boolean )lastEntry.getValue()).booleanValue(); 167 } 168 169 public void remove() { 170 i.remove(); 171 lastEntry = null; 172 } 173 }; 174 } 175 176 public boolean get(Object key) { 177 Boolean value = (Boolean )map.get(key); 178 return value == null ? MapDefaults.defaultBoolean() : value.booleanValue(); 179 } 180 181 public Set keySet() 182 { return map.keySet(); } 183 184 public boolean lget() { 185 if (lastValue == null) 186 Exceptions.noLastElement(); 187 return lastValue.booleanValue(); 188 } 189 190 public boolean put(Object key, boolean value) { 191 Boolean oldValue = (Boolean )map.put(key, new Boolean (value)); 192 return oldValue == null ? MapDefaults.defaultBoolean() : oldValue.booleanValue(); 193 } 194 195 public boolean remove(Object key) { 196 Boolean value = (Boolean )map.remove(key); 197 return value == null ? MapDefaults.defaultBoolean() : value.booleanValue(); 198 } 199 200 public int size() 201 { return map.size(); } 202 203 public BooleanCollection values() 204 { return new CollectionToBooleanCollectionAdapter(map.values()); } 205 206 public boolean tget(Object key) { 207 Boolean value = (Boolean )map.get(key); 208 if (value == null) 209 Exceptions.noSuchMapping(key); 210 return value.booleanValue(); 211 } 212 213 222 public boolean validate() 223 { return Adapter.isObjectKeyBooleanAdaptable(map); } 224 225 234 public void evalidate() { 235 if (!validate()) 236 Exceptions.cannotAdapt("map"); 237 } 238 239 } | Popular Tags |