1 19 package bak.pcj.adapter; 20 21 import bak.pcj.Adapter; 22 import bak.pcj.LongIterator; 23 import bak.pcj.DoubleCollection; 24 import bak.pcj.map.LongKeyDoubleMap; 25 import bak.pcj.map.AbstractLongKeyDoubleMap; 26 import bak.pcj.map.LongKeyDoubleMapIterator; 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.Iterator ; 34 35 70 public class MapToLongKeyDoubleMapAdapter extends AbstractLongKeyDoubleMap implements LongKeyDoubleMap { 71 72 73 protected Map map; 74 75 76 protected Double lastValue; 77 78 94 public MapToLongKeyDoubleMapAdapter(Map map) { 95 if (map == null) 96 Exceptions.nullArgument("map"); 97 this.map = map; 98 lastValue = null; 99 } 100 101 129 public MapToLongKeyDoubleMapAdapter(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(long key) { 142 lastValue = (Double )map.get(new Long (key)); 143 return lastValue != null; 144 } 145 146 public boolean containsValue(double value) 147 { return map.containsValue(new Double (value)); } 148 149 public LongKeyDoubleMapIterator entries() { 150 return new LongKeyDoubleMapIterator() { 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 long getKey() { 161 if (lastEntry == null) 162 Exceptions.noElementToGet(); 163 return ((Long )lastEntry.getKey()).longValue(); 164 } 165 166 public double getValue() { 167 if (lastEntry == null) 168 Exceptions.noElementToGet(); 169 return ((Double )lastEntry.getValue()).doubleValue(); 170 } 171 172 public void remove() { 173 i.remove(); 174 lastEntry = null; 175 } 176 }; 177 } 178 179 public double get(long key) { 180 Double value = (Double )map.get(new Long (key)); 181 return value == null ? MapDefaults.defaultDouble() : value.doubleValue(); 182 } 183 184 public LongSet keySet() 185 { return new SetToLongSetAdapter(map.keySet()); } 186 187 public double lget() { 188 if (lastValue == null) 189 Exceptions.noLastElement(); 190 return lastValue.doubleValue(); 191 } 192 193 public double put(long key, double value) { 194 Double oldValue = (Double )map.put(new Long (key), new Double (value)); 195 return oldValue == null ? MapDefaults.defaultDouble() : oldValue.doubleValue(); 196 } 197 198 public double remove(long key) { 199 Double value = (Double )map.remove(new Long (key)); 200 return value == null ? MapDefaults.defaultDouble() : value.doubleValue(); 201 } 202 203 public int size() 204 { return map.size(); } 205 206 public DoubleCollection values() 207 { return new CollectionToDoubleCollectionAdapter(map.values()); } 208 209 public double tget(long key) { 210 Double value = (Double )map.get(new Long (key)); 211 if (value == null) 212 Exceptions.noSuchMapping(String.valueOf(key)); 213 return value.doubleValue(); 214 } 215 216 225 public boolean validate() 226 { return Adapter.isLongKeyDoubleAdaptable(map); } 227 228 238 public void evalidate() { 239 if (!validate()) 240 Exceptions.cannotAdapt("map"); 241 } 242 243 } | Popular Tags |