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;
018
019 import java.util.Map;
020
021 /**
022 * A default implementation of {@link java.util.Map.Entry}
023 *
024 * @deprecated Use the version in the keyvalue subpackage. Will be removed in v4.0
025 * @since Commons Collections 1.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 class DefaultMapEntry implements Map.Entry, KeyValue {
034
035 /** The key */
036 private Object key;
037 /** The value */
038 private Object value;
039
040 /**
041 * Constructs a new <code>DefaultMapEntry</code> with a null key
042 * and null value.
043 */
044 public DefaultMapEntry() {
045 super();
046 }
047
048 /**
049 * Constructs a new <code>DefaultMapEntry</code> with the given
050 * key and given value.
051 *
052 * @param entry the entry to copy, must not be null
053 * @throws NullPointerException if the entry is null
054 */
055 public DefaultMapEntry(Map.Entry entry) {
056 super();
057 this.key = entry.getKey();
058 this.value = entry.getValue();
059 }
060
061 /**
062 * Constructs a new <code>DefaultMapEntry</code> with the given
063 * key and given value.
064 *
065 * @param key the key for the entry, may be null
066 * @param value the value for the entry, may be null
067 */
068 public DefaultMapEntry(Object key, Object value) {
069 super();
070 this.key = key;
071 this.value = value;
072 }
073
074 // Map.Entry interface
075 //-------------------------------------------------------------------------
076 /**
077 * Gets the key from the Map Entry.
078 *
079 * @return the key
080 */
081 public Object getKey() {
082 return key;
083 }
084
085 /**
086 * Sets the key stored in this Map Entry.
087 * <p>
088 * This Map Entry is not connected to a Map, so only the local data is changed.
089 *
090 * @param key the new key
091 */
092 public void setKey(Object key) {
093 this.key = key;
094 }
095
096 /**
097 * Gets the value from the Map Entry.
098 *
099 * @return the value
100 */
101 public Object getValue() {
102 return value;
103 }
104
105 /**
106 * Sets the value stored in this Map Entry.
107 * <p>
108 * This Map Entry is not connected to a Map, so only the local data is changed.
109 *
110 * @param value the new value
111 * @return the previous value
112 */
113 public Object setValue(Object value) {
114 Object answer = this.value;
115 this.value = value;
116 return answer;
117 }
118
119 // Basics
120 //-----------------------------------------------------------------------
121 /**
122 * Compares this Map Entry with another Map Entry.
123 * <p>
124 * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
125 *
126 * @param obj the object to compare to
127 * @return true if equal key and value
128 */
129 public boolean equals(Object obj) {
130 if (obj == this) {
131 return true;
132 }
133 if (obj instanceof Map.Entry == false) {
134 return false;
135 }
136 Map.Entry other = (Map.Entry) 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 *
147 * @return a suitable hash code
148 */
149 public int hashCode() {
150 return (getKey() == null ? 0 : getKey().hashCode()) ^
151 (getValue() == null ? 0 : getValue().hashCode());
152 }
153
154 /**
155 * Written to match the output of the Map.Entry's used in
156 * a {@link java.util.HashMap}.
157 * @since 3.0
158 */
159 public String toString() {
160 return ""+getKey()+"="+getValue();
161 }
162
163 }