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.io.Serializable;
  18. import java.util.Map;
  19. import org.apache.commons.collections.KeyValue;
  20. /**
  21. * A Map Entry tied to a map underneath.
  22. * <p>
  23. * This can be used to enable a map entry to make changes on the underlying
  24. * map, however this will probably mess up any iterators.
  25. *
  26. * @since Commons Collections 3.0
  27. * @version $Revision: 1.5 $ $Date: 2004/04/09 14:35:10 $
  28. *
  29. * @author Stephen Colebourne
  30. */
  31. public class TiedMapEntry implements Map.Entry, KeyValue, Serializable {
  32. /** Serialization version */
  33. private static final long serialVersionUID = -8453869361373831205L;
  34. /** The map underlying the entry/iterator */
  35. private final Map map;
  36. /** The key */
  37. private final Object key;
  38. /**
  39. * Constructs a new entry with the given Map and key.
  40. *
  41. * @param map the map
  42. * @param key the key
  43. */
  44. public TiedMapEntry(Map map, Object key) {
  45. super();
  46. this.map = map;
  47. this.key = key;
  48. }
  49. // Map.Entry interface
  50. //-------------------------------------------------------------------------
  51. /**
  52. * Gets the key of this entry
  53. *
  54. * @return the key
  55. */
  56. public Object getKey() {
  57. return key;
  58. }
  59. /**
  60. * Gets the value of this entry direct from the map.
  61. *
  62. * @return the value
  63. */
  64. public Object getValue() {
  65. return map.get(key);
  66. }
  67. /**
  68. * Sets the value associated with the key direct onto the map.
  69. *
  70. * @param value the new value
  71. * @return the old value
  72. * @throws IllegalArgumentException if the value is set to this map entry
  73. */
  74. public Object setValue(Object value) {
  75. if (value == this) {
  76. throw new IllegalArgumentException("Cannot set value to this map entry");
  77. }
  78. return map.put(key, value);
  79. }
  80. /**
  81. * Compares this Map Entry with another Map Entry.
  82. * <p>
  83. * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
  84. *
  85. * @param obj the object to compare to
  86. * @return true if equal key and value
  87. */
  88. public boolean equals(Object obj) {
  89. if (obj == this) {
  90. return true;
  91. }
  92. if (obj instanceof Map.Entry == false) {
  93. return false;
  94. }
  95. Map.Entry other = (Map.Entry) obj;
  96. Object value = getValue();
  97. return
  98. (key == null ? other.getKey() == null : key.equals(other.getKey())) &&
  99. (value == null ? other.getValue() == null : value.equals(other.getValue()));
  100. }
  101. /**
  102. * Gets a hashCode compatible with the equals method.
  103. * <p>
  104. * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
  105. *
  106. * @return a suitable hash code
  107. */
  108. public int hashCode() {
  109. Object value = getValue();
  110. return (getKey() == null ? 0 : getKey().hashCode()) ^
  111. (value == null ? 0 : value.hashCode());
  112. }
  113. /**
  114. * Gets a string version of the entry.
  115. *
  116. * @return entry as a string
  117. */
  118. public String toString() {
  119. return getKey() + "=" + getValue();
  120. }
  121. }