--- jsr166/src/extra166y/CustomConcurrentHashMap.java 2009/06/30 14:56:53 1.2 +++ jsr166/src/extra166y/CustomConcurrentHashMap.java 2009/11/22 18:58:57 1.8 @@ -31,17 +31,17 @@ import sun.misc.Unsafe; * establish a computed value, along with * RemappingFunctions that can be used in method * {@link CustomConcurrentHashMap#compute} to atomically - * replace values. + * replace values. * *
  • Factory methods returning specialized forms for int * keys and/or values, that may be more space-efficient * * - * + * * Per-map settings are established in constructors, as in the * following usages (that assume static imports to simplify expression * of configuration parameters): - * + * *
      * {@code
      * identityMap = new CustomConcurrentHashMap
    @@ -55,7 +55,7 @@ import sun.misc.Unsafe;
      *          public boolean equal(Person k, Object x) {
      *            return x instanceOf Person && k.name.equals(((Person)x).name);
      *          }
    - *          public int hash(Object x) { 
    + *          public int hash(Object x) {
      *             return (x instanceOf Person)? ((Person)x).name.hashCode() : 0;
      *          }
      *        },
    @@ -69,7 +69,7 @@ import sun.misc.Unsafe;
      * and identity-based equality for keys. The third usage
      * illustrates a map with a custom Equivalence that looks only at the
      * name field of a (fictional) Person class.
    - * 
    + *
      * 

    This class also includes nested class {@link KeySet} * that provides space-efficient Set views of maps, also supporting * method intern, which may be of use in canonicalizing @@ -81,7 +81,7 @@ import sun.misc.Unsafe; * via a background thread common across all maps. Because of the * potential for asynchronous clearing of References, methods such as * containsValue have weaker guarantees than you might - * expect even in the absence of other explicity concurrent + * expect even in the absence of other explicitly concurrent * operations. For example containsValue(value) may * return true even if value is no longer available upon * return from the method. @@ -126,7 +126,7 @@ public class CustomConcurrentHashMap, Serializable { private static final long serialVersionUID = 7259069246764182397L; public final boolean equal(Object a, Object b) { return a == b; } public final int hash(Object a) { return System.identityHashCode(a); } } - static final class EquivalenceUsingEquals + static final class EquivalenceUsingEquals implements Equivalence, Serializable { private static final long serialVersionUID = 7259069247764182397L; public final boolean equal(Object a, Object b) { return a.equals(b); } @@ -212,19 +212,19 @@ public class CustomConcurrentHashMap IDENTITY = + public static final Equivalence IDENTITY = new EquivalenceUsingIdentity(); - + /** * An Equivalence object performing {@link Object#equals} based comparisons * and using {@link Object#hashCode} hashing */ - public static final Equivalence EQUALS = + public static final Equivalence EQUALS = new EquivalenceUsingEquals(); /** * A function computing a mapping from the given key to a value, - * or null if there is no mapping. + * or null if there is no mapping. */ public static interface MappingFunction { /** @@ -275,14 +275,15 @@ public class CustomConcurrentHashMap= MAX_SEGMENT_CAPACITY) return oldTable; - Node[] newTable = + Node[] newTable = (Node[])new Node[oldCapacity<<1]; int sizeMask = newTable.length - 1; NodeFactory fac = cchm.factory; @@ -436,7 +437,7 @@ public class CustomConcurrentHashMap keyEquivalence; - + /** * Equivalence object for values */ final Equivalence valueEquivalence; - + /** * The initial size of Segment tables when they are first constructed */ @@ -515,9 +516,9 @@ public class CustomConcurrentHashMap>> SEGMENT_BITS); if (sc < MIN_SEGMENT_CAPACITY) sc = MIN_SEGMENT_CAPACITY; - else if (sc > MAX_SEGMENT_CAPACITY) - sc = MAX_SEGMENT_CAPACITY; - this.initialSegmentCapacity = sc; + int capacity = MIN_SEGMENT_CAPACITY; // ensure power of two + while (capacity < sc) + capacity <<= 1; + if (capacity > MAX_SEGMENT_CAPACITY) + capacity = MAX_SEGMENT_CAPACITY; + this.initialSegmentCapacity = capacity; } this.segments = (Segment[])new Segment[NSEGMENTS]; } @@ -554,7 +558,7 @@ public class CustomConcurrentHashMap valueEquivalence, int expectedSize) { - this(keyStrength.getName(), keyEquivalence, + this(keyStrength.getName(), keyEquivalence, valueStrength.getName(), valueEquivalence, expectedSize); } @@ -600,7 +604,7 @@ public class CustomConcurrentHashMap keyEquivalence, int expectedSize) { return new CustomConcurrentHashMap - (keyStrength.getName(), keyEquivalence, INT_STRING, EQUALS, + (keyStrength.getName(), keyEquivalence, INT_STRING, EQUALS, expectedSize); } @@ -614,7 +618,7 @@ public class CustomConcurrentHashMap newIntKeyIntValueMap(int expectedSize) { return new CustomConcurrentHashMap - (INT_STRING, EQUALS, INT_STRING, EQUALS, + (INT_STRING, EQUALS, INT_STRING, EQUALS, expectedSize); } @@ -629,7 +633,7 @@ public class CustomConcurrentHashMapnull if + * @return the value associated with the key or null if * there is no mapping. * @throws NullPointerException if the specified key is null */ @@ -712,7 +716,7 @@ public class CustomConcurrentHashMap * * except that the action is performed atomically. Some - * attemmpted operations on this map by other threads may be + * attempted operations on this map by other threads may be * blocked while computation is in progress. Because this function * is invoked within atomicity control, the computation should be * short and simple. The most common usage is to construct a new @@ -1086,10 +1090,10 @@ public class CustomConcurrentHashMapnull if the computation * returned null. - * @throws NullPointerException if the specified key or mappingFunction + * @throws NullPointerException if the specified key or mappingFunction * is null, * @throws RuntimeException or Error if the mappingFunction does so, * in which case the mapping is left unestablished. @@ -1111,7 +1115,7 @@ public class CustomConcurrentHashMap - * - * except that the action is performed atomically. Some attemmpted + * + * except that the action is performed atomically. Some attempted * operations on this map by other threads may be blocked while * computation is in progress. * @@ -1161,8 +1165,8 @@ public class CustomConcurrentHashMapnull if the computation returned null - * @throws NullPointerException if the specified key or remappingFunction + * null if the computation returned null + * @throws NullPointerException if the specified key or remappingFunction * is null, * @throws RuntimeException or Error if the remappingFunction does so, * in which case the mapping is left in its previous state @@ -1183,7 +1187,7 @@ public class CustomConcurrentHashMap= 0) { Segment seg = segments[nextSegmentIndex--]; Node[] t; - if (seg != null && + if (seg != null && (t = seg.getTableForTraversal()) != null) { currentTable = t; nextTableIndex = t.length - 1; @@ -1287,7 +1291,7 @@ public class CustomConcurrentHashMap nextEntry() { if (nextNode == null) throw new NoSuchElementException(); - WriteThroughEntry e = new WriteThroughEntry((K)nextKey, + WriteThroughEntry e = new WriteThroughEntry((K)nextKey, (V)nextValue); advance(); return e; @@ -1330,10 +1334,10 @@ public class CustomConcurrentHashMap { - public K next() { - return super.nextKey(); + public K next() { + return super.nextKey(); } } @@ -1341,14 +1345,14 @@ public class CustomConcurrentHashMap { - public V next() { - return super.nextValue(); + public V next() { + return super.nextValue(); } } - final class EntryIterator extends HashIterator + final class EntryIterator extends HashIterator implements Iterator> { public Map.Entry next() { return super.nextEntry(); @@ -1403,7 +1407,7 @@ public class CustomConcurrentHashMap e = (Map.Entry)o; V v = CustomConcurrentHashMap.this.get(e.getKey()); - return v != null && + return v != null && valueEquivalence.equal(v, e.getValue()); } public boolean remove(Object o) { @@ -1501,7 +1505,7 @@ public class CustomConcurrentHashMap m = (Map) o; @@ -1602,11 +1606,11 @@ public class CustomConcurrentHashMap equivalence, int expectedSize) { this.cchm = new CustomConcurrentHashMap - (strength.getName(), equivalence, + (strength.getName(), equivalence, SELF_STRING, equivalence, expectedSize); } @@ -1614,15 +1618,15 @@ public class CustomConcurrentHashMaptrue if this set contains an * element equivalent to the given element with respect @@ -1633,7 +1637,7 @@ public class CustomConcurrentHashMapweakly consistent iterator over the * elements in this set, that may reflect some, all or none of @@ -1644,7 +1648,7 @@ public class CustomConcurrentHashMap iterator() { return cchm.keyIterator(); } - + /** * Adds the specified element to this set if there is not * already an element equivalent to the given element with @@ -1686,7 +1690,7 @@ public class CustomConcurrentHashMap r = q.remove(); if (r instanceof Reclaimable) ((Reclaimable)r).onReclamation(); - } catch (InterruptedException e) { - /* ignore */ + } catch (InterruptedException e) { + /* ignore */ } } } @@ -1758,29 +1762,29 @@ public class CustomConcurrentHashMap