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