Change parentage of these classes so that they subclass ArrayList,

HashMap, and TreeMap respectively.  This improves the ability to
substitute "fast" versions for the use of the regular collection classes.

Implement clone(), equals(), and hashCode() methods in accordance with the
contracts specified in the Collections classes APIs.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130444 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Craig R. McClanahan 2001-04-21 12:19:57 +00:00
parent a784206a3a
commit fff00791ed
3 changed files with 322 additions and 24 deletions

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/FastArrayList.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $
* $Revision: 1.1 $
* $Date: 2001/04/16 22:42:04 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/FastArrayList.java,v 1.2 2001/04/21 12:19:57 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2001/04/21 12:19:57 $
*
* ====================================================================
*
@ -93,14 +93,11 @@ import java.util.ListIterator;
* <code>java.util.ArrayList</code> directly (with no synchronization), for
* maximum performance.</p>
*
* <p><strong>NOTE</strong>: The following methods are <strong>NOT</strong>
* overridden: clone(), equals(Object), hashCode().</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2001/04/16 22:42:04 $
* @version $Revision: 1.2 $ $Date: 2001/04/21 12:19:57 $
*/
public class FastArrayList implements List, Cloneable, Serializable {
public class FastArrayList extends ArrayList {
// ----------------------------------------------------------- Constructors
@ -300,6 +297,26 @@ public class FastArrayList implements List, Cloneable, Serializable {
}
/**
* Return a shallow copy of this <code>FastArrayList</code> instance.
* The elements themselves are not copied.
*/
public Object clone() {
FastArrayList results = null;
if (fast) {
results = new FastArrayList(list);
} else {
synchronized (list) {
results = new FastArrayList(list);
}
}
results.setFast(getFast());
return (results);
}
/**
* Return <code>true</code> if this list contains the specified element.
*
@ -361,6 +378,51 @@ public class FastArrayList implements List, Cloneable, Serializable {
}
/**
* Compare the specified object with this list for equality. This
* implementation uses exactly the code that is used to define the
* list equals function in the documentation for the
* <code>List.equals</code> method.
*
* @param o Object to be compared to this list
*/
public boolean equals(Object o) {
// Simple tests that require no synchronization
if (o == this)
return (true);
else if (!(o instanceof List))
return (false);
List lo = (List) o;
// Compare the sets of elements for equality
if (fast) {
ListIterator li1 = list.listIterator();
ListIterator li2 = lo.listIterator();
while (li1.hasNext() && li2.hasNext()) {
Object o1 = li1.next();
Object o2 = li2.next();
if (!(o1 == null ? o2 == null : o1.equals(o2)))
return (false);
}
return (!(li1.hasNext() || li2.hasNext()));
} else {
synchronized (list) {
ListIterator li1 = list.listIterator();
ListIterator li2 = lo.listIterator();
while (li1.hasNext() && li2.hasNext()) {
Object o1 = li1.next();
Object o2 = li2.next();
if (!(o1 == null ? o2 == null : o1.equals(o2)))
return (false);
}
return (!(li1.hasNext() || li2.hasNext()));
}
}
}
/**
* Return the element at the specified position in the list.
*
@ -381,6 +443,36 @@ public class FastArrayList implements List, Cloneable, Serializable {
}
/**
* Return the hash code value for this list. This implementation uses
* exactly the code that is used to define the list hash function in the
* documentation for the <code>List.hashCode</code> method.
*/
public int hashCode() {
if (fast) {
int hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext()) {
Object o = i.next();
hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode());
}
return (hashCode);
} else {
synchronized (list) {
int hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext()) {
Object o = i.next();
hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode());
}
return (hashCode);
}
}
}
/**
* Search for the first occurrence of the given argument, testing
* for equality using the <code>equals()</code> method, and return

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/FastHashMap.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $
* $Revision: 1.1 $
* $Date: 2001/04/16 22:42:04 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/FastHashMap.java,v 1.2 2001/04/21 12:19:57 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2001/04/21 12:19:57 $
*
* ====================================================================
*
@ -68,6 +68,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@ -93,14 +94,11 @@ import java.util.Set;
* <code>java.util.HashMap</code> directly (with no synchronization), for
* maximum performance.</p>
*
* <p><strong>NOTE</strong>: The following methods are <strong>NOT</strong>
* overridden: clone(), equals(Object), hashCode().</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2001/04/16 22:42:04 $
* @version $Revision: 1.2 $ $Date: 2001/04/21 12:19:57 $
*/
public class FastHashMap implements Map, Cloneable, Serializable {
public class FastHashMap extends HashMap {
// ----------------------------------------------------------- Constructors
@ -206,6 +204,26 @@ public class FastHashMap implements Map, Cloneable, Serializable {
}
/**
* Return a shallow copy of this <code>FastHashMap</code> instance.
* The keys and values themselves are not copied.
*/
public Object clone() {
FastHashMap results = null;
if (fast) {
results = new FastHashMap(map);
} else {
synchronized (map) {
results = new FastHashMap(map);
}
}
results.setFast(getFast());
return (results);
}
/**
* Return <code>true</code> if this map contains a mapping for the
* specified key.
@ -261,6 +279,65 @@ public class FastHashMap implements Map, Cloneable, Serializable {
}
/**
* Compare the specified object with this list for equality. This
* implementation uses exactly the code that is used to define the
* list equals function in the documentation for the
* <code>Map.equals</code> method.
*
* @param o Object to be compared to this list
*/
public boolean equals(Object o) {
// Simple tests that require no synchronization
if (o == this)
return (true);
else if (!(o instanceof Map))
return (false);
Map mo = (Map) o;
// Compare the two maps for equality
if (fast) {
if (mo.size() != map.size())
return (false);
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Entry e = (Entry) i.next();
Object key = e.getKey();
Object value = e.getValue();
if (value == null) {
if (!(mo.get(key) == null && mo.containsKey(key)))
return (false);
} else {
if (!value.equals(mo.get(key)))
return (false);
}
}
return (true);
} else {
synchronized (map) {
if (mo.size() != map.size())
return (false);
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Entry e = (Entry) i.next();
Object key = e.getKey();
Object value = e.getValue();
if (value == null) {
if (!(mo.get(key) == null && mo.containsKey(key)))
return (false);
} else {
if (!value.equals(mo.get(key)))
return (false);
}
}
return (true);
}
}
}
/**
* Return the value to which this map maps the specified key. Returns
* <code>null</code> if the map contains no mapping for this key, or if
@ -282,6 +359,32 @@ public class FastHashMap implements Map, Cloneable, Serializable {
}
/**
* Return the hash code value for this map. This implementation uses
* exactly the code that is used to define the list hash function in the
* documentation for the <code>Map.hashCode</code> method.
*/
public int hashCode() {
if (fast) {
int h = 0;
Iterator i = map.entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return (h);
} else {
synchronized (map) {
int h = 0;
Iterator i = map.entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return (h);
}
}
}
/**
* Return <code>true</code> if this map contains no mappings.
*/

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/FastTreeMap.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $
* $Revision: 1.1 $
* $Date: 2001/04/16 22:42:04 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/FastTreeMap.java,v 1.2 2001/04/21 12:19:57 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2001/04/21 12:19:57 $
*
* ====================================================================
*
@ -68,6 +68,7 @@ import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
@ -95,14 +96,11 @@ import java.util.TreeMap;
* <code>java.util.TreeMap</code> directly (with no synchronization), for
* maximum performance.</p>
*
* <p><strong>NOTE</strong>: The following methods are <strong>NOT</strong>
* overridden: clone(), equals(Object), hashCode().</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2001/04/16 22:42:04 $
* @version $Revision: 1.2 $ $Date: 2001/04/21 12:19:57 $
*/
public class FastTreeMap implements Map, Cloneable, Serializable {
public class FastTreeMap extends TreeMap {
// ----------------------------------------------------------- Constructors
@ -209,6 +207,26 @@ public class FastTreeMap implements Map, Cloneable, Serializable {
}
/**
* Return a shallow copy of this <code>FastTreeMap</code> instance.
* The keys and values themselves are not copied.
*/
public Object clone() {
FastTreeMap results = null;
if (fast) {
results = new FastTreeMap(map);
} else {
synchronized (map) {
results = new FastTreeMap(map);
}
}
results.setFast(getFast());
return (results);
}
/**
* Return the comparator used to order this map, or <code>null</code>
* if this map uses its keys' natural order.
@ -281,6 +299,65 @@ public class FastTreeMap implements Map, Cloneable, Serializable {
}
/**
* Compare the specified object with this list for equality. This
* implementation uses exactly the code that is used to define the
* list equals function in the documentation for the
* <code>Map.equals</code> method.
*
* @param o Object to be compared to this list
*/
public boolean equals(Object o) {
// Simple tests that require no synchronization
if (o == this)
return (true);
else if (!(o instanceof Map))
return (false);
Map mo = (Map) o;
// Compare the two maps for equality
if (fast) {
if (mo.size() != map.size())
return (false);
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Entry e = (Entry) i.next();
Object key = e.getKey();
Object value = e.getValue();
if (value == null) {
if (!(mo.get(key) == null && mo.containsKey(key)))
return (false);
} else {
if (!value.equals(mo.get(key)))
return (false);
}
}
return (true);
} else {
synchronized (map) {
if (mo.size() != map.size())
return (false);
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Entry e = (Entry) i.next();
Object key = e.getKey();
Object value = e.getValue();
if (value == null) {
if (!(mo.get(key) == null && mo.containsKey(key)))
return (false);
} else {
if (!value.equals(mo.get(key)))
return (false);
}
}
return (true);
}
}
}
/**
* Return the first (lowest) key currently in this sorted map.
*/
@ -318,6 +395,32 @@ public class FastTreeMap implements Map, Cloneable, Serializable {
}
/**
* Return the hash code value for this map. This implementation uses
* exactly the code that is used to define the list hash function in the
* documentation for the <code>Map.hashCode</code> method.
*/
public int hashCode() {
if (fast) {
int h = 0;
Iterator i = map.entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return (h);
} else {
synchronized (map) {
int h = 0;
Iterator i = map.entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return (h);
}
}
}
/**
* Return a view of the portion of this map whose keys are strictly
* less than the specified key.