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