src/share/classes/sun/swing/BakedArrayList.java

Print this page




  27 import java.util.*;
  28 
  29 /**
  30  * <b>WARNING:</b> This class is an implementation detail and is only
  31  * public so that it can be used by two packages. You should NOT consider
  32  * this public API.
  33  * <p>
  34  * <b>WARNING 2:</b> This is not a general purpose List implementation! It
  35  * has a specific use and will not work correctly if you use it outside of
  36  * its use.
  37  * <p>
  38  * A specialized ArrayList that caches its hashCode as well as overriding
  39  * equals to avoid creating an Iterator. This class is useful in scenarios
  40  * where the list won't change and you want to avoid the overhead of hashCode
  41  * iterating through the elements invoking hashCode. This also assumes you'll
  42  * only ever compare a BakedArrayList to another BakedArrayList.
  43  *
  44  * @author Scott Violet
  45  */
  46 @SuppressWarnings("serial") // JDK-implementation class
  47 public class BakedArrayList extends ArrayList {
  48     /**
  49      * The cached hashCode.
  50      */
  51     private int _hashCode;
  52 
  53     public BakedArrayList(int size) {
  54         super(size);
  55     }
  56 
  57     public BakedArrayList(java.util.List data) {
  58         this(data.size());
  59         for (int counter = 0, max = data.size(); counter < max; counter++){
  60             add(data.get(counter));
  61         }
  62         cacheHashCode();
  63     }
  64 
  65     /**
  66      * Caches the hash code. It is assumed you won't modify the list, or that
  67      * if you do you'll call cacheHashCode again.
  68      */
  69     public void cacheHashCode() {
  70         _hashCode = 1;
  71         for (int counter = size() - 1; counter >= 0; counter--) {
  72             _hashCode = 31 * _hashCode + get(counter).hashCode();
  73         }
  74     }
  75 
  76     public int hashCode() {
  77         return _hashCode;


  27 import java.util.*;
  28 
  29 /**
  30  * <b>WARNING:</b> This class is an implementation detail and is only
  31  * public so that it can be used by two packages. You should NOT consider
  32  * this public API.
  33  * <p>
  34  * <b>WARNING 2:</b> This is not a general purpose List implementation! It
  35  * has a specific use and will not work correctly if you use it outside of
  36  * its use.
  37  * <p>
  38  * A specialized ArrayList that caches its hashCode as well as overriding
  39  * equals to avoid creating an Iterator. This class is useful in scenarios
  40  * where the list won't change and you want to avoid the overhead of hashCode
  41  * iterating through the elements invoking hashCode. This also assumes you'll
  42  * only ever compare a BakedArrayList to another BakedArrayList.
  43  *
  44  * @author Scott Violet
  45  */
  46 @SuppressWarnings("serial") // JDK-implementation class
  47 public class BakedArrayList extends ArrayList<Object> {
  48     /**
  49      * The cached hashCode.
  50      */
  51     private int _hashCode;
  52 
  53     public BakedArrayList(int size) {
  54         super(size);
  55     }
  56 
  57     public BakedArrayList(java.util.List<?> data) {
  58         this(data.size());
  59         for (int counter = 0, max = data.size(); counter < max; counter++){
  60             add(data.get(counter));
  61         }
  62         cacheHashCode();
  63     }
  64 
  65     /**
  66      * Caches the hash code. It is assumed you won't modify the list, or that
  67      * if you do you'll call cacheHashCode again.
  68      */
  69     public void cacheHashCode() {
  70         _hashCode = 1;
  71         for (int counter = size() - 1; counter >= 0; counter--) {
  72             _hashCode = 31 * _hashCode + get(counter).hashCode();
  73         }
  74     }
  75 
  76     public int hashCode() {
  77         return _hashCode;