Add TiedMapEntry
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131320 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7040e31845
commit
792b248c3c
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/pairs/Attic/TiedMapEntry.java,v 1.1 2003/11/02 19:45:48 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.pairs;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A Map Entry tied to a map underneath.
|
||||
* <p>
|
||||
* This can be used to enable a map entry to make changes on the underlying
|
||||
* map, however this will probably mess up any iterators.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/02 19:45:48 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TiedMapEntry implements Map.Entry, KeyValue {
|
||||
|
||||
/** The map underlying the entry/iterator */
|
||||
private final Map map;
|
||||
/** The key */
|
||||
private final Object key;
|
||||
|
||||
/**
|
||||
* Constructs a new entry with the given Map and key.
|
||||
*
|
||||
* @param map the map
|
||||
* @param key the key
|
||||
*/
|
||||
public TiedMapEntry(Map map, Object key) {
|
||||
super();
|
||||
this.map = map;
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
// Map.Entry interface
|
||||
//-------------------------------------------------------------------------
|
||||
/**
|
||||
* Gets the key of this entry
|
||||
*
|
||||
* @return the key
|
||||
*/
|
||||
public Object getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of this entry direct from the map.
|
||||
*
|
||||
* @return the value
|
||||
*/
|
||||
public Object getValue() {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value associated with the key direct onto the map.
|
||||
*
|
||||
* @param value the new value
|
||||
* @return the old value
|
||||
* @throws IllegalArgumentException if the value is set to this map entry
|
||||
*/
|
||||
public Object setValue(Object value) {
|
||||
if (value == this) {
|
||||
throw new IllegalArgumentException("Cannot set value to this map entry");
|
||||
}
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this Map Entry with another Map Entry.
|
||||
* <p>
|
||||
* Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
|
||||
*
|
||||
* @param obj the object to compare to
|
||||
* @return true if equal key and value
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Map.Entry == false) {
|
||||
return false;
|
||||
}
|
||||
Map.Entry other = (Map.Entry) obj;
|
||||
Object value = getValue();
|
||||
return
|
||||
(key == null ? other.getKey() == null : key.equals(other.getKey())) &&
|
||||
(value == null ? other.getValue() == null : value.equals(other.getValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a hashCode compatible with the equals method.
|
||||
* <p>
|
||||
* Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
|
||||
*
|
||||
* @return a suitable hashcode
|
||||
*/
|
||||
public int hashCode() {
|
||||
Object value = getValue();
|
||||
return (getKey() == null ? 0 : getKey().hashCode()) ^
|
||||
(value == null ? 0 : value.hashCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a string version of the entry.
|
||||
*
|
||||
* @return entry as a string
|
||||
*/
|
||||
public String toString() {
|
||||
return getKey() + "=" + getValue();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/pairs/Attic/TestAll.java,v 1.2 2003/11/02 17:06:59 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/pairs/Attic/TestAll.java,v 1.3 2003/11/02 19:45:48 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
|
|||
* Entry point for key-value test cases.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.2 $ $Date: 2003/11/02 17:06:59 $
|
||||
* @version $Revision: 1.3 $ $Date: 2003/11/02 19:45:48 $
|
||||
*
|
||||
* @author Neil O'Toole
|
||||
*/
|
||||
|
@ -85,6 +85,7 @@ public class TestAll extends TestCase {
|
|||
|
||||
suite.addTest(TestDefaultKeyValue.suite());
|
||||
suite.addTest(TestDefaultMapEntry.suite());
|
||||
suite.addTest(TestTiedMapEntry.suite());
|
||||
suite.addTest(TestUnmodifiableMapEntry.suite());
|
||||
return suite;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/pairs/Attic/TestTiedMapEntry.java,v 1.1 2003/11/02 19:45:48 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.pairs;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Test the TiedMapEntry class.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/02 19:45:48 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TestTiedMapEntry extends AbstractTestMapEntry {
|
||||
|
||||
public TestTiedMapEntry(String testName) {
|
||||
super(testName);
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(TestTiedMapEntry.class);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestTiedMapEntry.class);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Gets the instance to test
|
||||
*/
|
||||
public Map.Entry makeMapEntry(Object key, Object value) {
|
||||
Map map = new HashMap();
|
||||
map.put(key, value);
|
||||
return new TiedMapEntry(map, key);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Tests the constructors.
|
||||
*/
|
||||
public void testConstructors() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructors.
|
||||
*/
|
||||
public void testSetValue() {
|
||||
Map map = new HashMap();
|
||||
map.put("A", "a");
|
||||
map.put("B", "b");
|
||||
map.put("C", "c");
|
||||
Map.Entry entry = new TiedMapEntry(map, "A");
|
||||
assertSame("A", entry.getKey());
|
||||
assertSame("a", entry.getValue());
|
||||
assertSame("a", entry.setValue("x"));
|
||||
assertSame("A", entry.getKey());
|
||||
assertSame("x", entry.getValue());
|
||||
|
||||
entry = new TiedMapEntry(map, "B");
|
||||
assertSame("B", entry.getKey());
|
||||
assertSame("b", entry.getValue());
|
||||
assertSame("b", entry.setValue("y"));
|
||||
assertSame("B", entry.getKey());
|
||||
assertSame("y", entry.getValue());
|
||||
|
||||
entry = new TiedMapEntry(map, "C");
|
||||
assertSame("C", entry.getKey());
|
||||
assertSame("c", entry.getValue());
|
||||
assertSame("c", entry.setValue("z"));
|
||||
assertSame("C", entry.getKey());
|
||||
assertSame("z", entry.getValue());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue