001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.collections.keyvalue;
018
019 import java.util.Map;
020
021 /**
022 * Abstract Pair class to assist with creating correct
023 * {@link java.util.Map.Entry Map.Entry} implementations.
024 *
025 * @since Commons Collections 3.0
026 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
027 *
028 * @author James Strachan
029 * @author Michael A. Smith
030 * @author Neil O'Toole
031 * @author Stephen Colebourne
032 */
033 public abstract class AbstractMapEntry extends AbstractKeyValue implements Map.Entry {
034
035 /**
036 * Constructs a new entry with the given key and given value.
037 *
038 * @param key the key for the entry, may be null
039 * @param value the value for the entry, may be null
040 */
041 protected AbstractMapEntry(Object key, Object value) {
042 super(key, value);
043 }
044
045 // Map.Entry interface
046 //-------------------------------------------------------------------------
047 /**
048 * Sets the value stored in this <code>Map.Entry</code>.
049 * <p>
050 * This <code>Map.Entry</code> is not connected to a Map, so only the
051 * local data is changed.
052 *
053 * @param value the new value
054 * @return the previous value
055 */
056 public Object setValue(Object value) {
057 Object answer = this.value;
058 this.value = value;
059 return answer;
060 }
061
062 /**
063 * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
064 * <p>
065 * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
066 *
067 * @param obj the object to compare to
068 * @return true if equal key and value
069 */
070 public boolean equals(Object obj) {
071 if (obj == this) {
072 return true;
073 }
074 if (obj instanceof Map.Entry == false) {
075 return false;
076 }
077 Map.Entry other = (Map.Entry) obj;
078 return
079 (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
080 (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
081 }
082
083 /**
084 * Gets a hashCode compatible with the equals method.
085 * <p>
086 * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
087 *
088 * @return a suitable hash code
089 */
090 public int hashCode() {
091 return (getKey() == null ? 0 : getKey().hashCode()) ^
092 (getValue() == null ? 0 : getValue().hashCode());
093 }
094
095 }