KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.collections.KeyValue;
21
22 /**
23  * A mutable KeyValue pair that does not implement MapEntry.
24  * <p>
25  * Note that a <code>DefaultKeyValue</code> instance may not contain
26  * itself as a key or value.
27  *
28  * @since Commons Collections 3.0
29  * @version $Revision: 1.4 $ $Date: 2004/02/18 01:00:08 $
30  *
31  * @author James Strachan
32  * @author Michael A. Smith
33  * @author Neil O'Toole
34  * @author Stephen Colebourne
35  */

36 public class DefaultKeyValue extends AbstractKeyValue {
37
38     /**
39      * Constructs a new pair with a null key and null value.
40      */

41     public DefaultKeyValue() {
42         super(null, null);
43     }
44
45     /**
46      * Constructs a new pair with the specified key and given value.
47      *
48      * @param key the key for the entry, may be null
49      * @param value the value for the entry, may be null
50      */

51     public DefaultKeyValue(final Object JavaDoc key, final Object JavaDoc value) {
52         super(key, value);
53     }
54
55     /**
56      * Constructs a new pair from the specified KeyValue.
57      *
58      * @param pair the pair to copy, must not be null
59      * @throws NullPointerException if the entry is null
60      */

61     public DefaultKeyValue(final KeyValue pair) {
62         super(pair.getKey(), pair.getValue());
63     }
64
65     /**
66      * Constructs a new pair from the specified MapEntry.
67      *
68      * @param entry the entry to copy, must not be null
69      * @throws NullPointerException if the entry is null
70      */

71     public DefaultKeyValue(final Map.Entry JavaDoc entry) {
72         super(entry.getKey(), entry.getValue());
73     }
74
75     //-----------------------------------------------------------------------
76
/**
77      * Sets the key.
78      *
79      * @param key the new key
80      * @return the old key
81      * @throws IllegalArgumentException if key is this object
82      */

83     public Object JavaDoc setKey(final Object JavaDoc key) {
84         if (key == this) {
85             throw new IllegalArgumentException JavaDoc("DefaultKeyValue may not contain itself as a key.");
86         }
87
88         final Object JavaDoc old = this.key;
89         this.key = key;
90         return old;
91     }
92
93     /**
94      * Sets the value.
95      *
96      * @return the old value of the value
97      * @param value the new value
98      * @throws IllegalArgumentException if value is this object
99      */

100     public Object JavaDoc setValue(final Object JavaDoc value) {
101         if (value == this) {
102             throw new IllegalArgumentException JavaDoc("DefaultKeyValue may not contain itself as a value.");
103         }
104
105         final Object JavaDoc old = this.value;
106         this.value = value;
107         return old;
108     }
109
110     //-----------------------------------------------------------------------
111
/**
112      * Returns a new <code>Map.Entry</code> object with key and value from this pair.
113      *
114      * @return a MapEntry instance
115      */

116     public Map.Entry JavaDoc toMapEntry() {
117         return new DefaultMapEntry(this);
118     }
119
120     //-----------------------------------------------------------------------
121
/**
122      * Compares this Map Entry with another Map Entry.
123      * <p>
124      * Returns true if the compared object is also a <code>DefaultKeyValue</code>,
125      * and its key and value are equal to this object's key and value.
126      *
127      * @param obj the object to compare to
128      * @return true if equal key and value
129      */

130     public boolean equals(final Object JavaDoc obj) {
131         if (obj == this) {
132             return true;
133         }
134         if (obj instanceof DefaultKeyValue == false) {
135             return false;
136         }
137
138         DefaultKeyValue other = (DefaultKeyValue) obj;
139         return
140             (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
141             (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
142     }
143
144     /**
145      * Gets a hashCode compatible with the equals method.
146      * <p>
147      * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()},
148      * however subclasses may override this.
149      *
150      * @return a suitable hash code
151      */

152     public int hashCode() {
153         return (getKey() == null ? 0 : getKey().hashCode()) ^
154                (getValue() == null ? 0 : getValue().hashCode());
155     }
156
157 }
158
Popular Tags