001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.util.Map;
019:
020: /**
021: * A default implementation of {@link java.util.Map.Entry}
022: *
023: * @deprecated Use the version in the keyvalue subpackage. Will be removed in v4.0
024: * @since Commons Collections 1.0
025: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
026: *
027: * @author James Strachan
028: * @author Michael A. Smith
029: * @author Neil O'Toole
030: * @author Stephen Colebourne
031: */
032: public class DefaultMapEntry implements Map.Entry, KeyValue {
033:
034: /** The key */
035: private Object key;
036: /** The value */
037: private Object value;
038:
039: /**
040: * Constructs a new <code>DefaultMapEntry</code> with a null key
041: * and null value.
042: */
043: public DefaultMapEntry() {
044: super ();
045: }
046:
047: /**
048: * Constructs a new <code>DefaultMapEntry</code> with the given
049: * key and given value.
050: *
051: * @param entry the entry to copy, must not be null
052: * @throws NullPointerException if the entry is null
053: */
054: public DefaultMapEntry(Map.Entry entry) {
055: super ();
056: this .key = entry.getKey();
057: this .value = entry.getValue();
058: }
059:
060: /**
061: * Constructs a new <code>DefaultMapEntry</code> with the given
062: * key and given value.
063: *
064: * @param key the key for the entry, may be null
065: * @param value the value for the entry, may be null
066: */
067: public DefaultMapEntry(Object key, Object value) {
068: super ();
069: this .key = key;
070: this .value = value;
071: }
072:
073: // Map.Entry interface
074: //-------------------------------------------------------------------------
075: /**
076: * Gets the key from the Map Entry.
077: *
078: * @return the key
079: */
080: public Object getKey() {
081: return key;
082: }
083:
084: /**
085: * Sets the key stored in this Map Entry.
086: * <p>
087: * This Map Entry is not connected to a Map, so only the local data is changed.
088: *
089: * @param key the new key
090: */
091: public void setKey(Object key) {
092: this .key = key;
093: }
094:
095: /**
096: * Gets the value from the Map Entry.
097: *
098: * @return the value
099: */
100: public Object getValue() {
101: return value;
102: }
103:
104: /**
105: * Sets the value stored in this Map Entry.
106: * <p>
107: * This Map Entry is not connected to a Map, so only the local data is changed.
108: *
109: * @param value the new value
110: * @return the previous value
111: */
112: public Object setValue(Object value) {
113: Object answer = this .value;
114: this .value = value;
115: return answer;
116: }
117:
118: // Basics
119: //-----------------------------------------------------------------------
120: /**
121: * Compares this Map Entry with another Map Entry.
122: * <p>
123: * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
124: *
125: * @param obj the object to compare to
126: * @return true if equal key and value
127: */
128: public boolean equals(Object obj) {
129: if (obj == this ) {
130: return true;
131: }
132: if (obj instanceof Map.Entry == false) {
133: return false;
134: }
135: Map.Entry other = (Map.Entry) obj;
136: return (getKey() == null ? other.getKey() == null : getKey()
137: .equals(other.getKey()))
138: && (getValue() == null ? other.getValue() == null
139: : 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: }
|