1 19 package bak.pcj.adapter; 20 21 import bak.pcj.Adapter; 22 import bak.pcj.ByteIterator; 23 import bak.pcj.map.ByteKeyMap; 24 import bak.pcj.map.AbstractByteKeyMap; 25 import bak.pcj.map.ByteKeyMapIterator; 26 import bak.pcj.set.ByteSet; 27 import bak.pcj.util.Exceptions; 28 29 import java.util.Map ; 30 import java.util.Iterator ; 31 import java.util.Collection ; 32 33 68 public class MapToByteKeyMapAdapter extends AbstractByteKeyMap implements ByteKeyMap { 69 70 71 protected Map map; 72 73 87 public MapToByteKeyMapAdapter(Map map) { 88 this(map, false); 89 } 90 91 115 public MapToByteKeyMapAdapter(Map map, boolean validate) { 116 if (map == null) 117 Exceptions.nullArgument("map"); 118 this.map = map; 119 if (validate) 120 evalidate(); 121 } 122 123 public void clear() 124 { map.clear(); } 125 126 public boolean containsKey(byte key) { 127 return map.get(new Byte (key)) != null; 128 } 129 130 public boolean containsValue(Object value) 131 { return map.containsValue(value); } 132 133 public ByteKeyMapIterator entries() { 134 return new ByteKeyMapIterator() { 135 Iterator i = map.entrySet().iterator(); 136 Map.Entry lastEntry = null; 137 138 public boolean hasNext() 139 { return i.hasNext(); } 140 141 public void next() 142 { lastEntry = (Map.Entry)i.next(); } 143 144 public byte getKey() { 145 if (lastEntry == null) 146 Exceptions.noElementToGet(); 147 return ((Byte )lastEntry.getKey()).byteValue(); 148 } 149 150 public Object getValue() { 151 if (lastEntry == null) 152 Exceptions.noElementToGet(); 153 return lastEntry.getValue(); 154 } 155 156 public void remove() { 157 i.remove(); 158 lastEntry = null; 159 } 160 }; 161 } 162 163 public Object get(byte key) { 164 return map.get(new Byte (key)); 165 } 166 167 public ByteSet keySet() 168 { return new SetToByteSetAdapter(map.keySet()); } 169 170 public Object put(byte key, Object value) { 171 return map.put(new Byte (key), value); 172 } 173 174 public Object remove(byte key) { 175 return map.remove(new Byte (key)); 176 } 177 178 public int size() 179 { return map.size(); } 180 181 public Collection values() 182 { return map.values(); } 183 184 193 public boolean validate() 194 { return Adapter.isByteKeyAdaptable(map); } 195 196 205 public void evalidate() { 206 if (!validate()) 207 Exceptions.cannotAdapt("map"); 208 } 209 210 } | Popular Tags |