1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections;
  17. import java.util.Map;
  18. /**
  19. * A default implementation of {@link java.util.Map.Entry}
  20. *
  21. * @deprecated Use the version in the keyvalue subpackage. Will be removed in v4.0
  22. * @since Commons Collections 1.0
  23. * @version $Revision: 1.21 $ $Date: 2004/02/18 01:15:42 $
  24. *
  25. * @author James Strachan
  26. * @author Michael A. Smith
  27. * @author Neil O'Toole
  28. * @author Stephen Colebourne
  29. */
  30. public class DefaultMapEntry implements Map.Entry, KeyValue {
  31. /** The key */
  32. private Object key;
  33. /** The value */
  34. private Object value;
  35. /**
  36. * Constructs a new <code>DefaultMapEntry</code> with a null key
  37. * and null value.
  38. */
  39. public DefaultMapEntry() {
  40. super();
  41. }
  42. /**
  43. * Constructs a new <code>DefaultMapEntry</code> with the given
  44. * key and given value.
  45. *
  46. * @param entry the entry to copy, must not be null
  47. * @throws NullPointerException if the entry is null
  48. */
  49. public DefaultMapEntry(Map.Entry entry) {
  50. super();
  51. this.key = entry.getKey();
  52. this.value = entry.getValue();
  53. }
  54. /**
  55. * Constructs a new <code>DefaultMapEntry</code> with the given
  56. * key and given value.
  57. *
  58. * @param key the key for the entry, may be null
  59. * @param value the value for the entry, may be null
  60. */
  61. public DefaultMapEntry(Object key, Object value) {
  62. super();
  63. this.key = key;
  64. this.value = value;
  65. }
  66. // Map.Entry interface
  67. //-------------------------------------------------------------------------
  68. /**
  69. * Gets the key from the Map Entry.
  70. *
  71. * @return the key
  72. */
  73. public Object getKey() {
  74. return key;
  75. }
  76. /**
  77. * Sets the key stored in this Map Entry.
  78. * <p>
  79. * This Map Entry is not connected to a Map, so only the local data is changed.
  80. *
  81. * @param key the new key
  82. */
  83. public void setKey(Object key) {
  84. this.key = key;
  85. }
  86. /**
  87. * Gets the value from the Map Entry.
  88. *
  89. * @return the value
  90. */
  91. public Object getValue() {
  92. return value;
  93. }
  94. /**
  95. * Sets the value stored in this Map Entry.
  96. * <p>
  97. * This Map Entry is not connected to a Map, so only the local data is changed.
  98. *
  99. * @param value the new value
  100. * @return the previous value
  101. */
  102. public Object setValue(Object value) {
  103. Object answer = this.value;
  104. this.value = value;
  105. return answer;
  106. }
  107. // Basics
  108. //-----------------------------------------------------------------------
  109. /**
  110. * Compares this Map Entry with another Map Entry.
  111. * <p>
  112. * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
  113. *
  114. * @param obj the object to compare to
  115. * @return true if equal key and value
  116. */
  117. public boolean equals(Object obj) {
  118. if (obj == this) {
  119. return true;
  120. }
  121. if (obj instanceof Map.Entry == false) {
  122. return false;
  123. }
  124. Map.Entry other = (Map.Entry) obj;
  125. return
  126. (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
  127. (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
  128. }
  129. /**
  130. * Gets a hashCode compatible with the equals method.
  131. * <p>
  132. * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
  133. *
  134. * @return a suitable hash code
  135. */
  136. public int hashCode() {
  137. return (getKey() == null ? 0 : getKey().hashCode()) ^
  138. (getValue() == null ? 0 : getValue().hashCode());
  139. }
  140. /**
  141. * Written to match the output of the Map.Entry's used in
  142. * a {@link java.util.HashMap}.
  143. * @since 3.0
  144. */
  145. public String toString() {
  146. return ""+getKey()+"="+getValue();
  147. }
  148. }