KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > keyvalue > AbstractMapEntry


1 /*
2  * Copyright 2003-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.keyvalue;
17
18 import java.util.Map JavaDoc;
19
20 /**
21  * Abstract Pair class to assist with creating correct Map Entry implementations.
22  *
23  * @since Commons Collections 3.0
24  * @version $Revision: 1.4 $ $Date: 2004/02/18 01:00:08 $
25  *
26  * @author James Strachan
27  * @author Michael A. Smith
28  * @author Neil O'Toole
29  * @author Stephen Colebourne
30  */

31 public abstract class AbstractMapEntry extends AbstractKeyValue implements Map.Entry JavaDoc {
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(Object JavaDoc key, Object JavaDoc 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 Object JavaDoc setValue(Object JavaDoc value) {
54         Object JavaDoc 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 JavaDoc obj) {
68         if (obj == this) {
69             return true;
70         }
71         if (obj instanceof Map.Entry JavaDoc == false) {
72             return false;
73         }
74         Map.Entry JavaDoc other = (Map.Entry JavaDoc) obj;
75         return
76             (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
77             (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
78     }
79      
80     /**
81      * Gets a hashCode compatible with the equals method.
82      * <p>
83      * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
84      *
85      * @return a suitable hash code
86      */

87     public int hashCode() {
88         return (getKey() == null ? 0 : getKey().hashCode()) ^
89                (getValue() == null ? 0 : getValue().hashCode());
90     }
91
92 }
93
Popular Tags