1. /*
  2. * Copyright 2003-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.keyvalue;
  17. import java.util.Map;
  18. /**
  19. * Abstract Pair class to assist with creating correct Map Entry implementations.
  20. *
  21. * @since Commons Collections 3.0
  22. * @version $Revision: 1.4 $ $Date: 2004/02/18 01:00:08 $
  23. *
  24. * @author James Strachan
  25. * @author Michael A. Smith
  26. * @author Neil O'Toole
  27. * @author Stephen Colebourne
  28. */
  29. public abstract class AbstractMapEntry extends AbstractKeyValue implements Map.Entry {
  30. /**
  31. * Constructs a new entry with the given key and given value.
  32. *
  33. * @param key the key for the entry, may be null
  34. * @param value the value for the entry, may be null
  35. */
  36. protected AbstractMapEntry(Object key, Object value) {
  37. super(key, value);
  38. }
  39. // Map.Entry interface
  40. //-------------------------------------------------------------------------
  41. /**
  42. * Sets the value stored in this Map Entry.
  43. * <p>
  44. * This Map Entry is not connected to a Map, so only the local data is changed.
  45. *
  46. * @param value the new value
  47. * @return the previous value
  48. */
  49. public Object setValue(Object value) {
  50. Object answer = this.value;
  51. this.value = value;
  52. return answer;
  53. }
  54. /**
  55. * Compares this Map Entry with another Map Entry.
  56. * <p>
  57. * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
  58. *
  59. * @param obj the object to compare to
  60. * @return true if equal key and value
  61. */
  62. public boolean equals(Object obj) {
  63. if (obj == this) {
  64. return true;
  65. }
  66. if (obj instanceof Map.Entry == false) {
  67. return false;
  68. }
  69. Map.Entry other = (Map.Entry) obj;
  70. return
  71. (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
  72. (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
  73. }
  74. /**
  75. * Gets a hashCode compatible with the equals method.
  76. * <p>
  77. * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
  78. *
  79. * @return a suitable hash code
  80. */
  81. public int hashCode() {
  82. return (getKey() == null ? 0 : getKey().hashCode()) ^
  83. (getValue() == null ? 0 : getValue().hashCode());
  84. }
  85. }