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 import org.apache.commons.collections.KeyValue;
22
23 /**
24 * A mutable <code>KeyValue</code> pair that does not implement
25 * {@link java.util.Map.Entry Map.Entry}.
26 * <p>
27 * Note that a <code>DefaultKeyValue</code> instance may not contain
28 * itself as a key or value.
29 *
30 * @since 3.0
31 * @version $Id: DefaultKeyValue.java 1429905 2013-01-07 17:15:14Z ggregory $
32 */
33 public class DefaultKeyValue<K, V> extends AbstractKeyValue<K, V> {
34
35 /**
36 * Constructs a new pair with a null key and null value.
37 */
38 public DefaultKeyValue() {
39 super(null, null);
40 }
41
42 /**
43 * Constructs a new pair with the specified key and given value.
44 *
45 * @param key the key for the entry, may be null
46 * @param value the value for the entry, may be null
47 */
48 public DefaultKeyValue(final K key, final V value) {
49 super(key, value);
50 }
51
52 /**
53 * Constructs a new pair from the specified <code>KeyValue</code>.
54 *
55 * @param pair the pair to copy, must not be null
56 * @throws NullPointerException if the entry is null
57 */
58 public DefaultKeyValue(final KeyValue<K, V> pair) {
59 super(pair.getKey(), pair.getValue());
60 }
61
62 /**
63 * Constructs a new pair from the specified <code>Map.Entry</code>.
64 *
65 * @param entry the entry to copy, must not be null
66 * @throws NullPointerException if the entry is null
67 */
68 public DefaultKeyValue(final Map.Entry<K, V> entry) {
69 super(entry.getKey(), entry.getValue());
70 }
71
72 //-----------------------------------------------------------------------
73 /**
74 * Sets the key.
75 *
76 * @param key the new key
77 * @return the old key
78 * @throws IllegalArgumentException if key is this object
79 */
80 public K setKey(final K key) {
81 if (key == this) {
82 throw new IllegalArgumentException("DefaultKeyValue may not contain itself as a key.");
83 }
84
85 final K old = this.key;
86 this.key = key;
87 return old;
88 }
89
90 /**
91 * Sets the value.
92 *
93 * @return the old value of the value
94 * @param value the new value
95 * @throws IllegalArgumentException if value is this object
96 */
97 public V setValue(final V value) {
98 if (value == this) {
99 throw new IllegalArgumentException("DefaultKeyValue may not contain itself as a value.");
100 }
101
102 final V old = this.value;
103 this.value = value;
104 return old;
105 }
106
107 //-----------------------------------------------------------------------
108 /**
109 * Returns a new <code>Map.Entry</code> object with key and value from this pair.
110 *
111 * @return a MapEntry instance
112 */
113 public Map.Entry<K, V> toMapEntry() {
114 return new DefaultMapEntry<K, V>(this);
115 }
116
117 //-----------------------------------------------------------------------
118 /**
119 * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
120 * <p>
121 * Returns true if the compared object is also a <code>DefaultKeyValue</code>,
122 * and its key and value are equal to this object's key and value.
123 *
124 * @param obj the object to compare to
125 * @return true if equal key and value
126 */
127 @Override
128 public boolean equals(final Object obj) {
129 if (obj == this) {
130 return true;
131 }
132 if (obj instanceof DefaultKeyValue == false) {
133 return false;
134 }
135
136 final DefaultKeyValue<?, ?> other = (DefaultKeyValue<?, ?>) obj;
137 return
138 (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
139 (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
140 }
141
142 /**
143 * Gets a hashCode compatible with the equals method.
144 * <p>
145 * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()},
146 * however subclasses may override this.
147 *
148 * @return a suitable hash code
149 */
150 @Override
151 public int hashCode() {
152 return (getKey() == null ? 0 : getKey().hashCode()) ^
153 (getValue() == null ? 0 : getValue().hashCode());
154 }
155
156 }