1 19 package bak.pcj.adapter; 20 21 import bak.pcj.Adapter; 22 import bak.pcj.BooleanIterator; 23 import bak.pcj.ShortCollection; 24 import bak.pcj.map.BooleanKeyShortMap; 25 import bak.pcj.map.AbstractBooleanKeyShortMap; 26 import bak.pcj.map.BooleanKeyShortMapIterator; 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.Iterator ; 34 35 70 public class MapToBooleanKeyShortMapAdapter extends AbstractBooleanKeyShortMap implements BooleanKeyShortMap { 71 72 73 protected Map map; 74 75 76 protected Short lastValue; 77 78 94 public MapToBooleanKeyShortMapAdapter(Map map) { 95 if (map == null) 96 Exceptions.nullArgument("map"); 97 this.map = map; 98 lastValue = null; 99 } 100 101 129 public MapToBooleanKeyShortMapAdapter(Map map, boolean validate) { 130 if (map == null) 131 Exceptions.nullArgument("map"); 132 this.map = map; 133 lastValue = null; 134 if (validate) 135 evalidate(); 136 } 137 138 public void clear() 139 { map.clear(); } 140 141 public boolean containsKey(boolean key) { 142 lastValue = (Short )map.get(new Boolean (key)); 143 return lastValue != null; 144 } 145 146 public boolean containsValue(short value) 147 { return map.containsValue(new Short (value)); } 148 149 public BooleanKeyShortMapIterator entries() { 150 return new BooleanKeyShortMapIterator() { 151 Iterator i = map.entrySet().iterator(); 152 Map.Entry lastEntry = null; 153 154 public boolean hasNext() 155 { return i.hasNext(); } 156 157 public void next() 158 { lastEntry = (Map.Entry)i.next(); } 159 160 public boolean getKey() { 161 if (lastEntry == null) 162 Exceptions.noElementToGet(); 163 return ((Boolean )lastEntry.getKey()).booleanValue(); 164 } 165 166 public short getValue() { 167 if (lastEntry == null) 168 Exceptions.noElementToGet(); 169 return ((Short )lastEntry.getValue()).shortValue(); 170 } 171 172 public void remove() { 173 i.remove(); 174 lastEntry = null; 175 } 176 }; 177 } 178 179 public short get(boolean key) { 180 Short value = (Short )map.get(new Boolean (key)); 181 return value == null ? MapDefaults.defaultShort() : value.shortValue(); 182 } 183 184 public BooleanSet keySet() 185 { return new SetToBooleanSetAdapter(map.keySet()); } 186 187 public short lget() { 188 if (lastValue == null) 189 Exceptions.noLastElement(); 190 return lastValue.shortValue(); 191 } 192 193 public short put(boolean key, short value) { 194 Short oldValue = (Short )map.put(new Boolean (key), new Short (value)); 195 return oldValue == null ? MapDefaults.defaultShort() : oldValue.shortValue(); 196 } 197 198 public short remove(boolean key) { 199 Short value = (Short )map.remove(new Boolean (key)); 200 return value == null ? MapDefaults.defaultShort() : value.shortValue(); 201 } 202 203 public int size() 204 { return map.size(); } 205 206 public ShortCollection values() 207 { return new CollectionToShortCollectionAdapter(map.values()); } 208 209 public short tget(boolean key) { 210 Short value = (Short )map.get(new Boolean (key)); 211 if (value == null) 212 Exceptions.noSuchMapping(String.valueOf(key)); 213 return value.shortValue(); 214 } 215 216 225 public boolean validate() 226 { return Adapter.isBooleanKeyShortAdaptable(map); } 227 228 238 public void evalidate() { 239 if (!validate()) 240 Exceptions.cannotAdapt("map"); 241 } 242 243 } | Popular Tags |