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. import org.apache.commons.collections.KeyValue;
  19. /**
  20. * A mutable KeyValue pair that does not implement MapEntry.
  21. * <p>
  22. * Note that a <code>DefaultKeyValue</code> instance may not contain
  23. * itself as a key or value.
  24. *
  25. * @since Commons Collections 3.0
  26. * @version $Revision: 1.4 $ $Date: 2004/02/18 01:00:08 $
  27. *
  28. * @author James Strachan
  29. * @author Michael A. Smith
  30. * @author Neil O'Toole
  31. * @author Stephen Colebourne
  32. */
  33. public class DefaultKeyValue extends AbstractKeyValue {
  34. /**
  35. * Constructs a new pair with a null key and null value.
  36. */
  37. public DefaultKeyValue() {
  38. super(null, null);
  39. }
  40. /**
  41. * Constructs a new pair with the specified key and given value.
  42. *
  43. * @param key the key for the entry, may be null
  44. * @param value the value for the entry, may be null
  45. */
  46. public DefaultKeyValue(final Object key, final Object value) {
  47. super(key, value);
  48. }
  49. /**
  50. * Constructs a new pair from the specified KeyValue.
  51. *
  52. * @param pair the pair to copy, must not be null
  53. * @throws NullPointerException if the entry is null
  54. */
  55. public DefaultKeyValue(final KeyValue pair) {
  56. super(pair.getKey(), pair.getValue());
  57. }
  58. /**
  59. * Constructs a new pair from the specified MapEntry.
  60. *
  61. * @param entry the entry to copy, must not be null
  62. * @throws NullPointerException if the entry is null
  63. */
  64. public DefaultKeyValue(final Map.Entry entry) {
  65. super(entry.getKey(), entry.getValue());
  66. }
  67. //-----------------------------------------------------------------------
  68. /**
  69. * Sets the key.
  70. *
  71. * @param key the new key
  72. * @return the old key
  73. * @throws IllegalArgumentException if key is this object
  74. */
  75. public Object setKey(final Object key) {
  76. if (key == this) {
  77. throw new IllegalArgumentException("DefaultKeyValue may not contain itself as a key.");
  78. }
  79. final Object old = this.key;
  80. this.key = key;
  81. return old;
  82. }
  83. /**
  84. * Sets the value.
  85. *
  86. * @return the old value of the value
  87. * @param value the new value
  88. * @throws IllegalArgumentException if value is this object
  89. */
  90. public Object setValue(final Object value) {
  91. if (value == this) {
  92. throw new IllegalArgumentException("DefaultKeyValue may not contain itself as a value.");
  93. }
  94. final Object old = this.value;
  95. this.value = value;
  96. return old;
  97. }
  98. //-----------------------------------------------------------------------
  99. /**
  100. * Returns a new <code>Map.Entry</code> object with key and value from this pair.
  101. *
  102. * @return a MapEntry instance
  103. */
  104. public Map.Entry toMapEntry() {
  105. return new DefaultMapEntry(this);
  106. }
  107. //-----------------------------------------------------------------------
  108. /**
  109. * Compares this Map Entry with another Map Entry.
  110. * <p>
  111. * Returns true if the compared object is also a <code>DefaultKeyValue</code>,
  112. * and its key and value are equal to this object's key and value.
  113. *
  114. * @param obj the object to compare to
  115. * @return true if equal key and value
  116. */
  117. public boolean equals(final Object obj) {
  118. if (obj == this) {
  119. return true;
  120. }
  121. if (obj instanceof DefaultKeyValue == false) {
  122. return false;
  123. }
  124. DefaultKeyValue other = (DefaultKeyValue) 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. * however subclasses may override this.
  134. *
  135. * @return a suitable hash code
  136. */
  137. public int hashCode() {
  138. return (getKey() == null ? 0 : getKey().hashCode()) ^
  139. (getValue() == null ? 0 : getValue().hashCode());
  140. }
  141. }