Removed bad line-endings (multiple ^Ms).

Thanks to Christopher Elkins.
see: http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg04102.html


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130641 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Smith 2002-03-13 05:40:31 +00:00
parent 7733e2f193
commit cfb9865f41
16 changed files with 2550 additions and 2550 deletions

View File

@ -1,339 +1,339 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractBag.java,v 1.4 2002/02/22 04:58:17 mas Exp $
* $Revision: 1.4 $
* $Date: 2002/02/22 04:58:17 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Collections;
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.
* Subclasses need only to call {@link #setMap(Map)} in their constructor
* specifying a map instance that will be used to store the contents of
* the bag.
*
* @author Chuck Burdick
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractBag.java,v 1.5 2002/03/13 05:40:30 mas Exp $
* $Revision: 1.5 $
* $Date: 2002/03/13 05:40:30 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Collections;
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.
* Subclasses need only to call {@link #setMap(Map)} in their constructor
* specifying a map instance that will be used to store the contents of
* the bag.
*
* @author Chuck Burdick
* @author <a href="mas@apache.org">Michael A. Smith</a>
**/
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 (count == 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) {
return (o == this ||
(o != null && o.getClass().equals(this.getClass()) &&
((AbstractBag)o)._map.equals(this._map)));
}
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 (i <= 0) {
result = false;
} else if (count > i) {
_map.put(o, new Integer(count - i));
result = true;
_total -= i;
} else { // count > 0 && count <= i
// need to remove all
result = (_map.remove(o) != null);
_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 Collections.unmodifiableSet(_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 = getCount(current); index > 0; index--) {
result.add(current);
}
}
return result;
}
/**
* Return number of modifications for iterator
**/
private int modCount() {
return _mods;
}
}
**/
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 (count == 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) {
return (o == this ||
(o != null && o.getClass().equals(this.getClass()) &&
((AbstractBag)o)._map.equals(this._map)));
}
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 (i <= 0) {
result = false;
} else if (count > i) {
_map.put(o, new Integer(count - i));
result = true;
_total -= i;
} else { // count > 0 && count <= i
// need to remove all
result = (_map.remove(o) != null);
_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 Collections.unmodifiableSet(_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 = getCount(current); index > 0; index--) {
result.add(current);
}
}
return result;
}
/**
* Return number of modifications for iterator
**/
private int modCount() {
return _mods;
}
}

View File

@ -1,181 +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.3 2002/02/22 04:39:53 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/02/22 04:39:53 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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 <code>i</code> occurrences or less, 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 &gt; 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();
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Bag.java,v 1.4 2002/03/13 05:40:30 mas Exp $
* $Revision: 1.4 $
* $Date: 2002/03/13 05:40:30 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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 <code>i</code> occurrences or less, 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 &gt; 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();
}

View File

@ -1,74 +1,74 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Closure.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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;
/** An interface to represent some Closure, a block of code which is executed
* from inside some block, function or iteration which operates on an input
* object.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface Closure {
/** Performs some operation on the input object
*/
public void execute(Object input);
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Closure.java,v 1.3 2002/03/13 05:40:30 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:30 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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;
/** An interface to represent some Closure, a block of code which is executed
* from inside some block, function or iteration which operates on an input
* object.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface Closure {
/** Performs some operation on the input object
*/
public void execute(Object input);
}

View File

@ -1,139 +1,139 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/DefaultMapEntry.java,v 1.3 2002/02/22 04:58:17 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/02/22 04:58:17 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Map;
/** A default implementation of {@link Map.Entry Map.Entry}
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
*/
public class DefaultMapEntry implements Map.Entry {
private Object key;
private Object value;
public DefaultMapEntry() {
}
public DefaultMapEntry(Object key, Object value) {
this.key = key;
this.value = value;
}
/**
* Implemented per API documentation of {@link Map.Entry#equals(Object)}
**/
public boolean equals(Object o) {
if( o == null ) return false;
if( o == this ) return true;
if ( ! (o instanceof Map.Entry ) )
return false;
Map.Entry e2 = (Map.Entry)o;
return ((getKey() == null ?
e2.getKey() == null : getKey().equals(e2.getKey())) &&
(getValue() == null ?
e2.getValue() == null : getValue().equals(e2.getValue())));
}
/**
* Implemented per API documentation of {@link Map.Entry#hashCode()}
**/
public int hashCode() {
return ( ( getKey() == null ? 0 : getKey().hashCode() ) ^
( getValue() == null ? 0 : getValue().hashCode() ) );
}
// Map.Entry interface
//-------------------------------------------------------------------------
public Object getKey() {
return key;
}
public Object getValue() {
return value;
}
// Properties
//-------------------------------------------------------------------------
public void setKey(Object key) {
this.key = key;
}
/** Note that this method only sets the local reference inside this object and
* does not modify the original Map.
*
* @return the old value of the value
* @param value the new value
*/
public Object setValue(Object value) {
Object answer = this.value;
this.value = value;
return answer;
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/DefaultMapEntry.java,v 1.4 2002/03/13 05:40:30 mas Exp $
* $Revision: 1.4 $
* $Date: 2002/03/13 05:40:30 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Map;
/** A default implementation of {@link Map.Entry Map.Entry}
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
*/
public class DefaultMapEntry implements Map.Entry {
private Object key;
private Object value;
public DefaultMapEntry() {
}
public DefaultMapEntry(Object key, Object value) {
this.key = key;
this.value = value;
}
/**
* Implemented per API documentation of {@link Map.Entry#equals(Object)}
**/
public boolean equals(Object o) {
if( o == null ) return false;
if( o == this ) return true;
if ( ! (o instanceof Map.Entry ) )
return false;
Map.Entry e2 = (Map.Entry)o;
return ((getKey() == null ?
e2.getKey() == null : getKey().equals(e2.getKey())) &&
(getValue() == null ?
e2.getValue() == null : getValue().equals(e2.getValue())));
}
/**
* Implemented per API documentation of {@link Map.Entry#hashCode()}
**/
public int hashCode() {
return ( ( getKey() == null ? 0 : getKey().hashCode() ) ^
( getValue() == null ? 0 : getValue().hashCode() ) );
}
// Map.Entry interface
//-------------------------------------------------------------------------
public Object getKey() {
return key;
}
public Object getValue() {
return value;
}
// Properties
//-------------------------------------------------------------------------
public void setKey(Object key) {
this.key = key;
}
/** Note that this method only sets the local reference inside this object and
* does not modify the original Map.
*
* @return the old value of the value
* @param value the new value
*/
public Object setValue(Object value) {
Object answer = this.value;
this.value = value;
return answer;
}
}

View File

@ -1,89 +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.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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);
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/HashBag.java,v 1.3 2002/03/13 05:40:30 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:30 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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);
}
}

