1 19 package bak.pcj.adapter; 20 21 import bak.pcj.Adapter; 22 import bak.pcj.ByteIterator; 23 import bak.pcj.ByteCollection; 24 import bak.pcj.map.ObjectKeyByteMap; 25 import bak.pcj.map.AbstractObjectKeyByteMap; 26 import bak.pcj.map.ObjectKeyByteMapIterator; 27 import bak.pcj.map.MapDefaults; 28 import bak.pcj.map.NoSuchMappingException; 29 import bak.pcj.set.ByteSet; 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 MapToObjectKeyByteMapAdapter extends AbstractObjectKeyByteMap implements ObjectKeyByteMap { 72 73 74 protected Map map; 75 76 77 protected Byte lastValue; 78 79 94 public MapToObjectKeyByteMapAdapter(Map map) { 95 if (map == null) 96 Exceptions.nullArgument("map"); 97 this.map = map; 98 lastValue = null; 99 } 100 101 126 public MapToObjectKeyByteMapAdapter(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 = (Byte )map.get(key); 140 return lastValue != null; 141 } 142 143 public boolean containsValue(byte value) 144 { return map.containsValue(new Byte (value)); } 145 146 public ObjectKeyByteMapIterator entries() { 147 return new ObjectKeyByteMapIterator() { 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 byte getValue() { 164 if (lastEntry == null) 165 Exceptions.noElementToGet(); 166 return ((Byte )lastEntry.getValue()).byteValue(); 167 } 168 169 public void remove() { 170 i.remove(); 171 lastEntry = null; 172 } 173 }; 174 } 175 176 public byte get(Object key) { 177 Byte value = (Byte )map.get(key); 178 return value == null ? MapDefaults.defaultByte() : value.byteValue(); 179 } 180 181 public Set keySet() 182 { return map.keySet(); } 183 184 public byte lget() { 185 if (lastValue == null) 186 Exceptions.noLastElement(); 187 return lastValue.byteValue(); 188 } 189 190 public byte put(Object key, byte value) { 191 Byte oldValue = (Byte )map.put(key, new Byte (value)); 192 return oldValue == null ? MapDefaults.defaultByte() : oldValue.byteValue(); 193 } 194 195 public byte remove(Object key) { 196 Byte value = (Byte )map.remove(key); 197 return value == null ? MapDefaults.defaultByte() : value.byteValue(); 198 } 199 200 public int size() 201 { return map.size(); } 202 203 public ByteCollection values() 204 { return new CollectionToByteCollectionAdapter(map.values()); } 205 206 public byte tget(Object key) { 207 Byte value = (Byte )map.get(key); 208 if (value == null) 209 Exceptions.noSuchMapping(key); 210 return value.byteValue(); 211 } 212 213 222 public boolean validate() 223 { return Adapter.isObjectKeyByteAdaptable(map); } 224 225 234 public void evalidate() { 235 if (!validate()) 236 Exceptions.cannotAdapt("map"); 237 } 238 239 } | Popular Tags |