1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * 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
23 * {@link java.util.Map.Entry Map.Entry} implementations.
24 *
25 * @since 3.0
26 * @version $Id: AbstractMapEntry.java 1429905 2013-01-07 17:15:14Z ggregory $
27 */
28 public abstract class AbstractMapEntry<K, V> extends AbstractKeyValue<K, V> implements Map.Entry<K, V> {
29
30 /**
31 * Constructs a new entry with the given key and given value.
32 *
33 * @param key the key for the entry, may be null
34 * @param value the value for the entry, may be null
35 */
36 protected AbstractMapEntry(final K key, final V value) {
37 super(key, value);
38 }
39
40 // Map.Entry interface
41 //-------------------------------------------------------------------------
42 /**
43 * Sets the value stored in this <code>Map.Entry</code>.
44 * <p>
45 * This <code>Map.Entry</code> is not connected to a Map, so only the
46 * local data is changed.
47 *
48 * @param value the new value
49 * @return the previous value
50 */
51 public V setValue(final V value) {
52 final V answer = this.value;
53 this.value = value;
54 return answer;
55 }
56
57 /**
58 * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
59 * <p>
60 * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
61 *
62 * @param obj the object to compare to
63 * @return true if equal key and value
64 */
65 @Override
66 public boolean equals(final Object obj) {
67 if (obj == this) {
68 return true;
69 }
70 if (obj instanceof Map.Entry == false) {
71 return false;
72 }
73 final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
74 return
75 (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
76 (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
77 }
78
79 /**
80 * Gets a hashCode compatible with the equals method.
81 * <p>
82 * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
83 *
84 * @return a suitable hash code
85 */
86 @Override
87 public int hashCode() {
88 return (getKey() == null ? 0 : getKey().hashCode()) ^
89 (getValue() == null ? 0 : getValue().hashCode());
90 }
91
92 }