EnumMap.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* EnumMap.java - Map where keys are enum constants
  2. Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. import java.io.Serializable;
  33. /**
  34. * @author Tom Tromey (tromey@redhat.com)
  35. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  36. * @since 1.5
  37. */
  38. public class EnumMap<K extends Enum<K>, V>
  39. extends AbstractMap<K, V>
  40. implements Cloneable, Serializable
  41. {
  42. private static final long serialVersionUID = 458661240069192865L;
  43. V[] store;
  44. int cardinality;
  45. Class<K> enumClass;
  46. /**
  47. * The cache for {@link #entrySet()}.
  48. */
  49. transient Set<Map.Entry<K, V>> entries;
  50. static final Object emptySlot = new Object();
  51. public EnumMap(Class<K> keyType)
  52. {
  53. store = (V[]) new Object[keyType.getEnumConstants().length];
  54. Arrays.fill(store, emptySlot);
  55. cardinality = 0;
  56. enumClass = keyType;
  57. }
  58. public EnumMap(EnumMap<K, ? extends V> map)
  59. {
  60. store = (V[]) map.store.clone();
  61. cardinality = map.cardinality;
  62. enumClass = map.enumClass;
  63. }
  64. public EnumMap(Map<K, ? extends V> map)
  65. {
  66. if (map instanceof EnumMap)
  67. {
  68. EnumMap<K, ? extends V> other = (EnumMap<K, ? extends V>) map;
  69. store = (V[]) other.store.clone();
  70. cardinality = other.cardinality;
  71. enumClass = other.enumClass;
  72. }
  73. else
  74. {
  75. for (K key : map.keySet())
  76. {
  77. V value = map.get(key);
  78. if (store == null)
  79. {
  80. enumClass = key.getDeclaringClass();
  81. store = (V[]) new Object[enumClass.getEnumConstants().length];
  82. }
  83. int o = key.ordinal();
  84. if (store[o] == emptySlot)
  85. ++cardinality;
  86. store[o] = value;
  87. }
  88. // There must be a single element.
  89. if (store == null)
  90. throw new IllegalArgumentException("no elements in map");
  91. }
  92. }
  93. public int size()
  94. {
  95. return cardinality;
  96. }
  97. public boolean containsValue(Object value)
  98. {
  99. for (V i : store)
  100. {
  101. if (i != emptySlot && AbstractCollection.equals(i , value))
  102. return true;
  103. }
  104. return false;
  105. }
  106. public boolean containsKey(Object key)
  107. {
  108. if (! (key instanceof Enum))
  109. return false;
  110. Enum<K> e = (Enum<K>) key;
  111. if (e.getDeclaringClass() != enumClass)
  112. return false;
  113. return store[e.ordinal()] != emptySlot;
  114. }
  115. public V get(Object key)
  116. {
  117. if (! (key instanceof Enum))
  118. return null;
  119. Enum<K> e = (Enum<K>) key;
  120. if (e.getDeclaringClass() != enumClass)
  121. return null;
  122. V o = store[e.ordinal()];
  123. return o == emptySlot ? null : o;
  124. }
  125. public V put(K key, V value)
  126. {
  127. int o = key.ordinal();
  128. V result;
  129. if (store[o] == emptySlot)
  130. {
  131. result = null;
  132. ++cardinality;
  133. }
  134. else
  135. result = store[o];
  136. store[o] = value;
  137. return result;
  138. }
  139. public V remove(Object key)
  140. {
  141. if (! (key instanceof Enum))
  142. return null;
  143. Enum<K> e = (Enum<K>) key;
  144. if (e.getDeclaringClass() != enumClass)
  145. return null;
  146. V result = store[e.ordinal()];
  147. if (result == emptySlot)
  148. result = null;
  149. else
  150. --cardinality;
  151. store[e.ordinal()] = (V) emptySlot;
  152. return result;
  153. }
  154. public void putAll(Map<? extends K, ? extends V> map)
  155. {
  156. for (K key : map.keySet())
  157. {
  158. V value = map.get(key);
  159. int o = key.ordinal();
  160. if (store[o] == emptySlot)
  161. ++cardinality;
  162. store[o] = value;
  163. }
  164. }
  165. public void clear()
  166. {
  167. Arrays.fill(store, emptySlot);
  168. cardinality = 0;
  169. }
  170. public Set<K> keySet()
  171. {
  172. if (keys == null)
  173. {
  174. keys = new AbstractSet<K>()
  175. {
  176. public int size()
  177. {
  178. return cardinality;
  179. }
  180. public Iterator<K> iterator()
  181. {
  182. return new Iterator<K>()
  183. {
  184. int count = 0;
  185. int index = -1;
  186. public boolean hasNext()
  187. {
  188. return count < cardinality;
  189. }
  190. public K next()
  191. {
  192. ++count;
  193. for (++index; store[index] == emptySlot; ++index)
  194. ;
  195. return enumClass.getEnumConstants()[index];
  196. }
  197. public void remove()
  198. {
  199. --cardinality;
  200. store[index] = (V) emptySlot;
  201. }
  202. };
  203. }
  204. public void clear()
  205. {
  206. EnumMap.this.clear();
  207. }
  208. public boolean contains(Object o)
  209. {
  210. return contains(o);
  211. }
  212. public boolean remove(Object o)
  213. {
  214. return EnumMap.this.remove(o) != null;
  215. }
  216. };
  217. }
  218. return keys;
  219. }
  220. public Collection<V> values()
  221. {
  222. if (values == null)
  223. {
  224. values = new AbstractCollection<V>()
  225. {
  226. public int size()
  227. {
  228. return cardinality;
  229. }
  230. public Iterator<V> iterator()
  231. {
  232. return new Iterator<V>()
  233. {
  234. int count = 0;
  235. int index = -1;
  236. public boolean hasNext()
  237. {
  238. return count < cardinality;
  239. }
  240. public V next()
  241. {
  242. ++count;
  243. for (++index; store[index] == emptySlot; ++index)
  244. ;
  245. return store[index];
  246. }
  247. public void remove()
  248. {
  249. --cardinality;
  250. store[index] = (V) emptySlot;
  251. }
  252. };
  253. }
  254. public void clear()
  255. {
  256. EnumMap.this.clear();
  257. }
  258. };
  259. }
  260. return values;
  261. }
  262. public Set<Map.Entry<K, V>> entrySet()
  263. {
  264. if (entries == null)
  265. {
  266. entries = new AbstractSet<Map.Entry<K, V>>()
  267. {
  268. public int size()
  269. {
  270. return cardinality;
  271. }
  272. public Iterator<Map.Entry<K, V>> iterator()
  273. {
  274. return new Iterator<Map.Entry<K, V>>()
  275. {
  276. int count = 0;
  277. int index = -1;
  278. public boolean hasNext()
  279. {
  280. return count < cardinality;
  281. }
  282. public Map.Entry<K,V> next()
  283. {
  284. ++count;
  285. for (++index; store[index] == emptySlot; ++index)
  286. ;
  287. // FIXME: we could just return something that
  288. // only knows the index. That would be cleaner.
  289. return new AbstractMap.SimpleEntry<K, V>(enumClass.getEnumConstants()[index],
  290. store[index])
  291. {
  292. public V setValue(V newVal)
  293. {
  294. value = newVal;
  295. return put(key, newVal);
  296. }
  297. };
  298. }
  299. public void remove()
  300. {
  301. --cardinality;
  302. store[index] = (V) emptySlot;
  303. }
  304. };
  305. }
  306. public void clear()
  307. {
  308. EnumMap.this.clear();
  309. }
  310. public boolean contains(Object o)
  311. {
  312. if (! (o instanceof Map.Entry))
  313. return false;
  314. Map.Entry<K, V> other = (Map.Entry<K, V>) o;
  315. return (containsKey(other.getKey())
  316. && AbstractCollection.equals(get(other.getKey()),
  317. other.getValue()));
  318. }
  319. public boolean remove(Object o)
  320. {
  321. if (! (o instanceof Map.Entry))
  322. return false;
  323. Map.Entry<K, V> other = (Map.Entry<K, V>) o;
  324. return EnumMap.this.remove(other.getKey()) != null;
  325. }
  326. };
  327. }
  328. return entries;
  329. }
  330. public boolean equals(Object o)
  331. {
  332. if (! (o instanceof EnumMap))
  333. return false;
  334. EnumMap<K, V> other = (EnumMap<K, V>) o;
  335. if (other.enumClass != enumClass || other.cardinality != cardinality)
  336. return false;
  337. return Arrays.equals(store, other.store);
  338. }
  339. public EnumMap<K, V> clone()
  340. {
  341. EnumMap<K, V> result;
  342. try
  343. {
  344. result = (EnumMap<K, V>) super.clone();
  345. }
  346. catch (CloneNotSupportedException ignore)
  347. {
  348. // Can't happen.
  349. result = null;
  350. }
  351. result.store = (V[]) store.clone();
  352. return result;
  353. }
  354. }