Add unmodifiable BidiMap decorator

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131400 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-12-03 12:59:37 +00:00
parent 7a58d2f1b8
commit 66ca5337e6
5 changed files with 426 additions and 5 deletions

View File

@ -0,0 +1,119 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bidimap/AbstractBidiMapDecorator.java,v 1.1 2003/12/03 12:59:37 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 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.bidimap;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.map.AbstractMapDecorator;
/**
* Provides a base decorator that enables additional functionality to be added
* to a BidiMap via decoration.
* <p>
* Methods are forwarded directly to the decorated map.
* <p>
* This implementation does not perform any special processing with the map views.
* Instead it simply returns the set/collection from the wrapped map. This may be
* undesirable, for example if you are trying to write a validating implementation
* it would provide a loophole around the validation.
* But, you might want that loophole, so this class is kept simple.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/12/03 12:59:37 $
*
* @author Stephen Colebourne
*/
public abstract class AbstractBidiMapDecorator extends AbstractMapDecorator implements BidiMap {
/**
* Constructor that wraps (not copies).
*
* @param map the map to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
public AbstractBidiMapDecorator(BidiMap map) {
super(map);
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected BidiMap getBidiMap() {
return (BidiMap) map;
}
//-----------------------------------------------------------------------
public MapIterator mapIterator() {
return getBidiMap().mapIterator();
}
public Object getKey(Object value) {
return getBidiMap().getKey(value);
}
public Object removeValue(Object value) {
return getBidiMap().removeValue(value);
}
public BidiMap inverseBidiMap() {
return getBidiMap().inverseBidiMap();
}
}

View File

@ -0,0 +1,159 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bidimap/UnmodifiableBidiMap.java,v 1.1 2003/12/03 12:59:37 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 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.bidimap;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.Unmodifiable;
import org.apache.commons.collections.collection.UnmodifiableCollection;
import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
import org.apache.commons.collections.map.UnmodifiableEntrySet;
import org.apache.commons.collections.set.UnmodifiableSet;
/**
* Decorates another <code>BidiMap</code> to ensure it can't be altered.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/12/03 12:59:37 $
*
* @author Stephen Colebourne
*/
public final class UnmodifiableBidiMap extends AbstractBidiMapDecorator implements Unmodifiable {
/** The inverse unmodifiable map */
private UnmodifiableBidiMap inverse;
/**
* Factory method to create an unmodifiable map.
*
* @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null
*/
public static BidiMap decorate(BidiMap map) {
if (map instanceof Unmodifiable) {
return map;
}
return new UnmodifiableBidiMap(map);
}
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
*
* @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null
*/
private UnmodifiableBidiMap(BidiMap map) {
super(map);
}
//-----------------------------------------------------------------------
public void clear() {
throw new UnsupportedOperationException();
}
public Object put(Object key, Object value) {
throw new UnsupportedOperationException();
}
public void putAll(Map mapToCopy) {
throw new UnsupportedOperationException();
}
public Object remove(Object key) {
throw new UnsupportedOperationException();
}
public Set entrySet() {
Set set = super.entrySet();
return UnmodifiableEntrySet.decorate(set);
}
public Set keySet() {
Set set = super.keySet();
return UnmodifiableSet.decorate(set);
}
public Collection values() {
Collection coll = super.values();
return UnmodifiableCollection.decorate(coll);
}
//-----------------------------------------------------------------------
public Object removeValue(Object value) {
throw new UnsupportedOperationException();
}
public MapIterator mapIterator() {
MapIterator it = getBidiMap().mapIterator();
return UnmodifiableMapIterator.decorate(it);
}
public BidiMap inverseBidiMap() {
if (inverse == null) {
inverse = new UnmodifiableBidiMap(getBidiMap().inverseBidiMap());
inverse.inverse = this;
}
return inverse;
}
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bidimap/AbstractTestBidiMap.java,v 1.5 2003/12/01 22:49:00 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bidimap/AbstractTestBidiMap.java,v 1.6 2003/12/03 12:59:36 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -72,7 +72,7 @@ import org.apache.commons.collections.map.AbstractTestMap;
/**
* Abstract test class for {@link BidiMap} methods and contracts.
*
* @version $Revision: 1.5 $ $Date: 2003/12/01 22:49:00 $
* @version $Revision: 1.6 $ $Date: 2003/12/03 12:59:36 $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -92,7 +92,7 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
new Object[] { "value2", "key2" },
new Object[] { "value3", "key3" }
};
private final Object[][] entries;
protected final Object[][] entries;
public AbstractTestBidiMap(String testName) {
super(testName);
@ -149,6 +149,8 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
// BidiPut
//-----------------------------------------------------------------------
public void testBidiPut() {
if (isPutAddSupported() == false || isPutChangeSupported() == false) return;
BidiMap map = makeEmptyBidiMap();
BidiMap inverse = map.inverseBidiMap();
assertEquals(0, map.size());
@ -275,6 +277,8 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
//-----------------------------------------------------------------------
public void testBidiClear() {
if (isRemoveSupported() == false) return;
BidiMap map = makeFullBidiMap();
map.clear();
assertTrue("Map was not cleared.", map.isEmpty());
@ -290,6 +294,8 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
//-----------------------------------------------------------------------
public void testBidiRemove() {
if (isRemoveSupported() == false) return;
remove(makeFullBidiMap(), entries[0][0]);
remove(makeFullBidiMap().inverseBidiMap(), entries[0][1]);
@ -325,6 +331,8 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
//-----------------------------------------------------------------------
public void testBidiRemoveByKeySet() {
if (isRemoveSupported() == false) return;
removeByKeySet(makeFullBidiMap(), entries[0][0], entries[0][1]);
removeByKeySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]);
}
@ -345,6 +353,8 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
//-----------------------------------------------------------------------
public void testBidiRemoveByEntrySet() {
if (isRemoveSupported() == false) return;
removeByEntrySet(makeFullBidiMap(), entries[0][0], entries[0][1]);
removeByEntrySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]);
}
@ -365,6 +375,7 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
!map.inverseBidiMap().containsKey(value));
}
//-----------------------------------------------------------------------
public BulkTest bulkTestMapEntrySet() {
return new TestBidiMapEntrySet();
}
@ -448,6 +459,9 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
public BidiMap makeFullBidiMap() {
return main.makeFullBidiMap().inverseBidiMap();
}
public Map makeFullMap() {
return ((BidiMap) main.makeFullMap()).inverseBidiMap();
}
public Object[] getSampleKeys() {
return main.getSampleValues();
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bidimap/TestAll.java,v 1.1 2003/11/16 20:35:46 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bidimap/TestAll.java,v 1.2 2003/12/03 12:59:36 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
* Entry point for tests.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/16 20:35:46 $
* @version $Revision: 1.2 $ $Date: 2003/12/03 12:59:36 $
*
* @author Stephen Colebourne
*/
@ -87,6 +87,8 @@ public class TestAll extends TestCase {
suite.addTest(TestDualTreeBidiMap.suite());
suite.addTest(TestTreeBidiMap.suite());
suite.addTest(TestUnmodifiableBidiMap.suite());
return suite;
}

View File

@ -0,0 +1,127 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bidimap/TestUnmodifiableBidiMap.java,v 1.1 2003/12/03 12:59:36 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.bidimap;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.textui.TestRunner;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.BulkTest;
/**
* JUnit tests.
*
* @version $Revision: 1.1 $ $Date: 2003/12/03 12:59:36 $
*
* @author Stephen Colebourne
*/
public class TestUnmodifiableBidiMap extends AbstractTestBidiMap {
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return BulkTest.makeSuite(TestUnmodifiableBidiMap.class);
}
public TestUnmodifiableBidiMap(String testName) {
super(testName);
}
public BidiMap makeEmptyBidiMap() {
return UnmodifiableBidiMap.decorate(new DualHashBidiMap());
}
public BidiMap makeFullBidiMap() {
BidiMap bidi = new DualHashBidiMap();
for (int i = 0; i < entries.length; i++) {
bidi.put(entries[i][0], entries[i][1]);
}
return UnmodifiableBidiMap.decorate(bidi);
}
public Map makeFullMap() {
BidiMap bidi = new DualHashBidiMap();
addSampleMappings(bidi);
return UnmodifiableBidiMap.decorate(bidi);
}
public Map makeConfirmedMap() {
return new HashMap();
}
/**
* Override to prevent infinite recursion of tests.
*/
public String[] ignoredTests() {
return new String[] {"TestUnmodifiableBidiMap.bulkTestInverseMap.bulkTestInverseMap"};
}
public boolean isPutAddSupported() {
return false;
}
public boolean isPutChangeSupported() {
return false;
}
public boolean isRemoveSupported() {
return false;
}
}