Add DualTreeBidiMap implementation and tests

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131307 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-10-31 01:26:25 +00:00
parent 27a00be12e
commit 869d20576b
7 changed files with 799 additions and 21 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractDualBidiMap.java,v 1.4 2003/10/29 00:06:25 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractDualBidiMap.java,v 1.5 2003/10/31 01:26:25 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -73,7 +73,7 @@ import org.apache.commons.collections.decorators.AbstractMapEntryDecorator;
* <code>createMap</code> method.
*
* @since Commons Collections 3.0
* @version $Id: AbstractDualBidiMap.java,v 1.4 2003/10/29 00:06:25 scolebourne Exp $
* @version $Id: AbstractDualBidiMap.java,v 1.5 2003/10/31 01:26:25 scolebourne Exp $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -93,6 +93,10 @@ public abstract class AbstractDualBidiMap implements BidiMap {
* View of the keys.
*/
protected transient Set keySet = null;
/**
* View of the values.
*/
protected transient Collection values = null;
/**
* View of the entries.
*/
@ -248,7 +252,10 @@ public abstract class AbstractDualBidiMap implements BidiMap {
}
public Collection values() {
return inverseBidiMap().keySet();
if (values == null) {
values = new Values(this);
}
return values;
}
public Set entrySet() {
@ -304,12 +311,13 @@ public abstract class AbstractDualBidiMap implements BidiMap {
}
return modified;
}
public void clear() {
map.clear();
}
}
//-----------------------------------------------------------------------
/**
* Inner class KeySet.
*/
@ -323,8 +331,12 @@ public abstract class AbstractDualBidiMap implements BidiMap {
return new KeySetIterator(super.iterator(), map);
}
public boolean contains(Object key) {
return map.maps[0].containsKey(key);
}
public boolean remove(Object key) {
if (contains(key)) {
if (map.maps[0].containsKey(key)) {
Object value = map.maps[0].remove(key);
map.maps[1].remove(value);
return true;
@ -339,7 +351,7 @@ public abstract class AbstractDualBidiMap implements BidiMap {
protected static class KeySetIterator extends AbstractIteratorDecorator {
private final AbstractDualBidiMap map;
private Object last = null;
private Object lastKey = null;
private boolean canRemove = false;
protected KeySetIterator(Iterator iterator, AbstractDualBidiMap map) {
@ -348,23 +360,83 @@ public abstract class AbstractDualBidiMap implements BidiMap {
}
public Object next() {
last = super.next();
lastKey = super.next();
canRemove = true;
return last;
return lastKey;
}
public void remove() {
if (canRemove == false) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
Object value = map.maps[0].get(last);
Object value = map.maps[0].get(lastKey);
super.remove();
map.maps[1].remove(value);
last = null;
lastKey = null;
canRemove = false;
}
}
//-----------------------------------------------------------------------
/**
* Inner class Values.
*/
protected static class Values extends View implements Set {
protected Values(AbstractDualBidiMap map) {
super(map.maps[0].values(), map);
}
public Iterator iterator() {
return new ValuesIterator(super.iterator(), map);
}
public boolean contains(Object value) {
return map.maps[1].containsKey(value);
}
public boolean remove(Object value) {
if (map.maps[1].containsKey(value)) {
Object key = map.maps[1].remove(value);
map.maps[0].remove(key);
return true;
}
return false;
}
}
/**
* Inner class ValuesIterator.
*/
protected static class ValuesIterator extends AbstractIteratorDecorator {
private final AbstractDualBidiMap map;
private Object lastValue = null;
private boolean canRemove = false;
protected ValuesIterator(Iterator iterator, AbstractDualBidiMap map) {
super(iterator);
this.map = map;
}
public Object next() {
lastValue = super.next();
canRemove = true;
return lastValue;
}
public void remove() {
if (canRemove == false) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
super.remove(); // removes from maps[0]
map.maps[1].remove(lastValue);
lastValue = null;
canRemove = false;
}
}
//-----------------------------------------------------------------------
/**
* Inner class EntrySet.
*/

View File

@ -0,0 +1,190 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/DualTreeBidiMap.java,v 1.1 2003/10/31 01:26:25 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002-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;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.collections.decorators.AbstractSortedMapDecorator;
/**
* Implementation of <code>BidiMap</code> that uses two <code>TreeMap</code> instances.
*
* @since Commons Collections 3.0
* @version $Id: DualTreeBidiMap.java,v 1.1 2003/10/31 01:26:25 scolebourne Exp $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
*/
public class DualTreeBidiMap extends AbstractDualBidiMap implements SortedBidiMap {
/**
* Creates an empty <code>DualTreeBidiMap</code>
*/
public DualTreeBidiMap() {
super();
}
/**
* Constructs a <code>DualTreeBidiMap</code> and copies the mappings from
* specified <code>Map</code>.
*
* @param map the map whose mappings are to be placed in this map
*/
public DualTreeBidiMap(Map map) {
super();
putAll(map);
}
/**
* Constructs a <code>HashBidiMap</code> that decorates the specified maps.
*
* @param normalMap the normal direction map
* @param reverseMap the reverse direction map
* @param inverseBidiMap the inverse BidiMap
*/
protected DualTreeBidiMap(Map normalMap, Map reverseMap, BidiMap inverseBidiMap) {
super(normalMap, reverseMap, inverseBidiMap);
}
/**
* Creates a new instance of the map used by the subclass to store data.
*
* @return the map to be used for internal storage
*/
protected Map createMap() {
return new TreeMap();
}
/**
* Creates a new instance of this object.
*
* @param normalMap the normal direction map
* @param reverseMap the reverse direction map
* @param inverseBidiMap the inverse BidiMap
* @return new bidi map
*/
protected BidiMap createBidiMap(Map normalMap, Map reverseMap, BidiMap inverseMap) {
return new DualTreeBidiMap(normalMap, reverseMap, inverseMap);
}
// SortedBidiMap
//-----------------------------------------------------------------------
public SortedBidiMap inverseSortedBidiMap() {
return (SortedBidiMap) inverseBidiMap();
}
// SortedMap
//-----------------------------------------------------------------------
public Comparator comparator() {
return ((SortedMap) maps[0]).comparator();
}
public Object firstKey() {
return ((SortedMap) maps[0]).firstKey();
}
public Object lastKey() {
return ((SortedMap) maps[0]).lastKey();
}
//-----------------------------------------------------------------------
public SortedMap headMap(Object toKey) {
SortedMap sub = ((SortedMap) maps[0]).headMap(toKey);
return new ViewMap(this, sub);
}
public SortedMap tailMap(Object fromKey) {
SortedMap sub = ((SortedMap) maps[0]).tailMap(fromKey);
return new ViewMap(this, sub);
}
public SortedMap subMap(Object fromKey, Object toKey) {
SortedMap sub = ((SortedMap) maps[0]).subMap(fromKey, toKey);
return new ViewMap(this, sub);
}
protected static class ViewMap extends AbstractSortedMapDecorator {
final DualTreeBidiMap bidi;
protected ViewMap(DualTreeBidiMap bidi, SortedMap sm) {
super((SortedMap) bidi.createBidiMap(sm, bidi.maps[1], bidi.inverseBidiMap));
this.bidi = (DualTreeBidiMap) map;
}
public boolean containsValue(Object value) {
// override as default implementation jumps to [1]
return bidi.maps[0].containsValue(value);
}
public SortedMap headMap(Object toKey) {
return new ViewMap(bidi, super.headMap(toKey));
}
public SortedMap tailMap(Object fromKey) {
return new ViewMap(bidi, super.tailMap(fromKey));
}
public SortedMap subMap(Object fromKey, Object toKey) {
return new ViewMap(bidi, super.subMap(fromKey, toKey));
}
}
}

View File

@ -0,0 +1,419 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/AbstractTestSortedBidiMap.java,v 1.1 2003/10/31 01:26:25 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.commons.collections.pairs.DefaultMapEntry;
/**
* Abstract test class for {@link BidiMap} methods and contracts.
*
* @version $Revision: 1.1 $ $Date: 2003/10/31 01:26:25 $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
*/
public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
protected List sortedKeys = new ArrayList();
protected List sortedValues = new ArrayList();
protected SortedSet sortedNewValues = new TreeSet();
public AbstractTestSortedBidiMap(String testName) {
super(testName);
sortedKeys.addAll(Arrays.asList(getSampleKeys()));
Collections.sort(sortedKeys);
sortedKeys = Collections.unmodifiableList(sortedKeys);
Map map = new TreeMap();
for (int i = 0; i < getSampleKeys().length; i++) {
map.put(getSampleKeys()[i], getSampleValues()[i]);
}
sortedValues.addAll(map.values());
sortedValues = Collections.unmodifiableList(sortedValues);
sortedNewValues.addAll(Arrays.asList(getNewSampleValues()));
}
public AbstractTestSortedBidiMap() {
super();
sortedKeys.addAll(Arrays.asList(getSampleValues()));
Collections.sort(sortedKeys);
sortedKeys = Collections.unmodifiableList(sortedKeys);
Map map = new TreeMap();
for (int i = 0; i < getSampleKeys().length; i++) {
map.put(getSampleValues()[i], getSampleKeys()[i]);
}
sortedValues.addAll(map.values());
sortedValues = Collections.unmodifiableList(sortedValues);
sortedNewValues.addAll(Arrays.asList(getNewSampleValues()));
}
//-----------------------------------------------------------------------
protected boolean isAllowNullKey() {
return false;
}
protected boolean isAllowNullValue() {
return false;
}
protected Map makeConfirmedMap() {
return new TreeMap();
}
//-----------------------------------------------------------------------
public void testFirstKey() {
SortedMap sm = (SortedMap) makeFullMap();
assertSame(sm.keySet().iterator().next(), sm.firstKey());
}
public void testLastKey() {
SortedMap sm = (SortedMap) makeFullMap();
Object obj = null;
for (Iterator it = sm.keySet().iterator(); it.hasNext();) {
obj = (Object) it.next();
}
assertSame(obj, sm.lastKey());
}
//-----------------------------------------------------------------------
public void testBidiHeadMapContains() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
Object first = it.next();
Object toKey = it.next();
Object second = it.next();
Object firstValue = sm.get(first);
Object secondValue = sm.get(second);
SortedMap head = sm.headMap(toKey);
assertEquals(1, head.size());
assertEquals(true, sm.containsKey(first));
assertEquals(true, head.containsKey(first));
assertEquals(true, sm.containsValue(firstValue));
assertEquals(true, head.containsValue(firstValue));
assertEquals(true, sm.containsKey(second));
assertEquals(false, head.containsKey(second));
assertEquals(true, sm.containsValue(secondValue));
assertEquals(false, head.containsValue(secondValue));
}
//-----------------------------------------------------------------------
public void testBidiRemoveByHeadMap() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
SortedMap head = sm.headMap(toKey);
assertEquals(2, head.size());
assertEquals(true, sm.containsKey(first));
assertEquals(true, head.containsKey(first));
assertEquals(true, sm.containsKey(second));
assertEquals(true, head.containsKey(second));
Object firstValue = head.remove(first);
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(false, sm.inverseBidiMap().containsValue(first));
assertEquals(false, head.containsKey(first));
assertEquals(false, head.containsValue(firstValue));
assertEquals(1, head.size());
Object secondValue = head.remove(second);
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, head.containsKey(second));
assertEquals(false, head.containsValue(secondValue));
assertEquals(0, head.size());
}
//-----------------------------------------------------------------------
public void testBidiRemoveByHeadMapEntrySet() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
it.next();
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
SortedMap head = sm.headMap(toKey);
Set set = head.entrySet();
Iterator it2 = set.iterator();
Object fromEntry = it2.next();
Map.Entry firstEntry = new DefaultMapEntry((Map.Entry) it2.next());
Map.Entry secondEntry = new DefaultMapEntry((Map.Entry) it2.next());
assertEquals(true, sm.containsKey(first));
assertEquals(true, head.containsKey(first));
assertEquals(true, set.contains(firstEntry));
assertEquals(true, sm.containsKey(second));
assertEquals(true, head.containsKey(second));
assertEquals(true, set.contains(secondEntry));
set.remove(firstEntry);
assertEquals(false, sm.containsKey(firstEntry.getKey()));
assertEquals(false, sm.containsValue(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(firstEntry.getKey()));
assertEquals(false, head.containsKey(firstEntry.getKey()));
assertEquals(false, head.containsValue(firstEntry.getValue()));
assertEquals(false, set.contains(firstEntry));
set.remove(secondEntry);
assertEquals(false, sm.containsKey(secondEntry.getKey()));
assertEquals(false, sm.containsValue(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(secondEntry.getKey()));
assertEquals(false, head.containsKey(secondEntry.getKey()));
assertEquals(false, head.containsValue(secondEntry.getValue()));
assertEquals(false, set.contains(secondEntry));
}
//-----------------------------------------------------------------------
public void testBidiRemoveByTailMap() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
it.next();
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
SortedMap tail = sm.tailMap(fromKey);
assertEquals(true, sm.containsKey(first));
assertEquals(true, tail.containsKey(first));
assertEquals(true, sm.containsKey(second));
assertEquals(true, tail.containsKey(second));
Object firstValue = tail.remove(first);
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(false, sm.inverseBidiMap().containsValue(first));
assertEquals(false, tail.containsKey(first));
assertEquals(false, tail.containsValue(firstValue));
Object secondValue = tail.remove(second);
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, tail.containsKey(second));
assertEquals(false, tail.containsValue(secondValue));
}
//-----------------------------------------------------------------------
public void testBidiRemoveByTailMapEntrySet() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
it.next();
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
SortedMap tail = sm.tailMap(fromKey);
Set set = tail.entrySet();
Iterator it2 = set.iterator();
Object fromEntry = it2.next();
Map.Entry firstEntry = new DefaultMapEntry((Map.Entry) it2.next());
Map.Entry secondEntry = new DefaultMapEntry((Map.Entry) it2.next());
assertEquals(true, sm.containsKey(first));
assertEquals(true, tail.containsKey(first));
assertEquals(true, set.contains(firstEntry));
assertEquals(true, sm.containsKey(second));
assertEquals(true, tail.containsKey(second));
assertEquals(true, set.contains(secondEntry));
set.remove(firstEntry);
assertEquals(false, sm.containsKey(firstEntry.getKey()));
assertEquals(false, sm.containsValue(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(firstEntry.getKey()));
assertEquals(false, tail.containsKey(firstEntry.getKey()));
assertEquals(false, tail.containsValue(firstEntry.getValue()));
assertEquals(false, set.contains(firstEntry));
set.remove(secondEntry);
assertEquals(false, sm.containsKey(secondEntry.getKey()));
assertEquals(false, sm.containsValue(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(secondEntry.getKey()));
assertEquals(false, tail.containsKey(secondEntry.getKey()));
assertEquals(false, tail.containsValue(secondEntry.getValue()));
assertEquals(false, set.contains(secondEntry));
}
//-----------------------------------------------------------------------
public void testBidiRemoveBySubMap() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
it.next();
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
SortedMap sub = sm.subMap(fromKey, toKey);
assertEquals(true, sm.containsKey(first));
assertEquals(true, sub.containsKey(first));
assertEquals(true, sm.containsKey(second));
assertEquals(true, sub.containsKey(second));
Object firstValue = sub.remove(first);
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(false, sm.inverseBidiMap().containsValue(first));
assertEquals(false, sub.containsKey(first));
assertEquals(false, sub.containsValue(firstValue));
Object secondValue = sub.remove(second);
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, sub.containsKey(second));
assertEquals(false, sub.containsValue(secondValue));
}
//-----------------------------------------------------------------------
public void testBidiRemoveBySubMapEntrySet() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
it.next();
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
SortedMap sub = sm.subMap(fromKey, toKey);
Set set = sub.entrySet();
assertEquals(3, set.size());
Iterator it2 = set.iterator();
Object fromEntry = it2.next();
Map.Entry firstEntry = new DefaultMapEntry((Map.Entry) it2.next());
Map.Entry secondEntry = new DefaultMapEntry((Map.Entry) it2.next());
assertEquals(true, sm.containsKey(first));
assertEquals(true, sub.containsKey(first));
assertEquals(true, set.contains(firstEntry));
assertEquals(true, sm.containsKey(second));
assertEquals(true, sub.containsKey(second));
assertEquals(true, set.contains(secondEntry));
set.remove(firstEntry);
assertEquals(false, sm.containsKey(firstEntry.getKey()));
assertEquals(false, sm.containsValue(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(firstEntry.getKey()));
assertEquals(false, sub.containsKey(firstEntry.getKey()));
assertEquals(false, sub.containsValue(firstEntry.getValue()));
assertEquals(false, set.contains(firstEntry));
set.remove(secondEntry);
assertEquals(false, sm.containsKey(secondEntry.getKey()));
assertEquals(false, sm.containsValue(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(secondEntry.getKey()));
assertEquals(false, sub.containsKey(secondEntry.getKey()));
assertEquals(false, sub.containsValue(secondEntry.getValue()));
assertEquals(false, set.contains(secondEntry));
}
//-----------------------------------------------------------------------
public BulkTest bulkTestHeadMap() {
return new AbstractTestSortedMap.TestHeadMap(this);
}
public BulkTest bulkTestTailMap() {
return new AbstractTestSortedMap.TestTailMap(this);
}
public BulkTest bulkTestSubMap() {
return new AbstractTestSortedMap.TestSubMap(this);
}
}

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/TestAll.java,v 1.51 2003/10/28 18:56:12 ggregory Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.52 2003/10/31 01:26:25 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
/**
* Entry point for all Collections package tests.
*
* @version $Revision: 1.51 $ $Date: 2003/10/28 18:56:12 $
* @version $Revision: 1.52 $ $Date: 2003/10/31 01:26:25 $
*
* @author Rodney Waldhoff
* @author Stephen Colebourne
@ -96,6 +96,8 @@ public class TestAll extends TestCase {
suite.addTest(TestCommonsLinkedList.suite());
suite.addTest(TestCursorableLinkedList.suite());
suite.addTest(TestDoubleOrderedMap.suite());
suite.addTest(TestDualHashBidiMap.suite());
suite.addTest(TestDualTreeBidiMap.suite());
suite.addTest(TestExtendedProperties.suite());
suite.addTest(TestFastArrayList.suite());
suite.addTest(TestFastArrayList1.suite());
@ -104,7 +106,6 @@ public class TestAll extends TestCase {
suite.addTest(TestFastTreeMap.suite());
suite.addTest(TestFastTreeMap1.suite());
suite.addTest(TestHashBag.suite());
suite.addTest(TestDualHashBidiMap.suite());
suite.addTest(TestIteratorUtils.suite());
suite.addTest(TestLRUMap.suite());
suite.addTest(TestMultiHashMap.suite());

View File

@ -0,0 +1,96 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestDualTreeBidiMap.java,v 1.1 2003/10/31 01:26:25 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;
import junit.framework.Test;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Revision: 1.1 $ $Date: 2003/10/31 01:26:25 $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
*/
public class TestDualTreeBidiMap extends AbstractTestSortedBidiMap {
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return BulkTest.makeSuite(TestDualTreeBidiMap.class);
}
public TestDualTreeBidiMap(String testName) {
super(testName);
}
protected BidiMap makeEmptyBidiMap() {
return new DualTreeBidiMap();
}
/**
* Override to prevent infinite recursion of tests.
*/
protected String[] ignoredTests() {
return new String[] {"TestDualTreeBidiMap.bulkTestInverseMap.bulkTestInverseMap"};
}
}

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/decorators/Attic/TestFixedSizeSortedMap.java,v 1.5 2003/10/07 22:20:58 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestFixedSizeSortedMap.java,v 1.6 2003/10/31 01:26:25 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -62,16 +62,16 @@ import java.util.SortedMap;
import java.util.TreeMap;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.AbstractTestSortedMap;
import org.apache.commons.collections.BulkTest;
/**
* Extension of {@link TestSortedMap} for exercising the {@link FixedSizeSortedMap}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.5 $ $Date: 2003/10/07 22:20:58 $
* @version $Revision: 1.6 $ $Date: 2003/10/31 01:26:25 $
*
* @author Stephen Colebourne
*/
@ -82,7 +82,7 @@ public class TestFixedSizeSortedMap extends AbstractTestSortedMap {
}
public static Test suite() {
return new TestSuite(TestFixedSizeSortedMap.class);
return BulkTest.makeSuite(TestFixedSizeSortedMap.class);
}
public static void main(String args[]) {

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/decorators/Attic/TestTransformedSortedMap.java,v 1.4 2003/10/06 23:44:23 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestTransformedSortedMap.java,v 1.5 2003/10/31 01:26:25 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -62,16 +62,16 @@ import java.util.Set;
import java.util.TreeMap;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.AbstractTestSortedMap;
import org.apache.commons.collections.BulkTest;
/**
* Extension of {@link AbstractTestSortedMap} for exercising the {@link TransformedSortedMap}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.4 $ $Date: 2003/10/06 23:44:23 $
* @version $Revision: 1.5 $ $Date: 2003/10/31 01:26:25 $
*
* @author Stephen Colebourne
*/
@ -82,7 +82,7 @@ public class TestTransformedSortedMap extends AbstractTestSortedMap {
}
public static Test suite() {
return new TestSuite(TestTransformedSortedMap.class);
return BulkTest.makeSuite(TestTransformedSortedMap.class);
}
public static void main(String args[]) {