KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.commons.collections.KeyValue;
22
23 /**
24  * A Map Entry tied to a map underneath.
25  * <p>
26  * This can be used to enable a map entry to make changes on the underlying
27  * map, however this will probably mess up any iterators.
28  *
29  * @since Commons Collections 3.0
30  * @version $Revision: 1.5 $ $Date: 2004/04/09 14:35:10 $
31  *
32  * @author Stephen Colebourne
33  */

34 public class TiedMapEntry implements Map.Entry JavaDoc, KeyValue, Serializable JavaDoc {
35
36     /** Serialization version */
37     private static final long serialVersionUID = -8453869361373831205L;
38
39     /** The map underlying the entry/iterator */
40     private final Map JavaDoc map;
41     /** The key */
42     private final Object JavaDoc key;
43     
44     /**
45      * Constructs a new entry with the given Map and key.
46      *
47      * @param map the map
48      * @param key the key
49      */

50     public TiedMapEntry(Map JavaDoc map, Object JavaDoc key) {
51         super();
52         this.map = map;
53         this.key = key;
54     }
55
56     // Map.Entry interface
57
//-------------------------------------------------------------------------
58
/**
59      * Gets the key of this entry
60      *
61      * @return the key
62      */

63     public Object JavaDoc getKey() {
64         return key;
65     }
66
67     /**
68      * Gets the value of this entry direct from the map.
69      *
70      * @return the value
71      */

72     public Object JavaDoc getValue() {
73         return map.get(key);
74     }
75
76     /**
77      * Sets the value associated with the key direct onto the map.
78      *
79      * @param value the new value
80      * @return the old value
81      * @throws IllegalArgumentException if the value is set to this map entry
82      */

83     public Object JavaDoc setValue(Object JavaDoc value) {
84         if (value == this) {
85             throw new IllegalArgumentException JavaDoc("Cannot set value to this map entry");
86         }
87         return map.put(key, value);
88     }
89
90     /**
91      * Compares this Map Entry with another Map Entry.
92      * <p>
93      * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
94      *
95      * @param obj the object to compare to
96      * @return true if equal key and value
97      */

98     public boolean equals(Object JavaDoc obj) {
99         if (obj == this) {
100             return true;
101         }
102         if (obj instanceof Map.Entry JavaDoc == false) {
103             return false;
104         }
105         Map.Entry JavaDoc other = (Map.Entry JavaDoc) obj;
106         Object JavaDoc value = getValue();
107         return
108             (key == null ? other.getKey() == null : key.equals(other.getKey())) &&
109             (value == null ? other.getValue() == null : value.equals(other.getValue()));
110     }
111      
112     /**
113      * Gets a hashCode compatible with the equals method.
114      * <p>
115      * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
116      *
117      * @return a suitable hash code
118      */

119     public int hashCode() {
120         Object JavaDoc value = getValue();
121         return (getKey() == null ? 0 : getKey().hashCode()) ^
122                (value == null ? 0 : value.hashCode());
123     }
124     
125     /**
126      * Gets a string version of the entry.
127      *
128      * @return entry as a string
129      */

130     public String JavaDoc toString() {
131         return getKey() + "=" + getValue();
132     }
133
134 }
135
Popular Tags