001: /*
002: * Copyright 2003-2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.keyvalue;
017:
018: import java.io.Serializable;
019: import java.util.Map;
020:
021: import org.apache.commons.collections.KeyValue;
022:
023: /**
024: * A {@link java.util.Map.Entry Map.Entry} tied to a map underneath.
025: * <p>
026: * This can be used to enable a map entry to make changes on the underlying
027: * map, however this will probably mess up any iterators.
028: *
029: * @since Commons Collections 3.0
030: * @version $Revision: 405927 $ $Date: 2006-05-12 23:57:03 +0100 (Fri, 12 May 2006) $
031: *
032: * @author Stephen Colebourne
033: */
034: public class TiedMapEntry implements Map.Entry, KeyValue, Serializable {
035:
036: /** Serialization version */
037: private static final long serialVersionUID = -8453869361373831205L;
038:
039: /** The map underlying the entry/iterator */
040: private final Map map;
041: /** The key */
042: private final Object key;
043:
044: /**
045: * Constructs a new entry with the given Map and key.
046: *
047: * @param map the map
048: * @param key the key
049: */
050: public TiedMapEntry(Map map, Object key) {
051: super ();
052: this .map = map;
053: this .key = key;
054: }
055:
056: // Map.Entry interface
057: //-------------------------------------------------------------------------
058: /**
059: * Gets the key of this entry
060: *
061: * @return the key
062: */
063: public Object getKey() {
064: return key;
065: }
066:
067: /**
068: * Gets the value of this entry direct from the map.
069: *
070: * @return the value
071: */
072: public Object getValue() {
073: return map.get(key);
074: }
075:
076: /**
077: * Sets the value associated with the key direct onto the map.
078: *
079: * @param value the new value
080: * @return the old value
081: * @throws IllegalArgumentException if the value is set to this map entry
082: */
083: public Object setValue(Object value) {
084: if (value == this ) {
085: throw new IllegalArgumentException(
086: "Cannot set value to this map entry");
087: }
088: return map.put(key, value);
089: }
090:
091: /**
092: * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
093: * <p>
094: * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
095: *
096: * @param obj the object to compare to
097: * @return true if equal key and value
098: */
099: public boolean equals(Object obj) {
100: if (obj == this ) {
101: return true;
102: }
103: if (obj instanceof Map.Entry == false) {
104: return false;
105: }
106: Map.Entry other = (Map.Entry) obj;
107: Object value = getValue();
108: return (key == null ? other.getKey() == null : key.equals(other
109: .getKey()))
110: && (value == null ? other.getValue() == null : value
111: .equals(other.getValue()));
112: }
113:
114: /**
115: * Gets a hashCode compatible with the equals method.
116: * <p>
117: * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
118: *
119: * @return a suitable hash code
120: */
121: public int hashCode() {
122: Object value = getValue();
123: return (getKey() == null ? 0 : getKey().hashCode())
124: ^ (value == null ? 0 : value.hashCode());
125: }
126:
127: /**
128: * Gets a string version of the entry.
129: *
130: * @return entry as a string
131: */
132: public String toString() {
133: return getKey() + "=" + getValue();
134: }
135:
136: }
|