01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.collections.keyvalue;
18:
19: import java.util.Map;
20:
21: /**
22: * Abstract Pair class to assist with creating correct Map Entry implementations.
23: *
24: * @since Commons Collections 3.0
25: * @version $Revision: 438363 $ $Date: 2006-08-30 05:48:00 +0100 (Wed, 30 Aug 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 Map Entry.
49: * <p>
50: * This Map Entry is not connected to a Map, so only the local data is changed.
51: *
52: * @param value the new value
53: * @return the previous value
54: */
55: public Object setValue(Object value) {
56: Object answer = this .value;
57: this .value = value;
58: return answer;
59: }
60:
61: /**
62: * Compares this Map Entry with another Map Entry.
63: * <p>
64: * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
65: *
66: * @param obj the object to compare to
67: * @return true if equal key and value
68: */
69: public boolean equals(Object obj) {
70: if (obj == this ) {
71: return true;
72: }
73: if (obj instanceof Map.Entry == false) {
74: return false;
75: }
76: Map.Entry other = (Map.Entry) obj;
77: return (getKey() == null ? other.getKey() == null : getKey()
78: .equals(other.getKey()))
79: && (getValue() == null ? other.getValue() == null
80: : getValue().equals(other.getValue()));
81: }
82:
83: /**
84: * Gets a hashCode compatible with the equals method.
85: * <p>
86: * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
87: *
88: * @return a suitable hash code
89: */
90: public int hashCode() {
91: return (getKey() == null ? 0 : getKey().hashCode())
92: ^ (getValue() == null ? 0 : getValue().hashCode());
93: }
94:
95: }
|