001: /*
002: * Copyright 2003-2006 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.keyvalue;
017:
018: import java.util.Map;
019:
020: import org.apache.commons.collections.KeyValue;
021:
022: /**
023: * A mutable <code>KeyValue</code> pair that does not implement
024: * {@link java.util.Map.Entry Map.Entry}.
025: * <p>
026: * Note that a <code>DefaultKeyValue</code> instance may not contain
027: * itself as a key or value.
028: *
029: * @since Commons Collections 3.0
030: * @version $Revision: 405927 $ $Date: 2006-05-12 23:57:03 +0100 (Fri, 12 May 2006) $
031: *
032: * @author James Strachan
033: * @author Michael A. Smith
034: * @author Neil O'Toole
035: * @author Stephen Colebourne
036: */
037: public class DefaultKeyValue extends AbstractKeyValue {
038:
039: /**
040: * Constructs a new pair with a null key and null value.
041: */
042: public DefaultKeyValue() {
043: super (null, null);
044: }
045:
046: /**
047: * Constructs a new pair with the specified key and given value.
048: *
049: * @param key the key for the entry, may be null
050: * @param value the value for the entry, may be null
051: */
052: public DefaultKeyValue(final Object key, final Object value) {
053: super (key, value);
054: }
055:
056: /**
057: * Constructs a new pair from the specified <code>KeyValue</code>.
058: *
059: * @param pair the pair to copy, must not be null
060: * @throws NullPointerException if the entry is null
061: */
062: public DefaultKeyValue(final KeyValue pair) {
063: super (pair.getKey(), pair.getValue());
064: }
065:
066: /**
067: * Constructs a new pair from the specified <code>Map.Entry</code>.
068: *
069: * @param entry the entry to copy, must not be null
070: * @throws NullPointerException if the entry is null
071: */
072: public DefaultKeyValue(final Map.Entry entry) {
073: super (entry.getKey(), entry.getValue());
074: }
075:
076: //-----------------------------------------------------------------------
077: /**
078: * Sets the key.
079: *
080: * @param key the new key
081: * @return the old key
082: * @throws IllegalArgumentException if key is this object
083: */
084: public Object setKey(final Object key) {
085: if (key == this ) {
086: throw new IllegalArgumentException(
087: "DefaultKeyValue may not contain itself as a key.");
088: }
089:
090: final Object old = this .key;
091: this .key = key;
092: return old;
093: }
094:
095: /**
096: * Sets the value.
097: *
098: * @return the old value of the value
099: * @param value the new value
100: * @throws IllegalArgumentException if value is this object
101: */
102: public Object setValue(final Object value) {
103: if (value == this ) {
104: throw new IllegalArgumentException(
105: "DefaultKeyValue may not contain itself as a value.");
106: }
107:
108: final Object old = this .value;
109: this .value = value;
110: return old;
111: }
112:
113: //-----------------------------------------------------------------------
114: /**
115: * Returns a new <code>Map.Entry</code> object with key and value from this pair.
116: *
117: * @return a MapEntry instance
118: */
119: public Map.Entry toMapEntry() {
120: return new DefaultMapEntry(this );
121: }
122:
123: //-----------------------------------------------------------------------
124: /**
125: * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
126: * <p>
127: * Returns true if the compared object is also a <code>DefaultKeyValue</code>,
128: * and its key and value are equal to this object's key and value.
129: *
130: * @param obj the object to compare to
131: * @return true if equal key and value
132: */
133: public boolean equals(final Object obj) {
134: if (obj == this ) {
135: return true;
136: }
137: if (obj instanceof DefaultKeyValue == false) {
138: return false;
139: }
140:
141: DefaultKeyValue other = (DefaultKeyValue) obj;
142: return (getKey() == null ? other.getKey() == null : getKey()
143: .equals(other.getKey()))
144: && (getValue() == null ? other.getValue() == null
145: : getValue().equals(other.getValue()));
146: }
147:
148: /**
149: * Gets a hashCode compatible with the equals method.
150: * <p>
151: * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()},
152: * however subclasses may override this.
153: *
154: * @return a suitable hash code
155: */
156: public int hashCode() {
157: return (getKey() == null ? 0 : getKey().hashCode())
158: ^ (getValue() == null ? 0 : getValue().hashCode());
159: }
160:
161: }
|