1 19 package bak.pcj.adapter; 20 21 import bak.pcj.Adapter; 22 import bak.pcj.IntIterator; 23 import bak.pcj.map.IntKeyMap; 24 import bak.pcj.map.AbstractIntKeyMap; 25 import bak.pcj.map.IntKeyMapIterator; 26 import bak.pcj.set.IntSet; 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 MapToIntKeyMapAdapter extends AbstractIntKeyMap implements IntKeyMap { 69 70 71 protected Map map; 72 73 87 public MapToIntKeyMapAdapter(Map map) { 88 this(map, false); 89 } 90 91 115 public MapToIntKeyMapAdapter(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(int key) { 127 return map.get(new Integer (key)) != null; 128 } 129 130 public boolean containsValue(Object value) 131 { return map.containsValue(value); } 132 133 public IntKeyMapIterator entries() { 134 return new IntKeyMapIterator() { 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 int getKey() { 145 if (lastEntry == null) 146 Exceptions.noElementToGet(); 147 return ((Integer )lastEntry.getKey()).intValue(); 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(int key) { 164 return map.get(new Integer (key)); 165 } 166 167 public IntSet keySet() 168 { return new SetToIntSetAdapter(map.keySet()); } 169 170 public Object put(int key, Object value) { 171 return map.put(new Integer (key), value); 172 } 173 174 public Object remove(int key) { 175 return map.remove(new Integer (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.isIntKeyAdaptable(map); } 195 196 205 public void evalidate() { 206 if (!validate()) 207 Exceptions.cannotAdapt("map"); 208 } 209 210 } | Popular Tags |