craigmcc    01/04/21 05:19:58

  Modified:    collections/src/java/org/apache/commons/collections
                        FastArrayList.java FastHashMap.java
                        FastTreeMap.java
  Log:
  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.
  
  Revision  Changes    Path
  1.2       +100 -8    
jakarta-commons/collections/src/java/org/apache/commons/collections/FastArrayList.java
  
  Index: FastArrayList.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/FastArrayList.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FastArrayList.java        2001/04/16 22:42:04     1.1
  +++ FastArrayList.java        2001/04/21 12:19:57     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/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/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 @@
    * <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
  @@ -301,6 +298,26 @@
   
   
       /**
  +     * 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.
        *
        * @param element The element to test for
  @@ -362,6 +379,51 @@
   
   
       /**
  +     * 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.
        *
        * @param index The index of the element to return
  @@ -375,6 +437,36 @@
           } else {
               synchronized (list) {
                   return (list.get(index));
  +            }
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * 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);
               }
           }
   
  
  
  
  1.2       +111 -8    
jakarta-commons/collections/src/java/org/apache/commons/collections/FastHashMap.java
  
  Index: FastHashMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/FastHashMap.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FastHashMap.java  2001/04/16 22:42:04     1.1
  +++ FastHashMap.java  2001/04/21 12:19:57     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/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/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.HashMap;
   import java.util.Iterator;
   import java.util.Map;
  +import java.util.Map.Entry;
   import java.util.Set;
   
   
  @@ -93,14 +94,11 @@
    * <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
  @@ -207,6 +205,26 @@
   
   
       /**
  +     * 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.
        *
  @@ -262,6 +280,65 @@
   
   
       /**
  +     * 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
        * there is a mapping with a value of <code>null</code>.  Use the
  @@ -276,6 +353,32 @@
           } else {
               synchronized (map) {
                   return (map.get(key));
  +            }
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * 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);
               }
           }
   
  
  
  
  1.2       +111 -8    
jakarta-commons/collections/src/java/org/apache/commons/collections/FastTreeMap.java
  
  Index: FastTreeMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/FastTreeMap.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FastTreeMap.java  2001/04/16 22:42:04     1.1
  +++ FastTreeMap.java  2001/04/21 12:19:57     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/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/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.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 @@
    * <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
  @@ -210,6 +208,26 @@
   
   
       /**
  +     * 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.
        */
  @@ -282,6 +300,65 @@
   
   
       /**
  +     * 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.
        */
       public Object firstKey() {
  @@ -312,6 +389,32 @@
           } else {
               synchronized (map) {
                   return (map.get(key));
  +            }
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * 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);
               }
           }
   
  
  
  

Reply via email to