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