KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > DefaultMapEntry


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.Map JavaDoc;
19
20 /**
21  * A default implementation of {@link java.util.Map.Entry}
22  *
23  * @deprecated Use the version in the keyvalue subpackage. Will be removed in v4.0
24  * @since Commons Collections 1.0
25  * @version $Revision: 1.21 $ $Date: 2004/02/18 01:15:42 $
26  *
27  * @author James Strachan
28  * @author Michael A. Smith
29  * @author Neil O'Toole
30  * @author Stephen Colebourne
31  */

32 public class DefaultMapEntry implements Map.Entry JavaDoc, KeyValue {
33     
34     /** The key */
35     private Object JavaDoc key;
36     /** The value */
37     private Object JavaDoc value;
38     
39     /**
40      * Constructs a new <code>DefaultMapEntry</code> with a null key
41      * and null value.
42      */

43     public DefaultMapEntry() {
44         super();
45     }
46
47     /**
48      * Constructs a new <code>DefaultMapEntry</code> with the given
49      * key and given value.
50      *
51      * @param entry the entry to copy, must not be null
52      * @throws NullPointerException if the entry is null
53      */

54     public DefaultMapEntry(Map.Entry JavaDoc entry) {
55         super();
56         this.key = entry.getKey();
57         this.value = entry.getValue();
58     }
59
60     /**
61      * Constructs a new <code>DefaultMapEntry</code> with the given
62      * key and given value.
63      *
64      * @param key the key for the entry, may be null
65      * @param value the value for the entry, may be null
66      */

67     public DefaultMapEntry(Object JavaDoc key, Object JavaDoc value) {
68         super();
69         this.key = key;
70         this.value = value;
71     }
72
73     // Map.Entry interface
74
//-------------------------------------------------------------------------
75
/**
76      * Gets the key from the Map Entry.
77      *
78      * @return the key
79      */

80     public Object JavaDoc getKey() {
81         return key;
82     }
83
84     /**
85      * Sets the key stored in this Map Entry.
86      * <p>
87      * This Map Entry is not connected to a Map, so only the local data is changed.
88      *
89      * @param key the new key
90      */

91     public void setKey(Object JavaDoc key) {
92         this.key = key;
93     }
94     
95     /**
96      * Gets the value from the Map Entry.
97      *
98      * @return the value
99      */

100     public Object JavaDoc 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 JavaDoc setValue(Object JavaDoc value) {
113         Object JavaDoc 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 JavaDoc obj) {
129         if (obj == this) {
130             return true;
131         }
132         if (obj instanceof Map.Entry JavaDoc == false) {
133             return false;
134         }
135         Map.Entry JavaDoc other = (Map.Entry JavaDoc) obj;
136         return
137             (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
138             (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
139     }
140      
141     /**
142      * Gets a hashCode compatible with the equals method.
143      * <p>
144      * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
145      *
146      * @return a suitable hash code
147      */

148     public int hashCode() {
149         return (getKey() == null ? 0 : getKey().hashCode()) ^
150                (getValue() == null ? 0 : getValue().hashCode());
151     }
152
153     /**
154      * Written to match the output of the Map.Entry's used in
155      * a {@link java.util.HashMap}.
156      * @since 3.0
157      */

158     public String JavaDoc toString() {
159         return ""+getKey()+"="+getValue();
160     }
161
162 }
163
Popular Tags