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