View File

@ -1,102 +1,102 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/IteratorEnumeration.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Enumeration;
import java.util.Iterator;
/** Adapter to make an {@link Iterator Iterator} instance appear to be an {@link Enumeration Enumeration} instances
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class IteratorEnumeration implements Enumeration {
private Iterator iterator;
public IteratorEnumeration() {
}
public IteratorEnumeration( Iterator iterator ) {
this.iterator = iterator;
}
// Iterator interface
//-------------------------------------------------------------------------
public boolean hasMoreElements() {
return iterator.hasNext();
}
public Object nextElement() {
return iterator.next();
}
// Properties
//-------------------------------------------------------------------------
public Iterator getIterator() {
return iterator;
}
public void setIterator( Iterator iterator ) {
this.iterator = iterator;
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/IteratorEnumeration.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Enumeration;
import java.util.Iterator;
/** Adapter to make an {@link Iterator Iterator} instance appear to be an {@link Enumeration Enumeration} instances
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class IteratorEnumeration implements Enumeration {
private Iterator iterator;
public IteratorEnumeration() {
}
public IteratorEnumeration( Iterator iterator ) {
this.iterator = iterator;
}
// Iterator interface
//-------------------------------------------------------------------------
public boolean hasMoreElements() {
return iterator.hasNext();
}
public Object nextElement() {
return iterator.next();
}
// Properties
//-------------------------------------------------------------------------
public Iterator getIterator() {
return iterator;
}
public void setIterator( Iterator iterator ) {
this.iterator = iterator;
}
}

View File

@ -1,428 +1,428 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.3 2002/02/23 03:50:23 bayard Exp $
* $Revision: 1.3 $
* $Date: 2002/02/23 03:50:23 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.io.*;
import java.text.*;
import java.util.*;
/** A helper class for using {@link Map Map} instances.
*
* It contains various typesafe methods
* as well as other useful features like deep copying
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
*/
public class MapUtils {
private static int debugIndent = 0;
// Type safe getters
//-------------------------------------------------------------------------
public static Object getObject( Map map, Object key ) {
if ( map != null ) {
return map.get( key );
}
return null;
}
public static String getString( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
return answer.toString();
}
}
return null;
}
public static Boolean getBoolean( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
if ( answer instanceof Boolean ) {
return (Boolean) answer;
}
else
if ( answer instanceof String ) {
return new Boolean( (String) answer );
}
else
if ( answer instanceof Number ) {
Number n = (Number) answer;
return ( n.intValue() != 0 ) ? Boolean.TRUE : Boolean.FALSE;
}
}
}
return null;
}
public static Number getNumber( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
if ( answer instanceof Number ) {
return (Number) answer;
}
else
if ( answer instanceof String ) {
try {
String text = (String) answer;
return NumberFormat.getInstance().parse( text );
}
catch (ParseException e) {
logInfo( e );
}
}
}
}
return null;
}
public static Byte getByte( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Byte ) {
return (Byte) answer;
}
return new Byte( answer.byteValue() );
}
public static Short getShort( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Short ) {
return (Short) answer;
}
return new Short( answer.shortValue() );
}
public static Integer getInteger( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Integer ) {
return (Integer) answer;
}
return new Integer( answer.intValue() );
}
public static Long getLong( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Long ) {
return (Long) answer;
}
return new Long( answer.longValue() );
}
public static Float getFloat( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Float ) {
return (Float) answer;
}
return new Float( answer.floatValue() );
}
public static Double getDouble( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Double ) {
return (Double) answer;
}
return new Double( answer.doubleValue() );
}
public static Map getMap( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null && answer instanceof Map ) {
return (Map) answer;
}
}
return null;
}
// Type safe getters with default values
//-------------------------------------------------------------------------
public static Object getObject( Map map, Object key, Object defaultValue ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
return answer;
}
}
return defaultValue;
}
public static String getString( Map map, Object key, String defaultValue ) {
String answer = getString( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Boolean getBoolean( Map map, Object key, Boolean defaultValue ) {
Boolean answer = getBoolean( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Number getNumber( Map map, Object key, Number defaultValue ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Byte getByte( Map map, Object key, Byte defaultValue ) {
Byte answer = getByte( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Short getShort( Map map, Object key, Short defaultValue ) {
Short answer = getShort( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Integer getInteger( Map map, Object key, Integer defaultValue ) {
Integer answer = getInteger( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Long getLong( Map map, Object key, Long defaultValue ) {
Long answer = getLong( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Float getFloat( Map map, Object key, Float defaultValue ) {
Float answer = getFloat( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Double getDouble( Map map, Object key, Double defaultValue ) {
Double answer = getDouble( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Map getMap( Map map, Object key, Map defaultValue ) {
Map answer = getMap( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
// Conversion methods
//-------------------------------------------------------------------------
public static Properties toProperties(Map input) {
Properties answer = new Properties();
if ( input != null ) {
for ( Iterator iter = input.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
answer.put(key, value);
}
}
return answer;
}
// Printing methods
//-------------------------------------------------------------------------
public static synchronized void verbosePrint( PrintStream out, Object key, Map map ) {
debugPrintIndent( out );
out.println( key + " = " );
debugPrintIndent( out );
out.println( "{" );
++debugIndent;
for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
String childKey = (String) entry.getKey();
Object childValue = entry.getValue();
if ( childValue instanceof Map ) {
verbosePrint( out, childKey, (Map) childValue );
}
else {
debugPrintIndent( out );
out.println( childKey + " = " + childValue);
}
}
--debugIndent;
debugPrintIndent( out );
out.println( "}" );
}
public static synchronized void debugPrint( PrintStream out, Object key, Map map ) {
debugPrintIndent( out );
out.println( key + " = " );
debugPrintIndent( out );
out.println( "{" );
++debugIndent;
for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
String childKey = (String) entry.getKey();
Object childValue = entry.getValue();
if ( childValue instanceof Map ) {
verbosePrint( out, childKey, (Map) childValue );
}
else {
debugPrintIndent( out );
String typeName = ( childValue != null )
? childValue.getClass().getName()
: null;
out.println( childKey + " = " + childValue + " class: " + typeName );
}
}
--debugIndent;
debugPrintIndent( out );
out.println( "}" );
}
// Implementation methods
//-------------------------------------------------------------------------
protected static void debugPrintIndent( PrintStream out ) {
for ( int i = 0; i < debugIndent; i++ ) {
out.print( " " );
}
}
protected static void logInfo(Exception e) {
// XXXX: should probably use log4j here instead...
System.out.println( "INFO: Exception: " + e );
}
/**
* Nice method for adding data to a map in such a way
* as to not get NPE's. The point being that if the
* value is null, map.put() will throw an exception.
* That blows in the case of this class cause you may want to
* essentially treat put("Not Null", null ) == put("Not Null", "")
* We will still throw a NPE if the key is null cause that should
* never happen.
*/
public static final void safeAddToMap(Map map, Object key, Object value)
throws NullPointerException
{
if (value == null)
{
map.put ( key, "" );
}
else
{
map.put ( key, value );
}
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.4 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.4 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.io.*;
import java.text.*;
import java.util.*;
/** A helper class for using {@link Map Map} instances.
*
* It contains various typesafe methods
* as well as other useful features like deep copying
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
*/
public class MapUtils {
private static int debugIndent = 0;
// Type safe getters
//-------------------------------------------------------------------------
public static Object getObject( Map map, Object key ) {
if ( map != null ) {
return map.get( key );
}
return null;
}
public static String getString( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
return answer.toString();
}
}
return null;
}
public static Boolean getBoolean( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
if ( answer instanceof Boolean ) {
return (Boolean) answer;
}
else
if ( answer instanceof String ) {
return new Boolean( (String) answer );
}
else
if ( answer instanceof Number ) {
Number n = (Number) answer;
return ( n.intValue() != 0 ) ? Boolean.TRUE : Boolean.FALSE;
}
}
}
return null;
}
public static Number getNumber( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
if ( answer instanceof Number ) {
return (Number) answer;
}
else
if ( answer instanceof String ) {
try {
String text = (String) answer;
return NumberFormat.getInstance().parse( text );
}
catch (ParseException e) {
logInfo( e );
}
}
}
}
return null;
}
public static Byte getByte( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Byte ) {
return (Byte) answer;
}
return new Byte( answer.byteValue() );
}
public static Short getShort( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Short ) {
return (Short) answer;
}
return new Short( answer.shortValue() );
}
public static Integer getInteger( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Integer ) {
return (Integer) answer;
}
return new Integer( answer.intValue() );
}
public static Long getLong( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Long ) {
return (Long) answer;
}
return new Long( answer.longValue() );
}
public static Float getFloat( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Float ) {
return (Float) answer;
}
return new Float( answer.floatValue() );
}
public static Double getDouble( Map map, Object key ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
return null;
}
else
if ( answer instanceof Double ) {
return (Double) answer;
}
return new Double( answer.doubleValue() );
}
public static Map getMap( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null && answer instanceof Map ) {
return (Map) answer;
}
}
return null;
}
// Type safe getters with default values
//-------------------------------------------------------------------------
public static Object getObject( Map map, Object key, Object defaultValue ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
return answer;
}
}
return defaultValue;
}
public static String getString( Map map, Object key, String defaultValue ) {
String answer = getString( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Boolean getBoolean( Map map, Object key, Boolean defaultValue ) {
Boolean answer = getBoolean( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Number getNumber( Map map, Object key, Number defaultValue ) {
Number answer = getNumber( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Byte getByte( Map map, Object key, Byte defaultValue ) {
Byte answer = getByte( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Short getShort( Map map, Object key, Short defaultValue ) {
Short answer = getShort( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Integer getInteger( Map map, Object key, Integer defaultValue ) {
Integer answer = getInteger( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Long getLong( Map map, Object key, Long defaultValue ) {
Long answer = getLong( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Float getFloat( Map map, Object key, Float defaultValue ) {
Float answer = getFloat( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Double getDouble( Map map, Object key, Double defaultValue ) {
Double answer = getDouble( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
public static Map getMap( Map map, Object key, Map defaultValue ) {
Map answer = getMap( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
// Conversion methods
//-------------------------------------------------------------------------
public static Properties toProperties(Map input) {
Properties answer = new Properties();
if ( input != null ) {
for ( Iterator iter = input.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
answer.put(key, value);
}
}
return answer;
}
// Printing methods
//-------------------------------------------------------------------------
public static synchronized void verbosePrint( PrintStream out, Object key, Map map ) {
debugPrintIndent( out );
out.println( key + " = " );
debugPrintIndent( out );
out.println( "{" );
++debugIndent;
for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
String childKey = (String) entry.getKey();
Object childValue = entry.getValue();
if ( childValue instanceof Map ) {
verbosePrint( out, childKey, (Map) childValue );
}
else {
debugPrintIndent( out );
out.println( childKey + " = " + childValue);
}
}
--debugIndent;
debugPrintIndent( out );
out.println( "}" );
}
public static synchronized void debugPrint( PrintStream out, Object key, Map map ) {
debugPrintIndent( out );
out.println( key + " = " );
debugPrintIndent( out );
out.println( "{" );
++debugIndent;
for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
String childKey = (String) entry.getKey();
Object childValue = entry.getValue();
if ( childValue instanceof Map ) {
verbosePrint( out, childKey, (Map) childValue );
}
else {
debugPrintIndent( out );
String typeName = ( childValue != null )
? childValue.getClass().getName()
: null;
out.println( childKey + " = " + childValue + " class: " + typeName );
}
}
--debugIndent;
debugPrintIndent( out );
out.println( "}" );
}
// Implementation methods
//-------------------------------------------------------------------------
protected static void debugPrintIndent( PrintStream out ) {
for ( int i = 0; i < debugIndent; i++ ) {
out.print( " " );
}
}
protected static void logInfo(Exception e) {
// XXXX: should probably use log4j here instead...
System.out.println( "INFO: Exception: " + e );
}
/**
* Nice method for adding data to a map in such a way
* as to not get NPE's. The point being that if the
* value is null, map.put() will throw an exception.
* That blows in the case of this class cause you may want to
* essentially treat put("Not Null", null ) == put("Not Null", "")
* We will still throw a NPE if the key is null cause that should
* never happen.
*/
public static final void safeAddToMap(Map map, Object key, Object value)
throws NullPointerException
{
if (value == null)
{
map.put ( key, "" );
}
else
{
map.put ( key, value );
}
}
}

View File

@ -1,206 +1,206 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MultiHashMap.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.*;
import java.io.*;
/** see MultiMap for details of an important semantic difference
* between this and a typical HashMap
*
* @author Christopher Berry
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class MultiHashMap extends HashMap implements MultiMap
{
//----------------- Data
private static int sCount = 0;
private String mName = null;
public MultiHashMap()
{
super();
setName();
}
public MultiHashMap( int initialCapacity )
{
super( initialCapacity );
setName();
}
public MultiHashMap(int initialCapacity, float loadFactor )
{
super( initialCapacity, loadFactor);
setName();
}
public MultiHashMap( Map mapToCopy )
{
super( mapToCopy );
}
private void setName()
{
sCount++;
mName = "MultiMap-" + sCount;
}
public String getName()
{ return mName; }
public Object put( Object key, Object value )
{
// NOTE:: put might be called during deserialization !!!!!!
// so we must provide a hook to handle this case
// This means that we cannot make MultiMaps of ArrayLists !!!
if ( value instanceof ArrayList ) {
return ( super.put( key, value ) );
}
ArrayList keyList = (ArrayList)(super.get( key ));
if ( keyList == null ) {
keyList = new ArrayList(10);
super.put( key, keyList );
}
boolean results = keyList.add( value );
return ( results ? value : null );
}
public boolean containsValue( Object value )
{
Set pairs = super.entrySet();
if ( pairs == null )
return false;
Iterator pairsIterator = pairs.iterator();
while ( pairsIterator.hasNext() ) {
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
ArrayList list = (ArrayList)(keyValuePair.getValue());
if( list.contains( value ) )
return true;
}
return false;
}
public Object remove( Object key, Object item )
{
ArrayList valuesForKey = (ArrayList) super.get( key );
if ( valuesForKey == null )
return null;
valuesForKey.remove( item );
return item;
}
public void clear()
{
Set pairs = super.entrySet();
Iterator pairsIterator = pairs.iterator();
while ( pairsIterator.hasNext() ) {
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
ArrayList list = (ArrayList)(keyValuePair.getValue());
list.clear();
}
super.clear();
}
public void putAll( Map mapToPut )
{
super.putAll( mapToPut );
}
public Collection values()
{
ArrayList returnList = new ArrayList( super.size() );
Set pairs = super.entrySet();
Iterator pairsIterator = pairs.iterator();
while ( pairsIterator.hasNext() ) {
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
ArrayList list = (ArrayList)(keyValuePair.getValue());
Object[] values = list.toArray();
for( int ii=0; ii < values.length; ii++ ) {
boolean successfulAdd = returnList.add( values[ii] );
}
}
return returnList;
}
// FIXME:: do we need to implement this??
// public boolean equals( Object obj ) {}
// --------------- From Cloneable
public Object clone()
{
MultiHashMap obj = (MultiHashMap)(super.clone());
obj.mName = mName;
return obj;
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MultiHashMap.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.*;
import java.io.*;
/** see MultiMap for details of an important semantic difference
* between this and a typical HashMap
*
* @author Christopher Berry
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class MultiHashMap extends HashMap implements MultiMap
{
//----------------- Data
private static int sCount = 0;
private String mName = null;
public MultiHashMap()
{
super();
setName();
}
public MultiHashMap( int initialCapacity )
{
super( initialCapacity );
setName();
}
public MultiHashMap(int initialCapacity, float loadFactor )
{
super( initialCapacity, loadFactor);
setName();
}
public MultiHashMap( Map mapToCopy )
{
super( mapToCopy );
}
private void setName()
{
sCount++;
mName = "MultiMap-" + sCount;
}
public String getName()
{ return mName; }
public Object put( Object key, Object value )
{
// NOTE:: put might be called during deserialization !!!!!!
// so we must provide a hook to handle this case
// This means that we cannot make MultiMaps of ArrayLists !!!
if ( value instanceof ArrayList ) {
return ( super.put( key, value ) );
}
ArrayList keyList = (ArrayList)(super.get( key ));
if ( keyList == null ) {
keyList = new ArrayList(10);
super.put( key, keyList );
}
boolean results = keyList.add( value );
return ( results ? value : null );
}
public boolean containsValue( Object value )
{
Set pairs = super.entrySet();
if ( pairs == null )
return false;
Iterator pairsIterator = pairs.iterator();
while ( pairsIterator.hasNext() ) {
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
ArrayList list = (ArrayList)(keyValuePair.getValue());
if( list.contains( value ) )
return true;
}
return false;
}
public Object remove( Object key, Object item )
{
ArrayList valuesForKey = (ArrayList) super.get( key );
if ( valuesForKey == null )
return null;
valuesForKey.remove( item );
return item;
}
public void clear()
{
Set pairs = super.entrySet();
Iterator pairsIterator = pairs.iterator();
while ( pairsIterator.hasNext() ) {
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
ArrayList list = (ArrayList)(keyValuePair.getValue());
list.clear();
}
super.clear();
}
public void putAll( Map mapToPut )
{
super.putAll( mapToPut );
}
public Collection values()
{
ArrayList returnList = new ArrayList( super.size() );
Set pairs = super.entrySet();
Iterator pairsIterator = pairs.iterator();
while ( pairsIterator.hasNext() ) {
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
ArrayList list = (ArrayList)(keyValuePair.getValue());
Object[] values = list.toArray();
for( int ii=0; ii < values.length; ii++ ) {
boolean successfulAdd = returnList.add( values[ii] );
}
}
return returnList;
}
// FIXME:: do we need to implement this??
// public boolean equals( Object obj ) {}
// --------------- From Cloneable
public Object clone()
{
MultiHashMap obj = (MultiHashMap)(super.clone());
obj.mName = mName;
return obj;
}
}

View File

@ -1,80 +1,80 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MultiMap.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Map;
/**
* This is simply a Map with slightly different semantics.
* Instead of returning an Object, it returns a Collection.
* So for example, you can put( key, new Integer(1) );
* and then a Object get( key ); will return you a Collection
* instead of an Integer.
* Thus, this is simply a tag interface.
*
* @author Christopher Berry
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface MultiMap extends Map {
public Object remove( Object key, Object item );
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MultiMap.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Map;
/**
* This is simply a Map with slightly different semantics.
* Instead of returning an Object, it returns a Collection.
* So for example, you can put( key, new Integer(1) );
* and then a Object get( key ); will return you a Collection
* instead of an Integer.
* Thus, this is simply a tag interface.
*
* @author Christopher Berry
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface MultiMap extends Map {
public Object remove( Object key, Object item );
}

View File

@ -1,73 +1,73 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Predicate.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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;
/** Performs some predicate which returns true or false based on the input object.
* Predicate instances can be used to implement queries or to do filtering.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface Predicate {
/** @return true if the input object matches this predicate, else returns false
*/
public boolean evaluate(Object input);
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Predicate.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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;
/** Performs some predicate which returns true or false based on the input object.
* Predicate instances can be used to implement queries or to do filtering.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface Predicate {
/** @return true if the input object matches this predicate, else returns false
*/
public boolean evaluate(Object input);
}

View File

@ -1,98 +1,98 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/SingletonIterator.java,v 1.3 2002/02/26 21:30:40 morgand Exp $
* $Revision: 1.3 $
* $Date: 2002/02/26 21:30:40 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Iterator;
import java.util.NoSuchElementException;
/** <p><code>SingletonIterator</code> is an {@link Iterator} over a single
* object instance.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.3 $
*/
public class SingletonIterator implements Iterator {
private boolean first = true;
private Object object;
public SingletonIterator(Object object) {
this.object = object;
}
public boolean hasNext() {
return first;
}
public Object next() {
if (! first ) {
throw new NoSuchElementException();
}
Object answer = object;
object = null;
first = false;
return answer;
}
public void remove() {
throw new UnsupportedOperationException( "remove() is not supported by this iterator" );
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/SingletonIterator.java,v 1.4 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.4 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Iterator;
import java.util.NoSuchElementException;
/** <p><code>SingletonIterator</code> is an {@link Iterator} over a single
* object instance.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.4 $
*/
public class SingletonIterator implements Iterator {
private boolean first = true;
private Object object;
public SingletonIterator(Object object) {
this.object = object;
}
public boolean hasNext() {
return first;
}
public Object next() {
if (! first ) {
throw new NoSuchElementException();
}
Object answer = object;
object = null;
first = false;
return answer;
}
public void remove() {
throw new UnsupportedOperationException( "remove() is not supported by this iterator" );
}
}

View File

@ -1,344 +1,344 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/SoftRefHashMap.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.lang.ref.*;
import java.lang.reflect.*;
import java.util.*;
/** <p>
* HashMap with SoftReference links to values which allows the values of the Map
* to be garbage collected by the JVM if it becomes low on memory.
* Derive from this class and
* override the factory method <code>createReference()</code> method to make
* a Map wrapped in other types of Reference.
* </p>
*
* <p>
* A synchronized version can be obtained with:
* <code>Collections.synchronizedMap( theMapToSynchronize )</code>
* </p>
*
* <p>
* <b>WARNING</b> the values() and entrySet() methods require optimisation
* like the standard {@link HashMap} implementations so that iteration
* over this Map is efficient.
* </p>
*
* @author James.Dodd
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class SoftRefHashMap implements Map {
/** The wrapped HashMap */
private Map hashMap = new HashMap();
public SoftRefHashMap() {
}
/**
* Removes References that have had their referents garbage collected
*/
public void purge() {
Map map = getMap();
Set keys = map.keySet();
if ( keys == null ) {
return;
}
for ( Iterator i = keys.iterator(); i.hasNext(); ) {
Object key = (Object) i.next();
Reference ref = (Reference) map.get( key );
if ( ref.get() == null ) {
map.remove( key );
}
}
}
// Map implementation
// -------------------------------------------------------
/**
* Retrieves the referent of the Referenced value
* @param key The key with which to retrieve the value
*/
public Object get( final Object key ) {
Reference ref = (Reference) getMap().get( key );
if ( ref == null ) {
return null;
}
return ref.get();
}
/**
* Adds a key-value mapping, wrapping the value in a Reference
*/
public Object put( final Object key, final Object value ) {
Object answer = getMap().put( key, createReference( value ) );
if ( answer != null ) {
return ((Reference) answer).get();
}
return null;
}
/**
* Returns a collection of the Referenced values
*/
public Collection values() {
Set wrappedValues = (Set) getMap().values();
Set values = new TreeSet();
if ( wrappedValues == null ) {
return values;
}
for ( Iterator i = wrappedValues.iterator(); i.hasNext(); ) {
Reference ref = (Reference) i.next();
if ( ref != null ) {
values.add( ref.get() );
}
}
return values;
}
/**
* Answers whether the argument is in the domain of the mappings
*/
public boolean containsKey( Object key ) {
return getMap().containsKey( key );
}
/**
* Answers whether the argument is a Referenced value
*/
public boolean containsValue( Object value ) {
Collection values = (Collection) getMap().values();
if ( values == null ) {
return false;
}
for ( Iterator i = values.iterator(); i.hasNext(); ) {
Reference ref = (Reference) i.next();
if ( ref == null ) {
continue;
}
Object target = ref.get();
if ( target == value ) {
return true;
}
}
return false;
}
/**
* Put all of the mappings in the argument into this wrapped map
*/
public void putAll( final java.util.Map map ) {
if ( map == null || map.size() == 0 ) {
return;
}
for ( Iterator i = map.keySet().iterator(); i.hasNext(); ) {
Object key = (Object) i.next();
put( key, map.get( key ) );
}
}
/**
* Returns a set view of the mappings in the wrapped map
*/
public Set entrySet() {
Set entries = new HashSet();
if ( size() == 0 ) {
return entries;
}
for ( Iterator i = keySet().iterator(); i.hasNext(); ) {
Object key = i.next();
Object value = get( key );
Entry entry = new Entry( key, value );
entries.add( entry );
}
return entries;
}
/**
* Removes a mapping from this map
*/
public Object remove( final Object key ) {
Reference ref = (Reference) getMap().remove( key );
if ( ref != null ) {
return ref.get();
}
return null;
}
/**
* Clears all mappings
*/
public void clear() {
getMap().clear();
}
/**
* Calculates the hash code for this map
*/
public int hashCode() {
return getMap().hashCode();
}
/**
* Returns the domain of the mappings
*/
public Set keySet() {
return getMap().keySet();
}
/**
* Answers whether there are any mappings
*/
public boolean isEmpty() {
return getMap().isEmpty();
}
/**
* Answers whether this map and the argument are 'the same'
*/
public boolean equals( final Object object ) {
return getMap().equals( object );
}
/**
* Returns the number of mappings in this map
*/
public int size() {
return getMap().size();
}
// Inner Classes
// ---------------------------------------------------------------------
/**
* A map entry, which is backed by this RefHashMap
*/
class Entry implements Map.Entry {
/**
* Constructor
*/
public Entry( Object key, Object value ) {
this.key = key;
this.value = value;
}
// Map.Entry interface
// -----------------------------------------------------------
/**
* Retrieves the key of this mapping
*/
public Object getKey() {
return key;
}
/**
* Retrieves the value of this mapping
*/
public Object getValue() {
return value;
}
/**
* Sets the value of this mapping
*/
public Object setValue( Object value ) {
this.value = value;
put( key, value );
return value;
}
/**
* Return the hash code of this mapping.
* This algorithm was taken from the JavaDoc for Map.Entry
*/
public int hashCode() {
return ( getKey() == null ? 0 : getKey().hashCode() ) ^
( getValue() == null ? 0 : getValue().hashCode() );
}
/** The domain of this mapping */
private Object key;
/** The range of this mapping */
private Object value;
}
/**
* Returns a reference to the argument.
* Override this method to make wrapped maps for other Reference types
*/
protected Reference createReference( Object referent ) {
return new SoftReference( referent );
}
/**
* Retrieves the wrapped HashMap
* @return The wrapped HashMap
*/
protected Map getMap() {
return hashMap;
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/SoftRefHashMap.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.lang.ref.*;
import java.lang.reflect.*;
import java.util.*;
/** <p>
* HashMap with SoftReference links to values which allows the values of the Map
* to be garbage collected by the JVM if it becomes low on memory.
* Derive from this class and
* override the factory method <code>createReference()</code> method to make
* a Map wrapped in other types of Reference.
* </p>
*
* <p>
* A synchronized version can be obtained with:
* <code>Collections.synchronizedMap( theMapToSynchronize )</code>
* </p>
*
* <p>
* <b>WARNING</b> the values() and entrySet() methods require optimisation
* like the standard {@link HashMap} implementations so that iteration
* over this Map is efficient.
* </p>
*
* @author James.Dodd
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class SoftRefHashMap implements Map {
/** The wrapped HashMap */
private Map hashMap = new HashMap();
public SoftRefHashMap() {
}
/**
* Removes References that have had their referents garbage collected
*/
public void purge() {
Map map = getMap();
Set keys = map.keySet();
if ( keys == null ) {
return;
}
for ( Iterator i = keys.iterator(); i.hasNext(); ) {
Object key = (Object) i.next();
Reference ref = (Reference) map.get( key );
if ( ref.get() == null ) {
map.remove( key );
}
}
}
// Map implementation
// -------------------------------------------------------
/**
* Retrieves the referent of the Referenced value
* @param key The key with which to retrieve the value
*/
public Object get( final Object key ) {
Reference ref = (Reference) getMap().get( key );
if ( ref == null ) {
return null;
}
return ref.get();
}
/**
* Adds a key-value mapping, wrapping the value in a Reference
*/
public Object put( final Object key, final Object value ) {
Object answer = getMap().put( key, createReference( value ) );
if ( answer != null ) {
return ((Reference) answer).get();
}
return null;
}
/**
* Returns a collection of the Referenced values
*/
public Collection values() {
Set wrappedValues = (Set) getMap().values();
Set values = new TreeSet();
if ( wrappedValues == null ) {
return values;
}
for ( Iterator i = wrappedValues.iterator(); i.hasNext(); ) {
Reference ref = (Reference) i.next();
if ( ref != null ) {
values.add( ref.get() );
}
}
return values;
}
/**
* Answers whether the argument is in the domain of the mappings
*/
public boolean containsKey( Object key ) {
return getMap().containsKey( key );
}
/**
* Answers whether the argument is a Referenced value
*/
public boolean containsValue( Object value ) {
Collection values = (Collection) getMap().values();
if ( values == null ) {
return false;
}
for ( Iterator i = values.iterator(); i.hasNext(); ) {
Reference ref = (Reference) i.next();
if ( ref == null ) {
continue;
}
Object target = ref.get();
if ( target == value ) {
return true;
}
}
return false;
}
/**
* Put all of the mappings in the argument into this wrapped map
*/
public void putAll( final java.util.Map map ) {
if ( map == null || map.size() == 0 ) {
return;
}
for ( Iterator i = map.keySet().iterator(); i.hasNext(); ) {
Object key = (Object) i.next();
put( key, map.get( key ) );
}
}
/**
* Returns a set view of the mappings in the wrapped map
*/
public Set entrySet() {
Set entries = new HashSet();
if ( size() == 0 ) {
return entries;
}
for ( Iterator i = keySet().iterator(); i.hasNext(); ) {
Object key = i.next();
Object value = get( key );
Entry entry = new Entry( key, value );
entries.add( entry );
}
return entries;
}
/**
* Removes a mapping from this map
*/
public Object remove( final Object key ) {
Reference ref = (Reference) getMap().remove( key );
if ( ref != null ) {
return ref.get();
}
return null;
}
/**
* Clears all mappings
*/
public void clear() {
getMap().clear();
}
/**
* Calculates the hash code for this map
*/
public int hashCode() {
return getMap().hashCode();
}
/**
* Returns the domain of the mappings
*/
public Set keySet() {
return getMap().keySet();
}
/**
* Answers whether there are any mappings
*/
public boolean isEmpty() {
return getMap().isEmpty();
}
/**
* Answers whether this map and the argument are 'the same'
*/
public boolean equals( final Object object ) {
return getMap().equals( object );
}
/**
* Returns the number of mappings in this map
*/
public int size() {
return getMap().size();
}
// Inner Classes
// ---------------------------------------------------------------------
/**
* A map entry, which is backed by this RefHashMap
*/
class Entry implements Map.Entry {
/**
* Constructor
*/
public Entry( Object key, Object value ) {
this.key = key;
this.value = value;
}
// Map.Entry interface
// -----------------------------------------------------------
/**
* Retrieves the key of this mapping
*/
public Object getKey() {
return key;
}
/**
* Retrieves the value of this mapping
*/
public Object getValue() {
return value;
}
/**
* Sets the value of this mapping
*/
public Object setValue( Object value ) {
this.value = value;
put( key, value );
return value;
}
/**
* Return the hash code of this mapping.
* This algorithm was taken from the JavaDoc for Map.Entry
*/
public int hashCode() {
return ( getKey() == null ? 0 : getKey().hashCode() ) ^
( getValue() == null ? 0 : getValue().hashCode() );
}
/** The domain of this mapping */
private Object key;
/** The range of this mapping */
private Object value;
}
/**
* Returns a reference to the argument.
* Override this method to make wrapped maps for other Reference types
*/
protected Reference createReference( Object referent ) {
return new SoftReference( referent );
}
/**
* Retrieves the wrapped HashMap
* @return The wrapped HashMap
*/
protected Map getMap() {
return hashMap;
}
}

View File

@ -1,88 +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.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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();
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/SortedBag.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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();
}

View File

@ -1,120 +1,120 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/TransformIterator.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Enumeration;
import java.util.Iterator;
/** A Proxy {@link Iterator Iterator} which uses a {@link Transformer Transformer} instance to
* transform the contents of the {@link Iterator Iterator} into some other form
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class TransformIterator extends ProxyIterator {
/** Holds value of property transformer. */
private Transformer transformer;
public TransformIterator() {
}
public TransformIterator( Iterator iterator ) {
super( iterator );
}
public TransformIterator( Iterator iterator, Transformer transformer ) {
super( iterator );
this.transformer = transformer;
}
// Iterator interface
//-------------------------------------------------------------------------
public Object next() {
return transform( super.next() );
}
// Properties
//-------------------------------------------------------------------------
/** Getter for property transformer.
* @return Value of property transformer.
*/
public Transformer getTransformer() {
return transformer;
}
/** Setter for property transformer.
* @param transformer New value of property transformer.
*/
public void setTransformer(Transformer transformer) {
this.transformer = transformer;
}
// Implementation methods
//-------------------------------------------------------------------------
protected Object transform( Object source ) {
Transformer transformer = getTransformer();
if ( transformer != null ) {
return transformer.transform( source );
}
return source;
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/TransformIterator.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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.Enumeration;
import java.util.Iterator;
/** A Proxy {@link Iterator Iterator} which uses a {@link Transformer Transformer} instance to
* transform the contents of the {@link Iterator Iterator} into some other form
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public class TransformIterator extends ProxyIterator {
/** Holds value of property transformer. */
private Transformer transformer;
public TransformIterator() {
}
public TransformIterator( Iterator iterator ) {
super( iterator );
}
public TransformIterator( Iterator iterator, Transformer transformer ) {
super( iterator );
this.transformer = transformer;
}
// Iterator interface
//-------------------------------------------------------------------------
public Object next() {
return transform( super.next() );
}
// Properties
//-------------------------------------------------------------------------
/** Getter for property transformer.
* @return Value of property transformer.
*/
public Transformer getTransformer() {
return transformer;
}
/** Setter for property transformer.
* @param transformer New value of property transformer.
*/
public void setTransformer(Transformer transformer) {
this.transformer = transformer;
}
// Implementation methods
//-------------------------------------------------------------------------
protected Object transform( Object source ) {
Transformer transformer = getTransformer();
if ( transformer != null ) {
return transformer.transform( source );
}
return source;
}
}

View File

@ -1,73 +1,73 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Transformer.java,v 1.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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;
/** An object capable of transforming an input object into some output object.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface Transformer {
/** Transforms the input object (leaving it unchanged) into some output object.
* @return the transformation of the input object to the output object
*/
public Object transform(Object input);
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Transformer.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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;
/** An object capable of transforming an input object into some output object.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
*/
public interface Transformer {
/** Transforms the input object (leaving it unchanged) into some output object.
* @return the transformation of the input object to the output object
*/
public Object transform(Object input);
}

View File

@ -1,117 +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.2 2002/02/10 08:07:42 jstrachan Exp $
* $Revision: 1.2 $
* $Date: 2002/02/10 08:07:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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();
}
}
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/TreeBag.java,v 1.3 2002/03/13 05:40:31 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/03/13 05:40:31 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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();
}
}