Added Bag interface, implementations and JUnit test cases as submitted by Chuck Burdick
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130498 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
66c451d618
commit
d08fa6d71e
|
@ -0,0 +1,335 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* This class provides a skeletal implementation of the {@link Bag}
|
||||
* interface to minimize the effort required for target implementations.
|
||||
*
|
||||
* @author Chuck Burdick
|
||||
**/
|
||||
public abstract class AbstractBag implements Bag {
|
||||
private Map _map = null;
|
||||
private int _total = 0;
|
||||
private int _mods = 0;
|
||||
|
||||
public boolean add(Object o) {
|
||||
return add(o, 1);
|
||||
}
|
||||
|
||||
public boolean add(Object o, int i) {
|
||||
_mods++;
|
||||
if (i > 0) {
|
||||
int count = (i + getCount(o));
|
||||
_map.put(o, new Integer(count));
|
||||
_total += i;
|
||||
return (getCount(o) == i);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addAll(Collection c) {
|
||||
boolean changed = false;
|
||||
Iterator i = c.iterator();
|
||||
while (i.hasNext()) {
|
||||
boolean added = add(i.next());
|
||||
changed = changed || added;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
_mods++;
|
||||
_map.clear();
|
||||
_total = 0;
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
return _map.containsKey(o);
|
||||
}
|
||||
|
||||
public boolean containsAll(Collection c) {
|
||||
return containsAll(new HashBag(c));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if the bag contains all elements in
|
||||
* the given collection, respecting cardinality.
|
||||
* @see #containsAll(Collection)
|
||||
**/
|
||||
public boolean containsAll(Bag other) {
|
||||
boolean result = true;
|
||||
Iterator i = other.uniqueSet().iterator();
|
||||
while (i.hasNext()) {
|
||||
Object current = i.next();
|
||||
boolean contains =
|
||||
getCount(current) >= ((Bag)other).getCount(current);
|
||||
result = result && contains;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
boolean result = false;
|
||||
if (o instanceof AbstractBag) {
|
||||
result = _map.equals(((AbstractBag)o).getMap());
|
||||
} else if (o instanceof Map) {
|
||||
result = _map.equals((Map)o);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return _map.hashCode();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return _map.isEmpty();
|
||||
}
|
||||
|
||||
public Iterator iterator() {
|
||||
return new BagIterator(this, extractList().iterator());
|
||||
}
|
||||
|
||||
private class BagIterator implements Iterator {
|
||||
private AbstractBag _parent = null;
|
||||
private Iterator _support = null;
|
||||
private Object _current = null;
|
||||
private int _mods = 0;
|
||||
|
||||
public BagIterator(AbstractBag parent, Iterator support) {
|
||||
_parent = parent;
|
||||
_support = support;
|
||||
_current = null;
|
||||
_mods = parent.modCount();
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return _support.hasNext();
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
if (_parent.modCount() != _mods) {
|
||||
throw new ConcurrentModificationException();
|
||||
}
|
||||
_current = _support.next();
|
||||
return _current;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
if (_parent.modCount() != _mods) {
|
||||
throw new ConcurrentModificationException();
|
||||
}
|
||||
_support.remove();
|
||||
_parent.remove(_current, 1);
|
||||
_mods++;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean remove (Object o) {
|
||||
return remove(o, getCount(o));
|
||||
}
|
||||
|
||||
public boolean remove (Object o, int i) {
|
||||
_mods++;
|
||||
boolean result = false;
|
||||
int count = getCount(o);
|
||||
if (count > i) {
|
||||
_map.put(o, new Integer(count - i));
|
||||
result = true;
|
||||
_total -= i;
|
||||
} else {
|
||||
result = uniqueSet().remove(o);
|
||||
_total -= count;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean removeAll(Collection c) {
|
||||
boolean result = false;
|
||||
if (c != null) {
|
||||
Iterator i = c.iterator();
|
||||
while (i.hasNext()) {
|
||||
boolean changed = remove(i.next(), 1);
|
||||
result = result || changed;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection c) {
|
||||
return retainAll(new HashBag(c));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove any members of the bag that are not in the given
|
||||
* bag, respecting cardinality.
|
||||
* @see #retainAll(Collection)
|
||||
* @return <code>true</code> if this call changed the collection
|
||||
**/
|
||||
public boolean retainAll(Bag other) {
|
||||
boolean result = false;
|
||||
Bag excess = new HashBag();
|
||||
Iterator i = uniqueSet().iterator();
|
||||
while (i.hasNext()) {
|
||||
Object current = i.next();
|
||||
int myCount = getCount(current);
|
||||
int otherCount = other.getCount(current);
|
||||
if (1 <= otherCount && otherCount <= myCount) {
|
||||
excess.add(current, myCount - otherCount);
|
||||
} else {
|
||||
excess.add(current, myCount);
|
||||
}
|
||||
}
|
||||
if (!excess.isEmpty()) {
|
||||
result = removeAll(excess);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
return extractList().toArray();
|
||||
}
|
||||
|
||||
public Object[] toArray(Object[] a) {
|
||||
return extractList().toArray(a);
|
||||
}
|
||||
|
||||
public int getCount(Object o) {
|
||||
int result = 0;
|
||||
Integer count = MapUtils.getInteger(_map, o);
|
||||
if (count != null) {
|
||||
result = count.intValue();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Set uniqueSet() {
|
||||
return _map.keySet();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return _total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually walks the bag to make sure the count is correct and
|
||||
* resets the running total
|
||||
**/
|
||||
protected int calcTotalSize() {
|
||||
_total = extractList().size();
|
||||
return _total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method for implementations to set the map that backs
|
||||
* this bag. Not intended for interactive use outside of
|
||||
* subclasses.
|
||||
**/
|
||||
protected void setMap(Map m) {
|
||||
_map = m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method for implementations to access the map that backs
|
||||
* this bag. Not intended for interactive use outside of
|
||||
* subclasses.
|
||||
**/
|
||||
protected Map getMap() {
|
||||
return _map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a list for use in iteration, etc.
|
||||
**/
|
||||
private List extractList() {
|
||||
List result = new ArrayList();
|
||||
Iterator i = uniqueSet().iterator();
|
||||
while (i.hasNext()) {
|
||||
Object current = i.next();
|
||||
for (int index = 0; index < getCount(current); index++) {
|
||||
result.add(current);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of modifications for iterator
|
||||
**/
|
||||
private int modCount() {
|
||||
return _mods;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Bag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A {@link Collection} that keeps a count of its members of the same
|
||||
* type, using <code>hashCode</code> to check for equality. Suppose
|
||||
* you have a Bag that contains <code>{a, a, b, c}</code>. Calling
|
||||
* {@link #getCount} on <code>a</code> would return 2, while calling
|
||||
* {@link #uniqueSet} would return <code>{a, b, c}</code>.
|
||||
*
|
||||
* @author Chuck Burdick
|
||||
**/
|
||||
public interface Bag extends Collection {
|
||||
/**
|
||||
* Return the number of occurrences (cardinality) of the given
|
||||
* object currently in the bag. If the object does not exist in the
|
||||
* bag, return 0.
|
||||
**/
|
||||
public int getCount(Object o);
|
||||
|
||||
/**
|
||||
* Add the given object to the bag and keep a count. If the object
|
||||
* is already in the {@link #uniqueSet} then increment its count as
|
||||
* reported by {@link #getCount}. Otherwise add it to the {@link
|
||||
* #uniqueSet} and report its count as 1.
|
||||
* @return <code>true</code> if the object was not already in the
|
||||
* <code>uniqueSet</code>
|
||||
* @see #getCount
|
||||
**/
|
||||
public boolean add(Object o);
|
||||
|
||||
/**
|
||||
* Add <code>i</code> copies of the given object to the bag and
|
||||
* keep a count.
|
||||
* @return <code>true</code> if the object was not already in the
|
||||
* <code>uniqueSet</code>
|
||||
* @see #add(Object)
|
||||
* @see #getCount
|
||||
**/
|
||||
public boolean add(Object o, int i);
|
||||
|
||||
/**
|
||||
* Remove all occurrences of the given object from the bag, and do
|
||||
* not represent the object in the {@link #uniqueSet}.
|
||||
* @see #remove(Object, int)
|
||||
* @return <code>true</code> if this call changed the collection
|
||||
**/
|
||||
public boolean remove(Object o);
|
||||
|
||||
/**
|
||||
* Remove the given number of occurrences from the bag. If the bag
|
||||
* contains less than <code>i</code> occurrences, the item will be
|
||||
* removed from the {@link #uniqueSet}.
|
||||
* @see #getCount
|
||||
* @see #remove(Object)
|
||||
* @return <code>true</code> if this call changed the collection
|
||||
**/
|
||||
public boolean remove(Object o, int i);
|
||||
|
||||
/**
|
||||
* The {@link Set} of unique members that represent all members in
|
||||
* the bag. Uniqueness constraints are the same as those in {@link
|
||||
* Set}.
|
||||
**/
|
||||
public Set uniqueSet();
|
||||
|
||||
/**
|
||||
* Returns the total number of items in the bag across all types.
|
||||
* @see #size
|
||||
**/
|
||||
public int size();
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if the bag contains all elements in
|
||||
* the given collection, respecting cardinality. That is, if the
|
||||
* given collection <code>C</code> contains <code>n</code> copies
|
||||
* of a given object, calling {@link #getCount} on that object must
|
||||
* be >= <code>n</code> for all <code>n</code> in <code>C</code>.
|
||||
**/
|
||||
public boolean containsAll(Collection c);
|
||||
|
||||
/**
|
||||
* Remove all elements represented in the given collection,
|
||||
* respecting cardinality. That is, if the given collection
|
||||
* <code>C</code> contains <code>n</code> copies of a given object,
|
||||
* the bag will have <code>n</code> fewer copies, assuming the bag
|
||||
* had at least <code>n</code> copies to begin with.
|
||||
* @return <code>true</code> if this call changed the collection
|
||||
**/
|
||||
public boolean removeAll(Collection c);
|
||||
|
||||
/**
|
||||
* Remove any members of the bag that are not in the given
|
||||
* collection, respecting cardinality. That is, if the given
|
||||
* collection <code>C</code> contains <code>n</code> copies of a
|
||||
* given object and the bag has <code>m > n</code> copies, then
|
||||
* delete <code>m - n</code> copies from the bag. In addition, if
|
||||
* <code>e</code> is an object in the bag but
|
||||
* <code>!C.contains(e)</code>, then remove <code>e</code> and any
|
||||
* of its copies.
|
||||
*
|
||||
* @return <code>true</code> if this call changed the collection
|
||||
**/
|
||||
public boolean retainAll(Collection c);
|
||||
|
||||
/**
|
||||
* Returns an {@link Iterator} over the entire set of members,
|
||||
* including copies due to cardinality. This iterator is fail-fast
|
||||
* and will not tolerate concurrent modifications.
|
||||
**/
|
||||
public Iterator iterator();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/HashBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* An implementation of {@link Bag} that is backed by a {@link
|
||||
* HashMap}.
|
||||
*
|
||||
* @author Chuck Burdick
|
||||
**/
|
||||
public class HashBag extends AbstractBag implements Bag {
|
||||
public HashBag() {
|
||||
setMap(new HashMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* New {@link Bag} containing all the members of the given
|
||||
* collection.
|
||||
* @see #addAll
|
||||
**/
|
||||
public HashBag(Collection c) {
|
||||
this();
|
||||
addAll(c);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/SortedBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* A type of {@link Bag} that maintains order among its unique
|
||||
* representative members.
|
||||
* @author Chuck Burdick
|
||||
**/
|
||||
public interface SortedBag extends Bag {
|
||||
|
||||
/**
|
||||
* Returns the comparator associated with this sorted set, or null
|
||||
* if it uses its elements' natural ordering.
|
||||
**/
|
||||
public Comparator comparator();
|
||||
|
||||
/**
|
||||
* Returns the first (lowest) member.
|
||||
**/
|
||||
public Object first();
|
||||
|
||||
/**
|
||||
* Returns the last (highest) member.
|
||||
**/
|
||||
public Object last();
|
||||
}
|
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/TreeBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* An implementation of {@link Bag} that is backed by a {@link
|
||||
* TreeMap}. Order will be maintained among the unique representative
|
||||
* members.
|
||||
*
|
||||
* @author Chuck Burdick
|
||||
**/
|
||||
public class TreeBag extends AbstractBag implements SortedBag, Bag {
|
||||
public TreeBag() {
|
||||
setMap(new TreeMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* New {@link Bag} that maintains order on its unique
|
||||
* representative members according to the given {@link
|
||||
* Comparator}.
|
||||
**/
|
||||
public TreeBag(Comparator c) {
|
||||
setMap(new TreeMap(c));
|
||||
}
|
||||
|
||||
/**
|
||||
* New {@link Bag} containing all the members of the given
|
||||
* collection.
|
||||
* @see #addAll
|
||||
**/
|
||||
public TreeBag(Collection c) {
|
||||
this();
|
||||
addAll(c);
|
||||
}
|
||||
|
||||
public Object first() {
|
||||
return ((SortedMap)getMap()).firstKey();
|
||||
}
|
||||
|
||||
public Object last() {
|
||||
return ((SortedMap)getMap()).lastKey();
|
||||
}
|
||||
|
||||
public Comparator comparator() {
|
||||
return ((SortedMap)getMap()).comparator();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<!-- $Id: package.html,v 1.3 2001/08/17 23:00:26 rwaldhoff Exp $ -->
|
||||
<!-- $Id: package.html,v 1.4 2001/08/29 15:28:07 jstrachan Exp $ -->
|
||||
<html>
|
||||
<head>
|
||||
<title>Package Documentation for org.apache.commons.collections</title>
|
||||
|
@ -56,6 +56,18 @@
|
|||
interface.
|
||||
</td>
|
||||
</tr>
|
||||
<td valign="top">Bag Interface and Implementations</td>
|
||||
<td>
|
||||
{@link org.apache.commons.collections.Bag}<br>
|
||||
{@link org.apache.commons.collections.HashBag}<br>
|
||||
{@link org.apache.commons.collections.SortedBag}<br>
|
||||
{@link org.apache.commons.collections.TreeBag}
|
||||
</td>
|
||||
<td valign="top">
|
||||
New collections interface that keeps a count of its members.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<tr>
|
||||
<td valign="top">Adapters</td>
|
||||
<td>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.10 2001/08/23 12:04:40 jstrachan Exp $
|
||||
* $Revision: 1.10 $
|
||||
* $Date: 2001/08/23 12:04:40 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.11 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.11 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -66,7 +66,7 @@ import junit.framework.*;
|
|||
/**
|
||||
* Entry point for all Collections tests.
|
||||
* @author Rodney Waldhoff
|
||||
* @version $Id: TestAll.java,v 1.10 2001/08/23 12:04:40 jstrachan Exp $
|
||||
* @version $Id: TestAll.java,v 1.11 2001/08/29 15:28:07 jstrachan Exp $
|
||||
*/
|
||||
public class TestAll extends TestCase {
|
||||
public TestAll(String testName) {
|
||||
|
@ -88,8 +88,10 @@ public class TestAll extends TestCase {
|
|||
suite.addTest(TestFastHashMap1.suite());
|
||||
suite.addTest(TestFastTreeMap.suite());
|
||||
suite.addTest(TestFastTreeMap1.suite());
|
||||
suite.addTest(TestHashBag.suite());
|
||||
suite.addTest(TestHashMap.suite());
|
||||
suite.addTest(TestSingletonIterator.suite());
|
||||
suite.addTest(TestTreeBag.suite());
|
||||
suite.addTest(TestTreeMap.suite());
|
||||
return suite;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,251 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Tests base {@link Bag} methods and contracts.
|
||||
* <p>
|
||||
* To use, simply extend this class, and implement
|
||||
* the {@link #makeBag} method.
|
||||
* <p>
|
||||
* If your {@link Bag} fails one of these tests by design,
|
||||
* you may still use this base set of cases. Simply override the
|
||||
* test case (method) your {@link Bag} fails.
|
||||
*
|
||||
* @author Chuck Burdick
|
||||
* @version $Id: TestBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
*/
|
||||
public abstract class TestBag extends TestCollection {
|
||||
public TestBag(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new, empty {@link Bag} to used for testing.
|
||||
*/
|
||||
public abstract Bag makeBag();
|
||||
|
||||
public Collection makeCollection() {
|
||||
return makeBag();
|
||||
}
|
||||
|
||||
public void testBagAdd() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A");
|
||||
assertTrue("Should contain 'A'", bag.contains("A"));
|
||||
assertEquals("Should have count of 1",
|
||||
1, bag.getCount("A"));
|
||||
bag.add("A");
|
||||
assertTrue("Should contain 'A'", bag.contains("A"));
|
||||
assertEquals("Should have count of 2",
|
||||
2, bag.getCount("A"));
|
||||
bag.add("B");
|
||||
assertTrue(bag.contains("A"));
|
||||
assertTrue(bag.contains("B"));
|
||||
}
|
||||
|
||||
public void testBagEqualsSelf() {
|
||||
Bag bag = makeBag();
|
||||
assertTrue(bag.equals(bag));
|
||||
bag.add("elt");
|
||||
assertTrue(bag.equals(bag));
|
||||
bag.add("elt"); // again
|
||||
assertTrue(bag.equals(bag));
|
||||
bag.add("elt2");
|
||||
assertTrue(bag.equals(bag));
|
||||
}
|
||||
|
||||
public void testRemove() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A");
|
||||
assertEquals("Should have count of 1", 1, bag.getCount("A"));
|
||||
bag.remove("A");
|
||||
assertEquals("Should have count of 0", 0, bag.getCount("A"));
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
assertEquals("Should have count of 4", 4, bag.getCount("A"));
|
||||
bag.remove("A", 0);
|
||||
assertEquals("Should have count of 4", 4, bag.getCount("A"));
|
||||
bag.remove("A", 2);
|
||||
assertEquals("Should have count of 2", 2, bag.getCount("A"));
|
||||
bag.remove("A");
|
||||
assertEquals("Should have count of 0", 0, bag.getCount("A"));
|
||||
}
|
||||
|
||||
public void testRemoveAll() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A", 2);
|
||||
assertEquals("Should have count of 2", 2, bag.getCount("A"));
|
||||
bag.add("B");
|
||||
bag.add("C");
|
||||
assertEquals("Should have count of 4", 4, bag.size());
|
||||
List delete = new ArrayList();
|
||||
delete.add("A");
|
||||
delete.add("B");
|
||||
bag.removeAll(delete);
|
||||
assertEquals("Should have count of 1", 1, bag.getCount("A"));
|
||||
assertEquals("Should have count of 0", 0, bag.getCount("B"));
|
||||
assertEquals("Should have count of 1", 1, bag.getCount("C"));
|
||||
assertEquals("Should have count of 2", 2, bag.size());
|
||||
}
|
||||
|
||||
public void testContains() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("B");
|
||||
bag.add("B");
|
||||
List compare = new ArrayList();
|
||||
compare.add("A");
|
||||
compare.add("B");
|
||||
assertEquals("Other list has 1 'B'", 1,
|
||||
(new HashBag(compare)).getCount("B"));
|
||||
assertTrue("Bag has at least 1 'B'", 1 <= bag.getCount("B"));
|
||||
assertTrue("Bag contains items in the list", bag.containsAll(compare));
|
||||
compare.add("A");
|
||||
compare.add("B");
|
||||
assertEquals("Other list has 2 'B'", 2,
|
||||
(new HashBag(compare)).getCount("B"));
|
||||
assertTrue("Bag has at least 2 'B'", 2 <= bag.getCount("B"));
|
||||
assertTrue("Bag contains items in the list", bag.containsAll(compare));
|
||||
compare.add("A");
|
||||
compare.add("B");
|
||||
assertEquals("Other list has 3 'B'", 3,
|
||||
(new HashBag(compare)).getCount("B"));
|
||||
assertTrue("Bag does not have 3 'B'", 3 > bag.getCount("B"));
|
||||
assertTrue("Bag contains items in the list", !bag.containsAll(compare));
|
||||
}
|
||||
|
||||
public void testSize() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("B");
|
||||
bag.add("B");
|
||||
assertEquals("Should have 5 total items", 5, bag.size());
|
||||
bag.remove("A", 2);
|
||||
assertEquals("Should have 1 'A'", 1, bag.getCount("A"));
|
||||
assertEquals("Should have 3 total items", 3, bag.size());
|
||||
bag.remove("B");
|
||||
assertEquals("Should have 1 total item", 1, bag.size());
|
||||
}
|
||||
|
||||
public void testRetainAll() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("B");
|
||||
bag.add("B");
|
||||
bag.add("C");
|
||||
List retains = new ArrayList();
|
||||
retains.add("B");
|
||||
retains.add("C");
|
||||
bag.retainAll(retains);
|
||||
assertEquals("Should have 2 total items", 2, bag.size());
|
||||
}
|
||||
|
||||
public void testIterator() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("B");
|
||||
assertEquals("Bag should have 3 items", 3, bag.size());
|
||||
Iterator i = bag.iterator();
|
||||
assertEquals("First should be 'A'", "A", i.next());
|
||||
assertEquals("Second should be 'A'", "A", i.next());
|
||||
i.remove();
|
||||
assertEquals("Third should be 'B'", "B", i.next());
|
||||
assertTrue("Should have no more", !i.hasNext());
|
||||
assertEquals("Bag should have 2 items", 2, bag.size());
|
||||
assertEquals("Bag should have 1 'A'", 1, bag.getCount("A"));
|
||||
}
|
||||
|
||||
public void testIteratorFail() {
|
||||
Bag bag = makeBag();
|
||||
bag.add("A");
|
||||
bag.add("A");
|
||||
bag.add("B");
|
||||
Iterator i = bag.iterator();
|
||||
assertEquals("First should be 'A'", "A", i.next());
|
||||
bag.remove("A");
|
||||
try {
|
||||
i.next();
|
||||
fail("Should throw ConcurrentModificationException");
|
||||
} catch (ConcurrentModificationException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestHashBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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.*;
|
||||
|
||||
/**
|
||||
* Extension of {@link TestBag} for exercising the {@link HashBag}
|
||||
* implementation.
|
||||
*
|
||||
* @author Chuck Burdick
|
||||
* @version $Id: TestHashBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $ */
|
||||
public class TestHashBag extends TestBag {
|
||||
public TestHashBag(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestHashBag.class);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
String[] testCaseName = { TestHashBag.class.getName() };
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
public Bag makeBag() {
|
||||
return new HashBag();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestTreeBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/08/29 15:28:07 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements 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 Group.
|
||||
*
|
||||
* 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.*;
|
||||
|
||||
/**
|
||||
* Extension of {@link TestBag} for exercising the {@link TreeBag}
|
||||
* implementation.
|
||||
*
|
||||
* @author Chuck Burdick
|
||||
* @version $Id: TestTreeBag.java,v 1.1 2001/08/29 15:28:07 jstrachan Exp $ */
|
||||
public class TestTreeBag extends TestBag {
|
||||
public TestTreeBag(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestTreeBag.class);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
String[] testCaseName = { TestTreeBag.class.getName() };
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
public Bag makeBag() {
|
||||
return new TreeBag();
|
||||
}
|
||||
|
||||
public SortedBag setupBag() {
|
||||
SortedBag bag = (SortedBag)makeBag();
|
||||
bag.add("C");
|
||||
bag.add("A");
|
||||
bag.add("B");
|
||||
bag.add("D");
|
||||
return bag;
|
||||
}
|
||||
|
||||
public void testOrdering() {
|
||||
Bag bag = setupBag();
|
||||
assertEquals("Should get elements in correct order",
|
||||
"A", bag.toArray()[0]);
|
||||
assertEquals("Should get elements in correct order",
|
||||
"B", bag.toArray()[1]);
|
||||
assertEquals("Should get elements in correct order",
|
||||
"C", bag.toArray()[2]);
|
||||
assertEquals("Should get first key",
|
||||
"A", ((SortedBag)bag).first());
|
||||
assertEquals("Should get last key",
|
||||
"D", ((SortedBag)bag).last());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue