/interpreter/tags/at2-build270707/src/edu/vub/util/IdentityHashMap.java

http://ambienttalk.googlecode.com/ · Java · 155 lines · 94 code · 25 blank · 36 comment · 1 complexity · 941088b575496751fd6d0f7dfc274d28 MD5 · raw file

  1. /**
  2. * Copyright (c) 2003-2004, Joe Walnes
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above copyright notice, this list of
  9. * conditions and the following disclaimer. Redistributions in binary form must reproduce
  10. * the above copyright notice, this list of conditions and the following disclaimer in
  11. * the documentation and/or other materials provided with the distribution.
  12. *
  13. * Neither the name of XStream nor the names of its contributors may be used to endorse
  14. * or promote products derived from this software without specific prior written
  15. * permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  20. * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  22. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  25. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. * DAMAGE.
  27. */
  28. package edu.vub.util;
  29. import java.util.Collection;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. import java.util.Map;
  33. import java.util.Set;
  34. /**
  35. * Behaves the same way as JDK1.4 java.util.IdentityHashMap, but in JDK1.3 as well.
  36. * <p/>
  37. * Modified version of ObjectIdDictionary from XStream.
  38. */
  39. public class IdentityHashMap implements Map
  40. {
  41. private transient Map map;
  42. private static class KeyWrapper
  43. {
  44. private final Object obj;
  45. KeyWrapper(Object obj)
  46. {
  47. this.obj = obj;
  48. }
  49. public int hashCode()
  50. {
  51. return System.identityHashCode(obj);
  52. }
  53. public boolean equals(Object other)
  54. {
  55. return obj == ((KeyWrapper)other).obj;
  56. }
  57. public String toString()
  58. {
  59. return obj.toString();
  60. }
  61. }
  62. /** Default Constructor */
  63. public IdentityHashMap()
  64. {
  65. map = new HashMap();
  66. }
  67. public int size()
  68. {
  69. return map.size();
  70. }
  71. public boolean isEmpty()
  72. {
  73. return map.isEmpty();
  74. }
  75. public boolean containsKey(Object key)
  76. {
  77. return map.containsKey(new KeyWrapper(key));
  78. }
  79. public boolean containsValue(Object value)
  80. {
  81. return map.containsValue(value);
  82. }
  83. public Object get(Object key)
  84. {
  85. return map.get(new KeyWrapper(key));
  86. }
  87. public Object put(Object key, Object value)
  88. {
  89. return map.put(new KeyWrapper(key), value);
  90. }
  91. public Object remove(Object key)
  92. {
  93. return map.remove(new KeyWrapper(key));
  94. }
  95. public void putAll(Map t)
  96. {
  97. throw new UnsupportedOperationException("unimplemented");
  98. }
  99. public void clear()
  100. {
  101. map.clear();
  102. }
  103. public int hashCode()
  104. {
  105. return map.hashCode();
  106. }
  107. public Set keySet()
  108. {
  109. return map.keySet();
  110. }
  111. public Collection values()
  112. {
  113. return map.values();
  114. }
  115. public Set entrySet()
  116. {
  117. return map.entrySet();
  118. }
  119. /**
  120. * @return an iterator over the set of keys.
  121. */
  122. public Iterator iterator()
  123. {
  124. return map.keySet().iterator();
  125. }
  126. public boolean equals(Object o)
  127. {
  128. throw new RuntimeException("unimplemented");
  129. }
  130. }