01: /*
02: * Copyright 2003-2006 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
18: import java.util.Map;
19:
20: /**
21: * Abstract Pair class to assist with creating correct
22: * {@link java.util.Map.Entry Map.Entry} implementations.
23: *
24: * @since Commons Collections 3.0
25: * @version $Revision: 405927 $ $Date: 2006-05-12 23:57:03 +0100 (Fri, 12 May 2006) $
26: *
27: * @author James Strachan
28: * @author Michael A. Smith
29: * @author Neil O'Toole
30: * @author Stephen Colebourne
31: */
32: public abstract class AbstractMapEntry extends AbstractKeyValue
33: implements Map.Entry {
34:
35: /**
36: * Constructs a new entry with the given key and given value.
37: *
38: * @param key the key for the entry, may be null
39: * @param value the value for the entry, may be null
40: */
41: protected AbstractMapEntry(Object key, Object value) {
42: super (key, value);
43: }
44:
45: // Map.Entry interface
46: //-------------------------------------------------------------------------
47: /**
48: * Sets the value stored in this <code>Map.Entry</code>.
49: * <p>
50: * This <code>Map.Entry</code> is not connected to a Map, so only the
51: * local data is changed.
52: *
53: * @param value the new value
54: * @return the previous value
55: */
56: public Object setValue(Object value) {
57: Object answer = this .value;
58: this .value = value;
59: return answer;
60: }
61:
62: /**
63: * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
64: * <p>
65: * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
66: *
67: * @param obj the object to compare to
68: * @return true if equal key and value
69: */
70: public boolean equals(Object obj) {
71: if (obj == this ) {
72: return true;
73: }
74: if (obj instanceof Map.Entry == false) {
75: return false;
76: }
77: Map.Entry other = (Map.Entry) obj;
78: return (getKey() == null ? other.getKey() == null : getKey()
79: .equals(other.getKey()))
80: && (getValue() == null ? other.getValue() == null
81: : getValue().equals(other.getValue()));
82: }
83:
84: /**
85: * Gets a hashCode compatible with the equals method.
86: * <p>
87: * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
88: *
89: * @return a suitable hash code
90: */
91: public int hashCode() {
92: return (getKey() == null ? 0 : getKey().hashCode())
93: ^ (getValue() == null ? 0 : getValue().hashCode());
94: }
95:
96: }
|