ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/CustomConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/extra166y/CustomConcurrentHashMap.java (file contents):
Revision 1.13 by jsr166, Sat Oct 16 16:40:44 2010 UTC vs.
Revision 1.25 by jsr166, Wed Jan 9 02:51:36 2013 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   package extra166y;
# Line 56 | Line 56 | import sun.misc.Unsafe;
56   *            return x instanceof Person && k.name.equals(((Person)x).name);
57   *          }
58   *          public int hash(Object x) {
59 < *             return (x instanceof Person)? ((Person)x).name.hashCode() : 0;
59 > *             return (x instanceof Person) ? ((Person)x).name.hashCode() : 0;
60   *          }
61   *        },
62   *      STRONG, EQUALS, 0);
# Line 236 | Line 236 | public class CustomConcurrentHashMap<K,
236           * simple. The most common usage is to construct a new object
237           * serving as an initial mapped value.
238           *
239 <         * @param key the (nonnull) key
239 >         * @param key the (non-null) key
240           * @return a value, or null if none
241           */
242          V map(K key);
# Line 320 | Line 320 | public class CustomConcurrentHashMap<K,
320           * Returns the value established during the creation of this
321           * node or, if since updated, the value set by the most
322           * recent call to setValue, or throws an exception if
323 <         * value could not be computed
323 >         * value could not be computed.
324           * @return the value
325           * @throws RuntimeException or Error if computeValue failed
326           */
# Line 544 | Line 544 | public class CustomConcurrentHashMap<K,
544      }
545  
546      /**
547 <     * Creates a new CustomConcurrentHashMap with the given parameters
547 >     * Creates a new CustomConcurrentHashMap with the given parameters.
548       * @param keyStrength the strength for keys
549       * @param keyEquivalence the Equivalence to use for keys
550       * @param valueStrength the strength for values
# Line 573 | Line 573 | public class CustomConcurrentHashMap<K,
573  
574      /**
575       * Returns a new map using Integer keys and the given value
576 <     * parameters
576 >     * parameters.
577       * @param valueStrength the strength for values
578       * @param valueEquivalence the Equivalence to use for values
579       * @param expectedSize an estimate of the number of elements
# Line 591 | Line 591 | public class CustomConcurrentHashMap<K,
591      }
592  
593      /**
594 <     * Returns a new map using the given key parameters and Integer values
594 >     * Returns a new map using the given key parameters and Integer values.
595       * @param keyStrength the strength for keys
596       * @param keyEquivalence the Equivalence to use for keys
597       * @param expectedSize an estimate of the number of elements
# Line 609 | Line 609 | public class CustomConcurrentHashMap<K,
609      }
610  
611      /**
612 <     * Returns a new map using Integer keys and values
612 >     * Returns a new map using Integer keys and values.
613       * @param expectedSize an estimate of the number of elements
614       * that will be held in the map. If no estimate is known,
615       * zero is an acceptable value.
# Line 623 | Line 623 | public class CustomConcurrentHashMap<K,
623      }
624  
625      /**
626 <     * Returns the segment for traversing table for key with given hash
626 >     * Returns the segment for traversing table for key with given hash.
627       * @param hash the hash code for the key
628       * @return the segment, or null if not yet initialized
629       */
# Line 656 | Line 656 | public class CustomConcurrentHashMap<K,
656      }
657  
658      /**
659 <     * Returns node for key, or null if none
659 >     * Returns node for key, or null if none.
660       */
661      final Node findNode(Object key, int hash, Segment seg) {
662          if (seg != null) {
# Line 697 | Line 697 | public class CustomConcurrentHashMap<K,
697      /**
698       * Returns the value associated with a key equivalent to the given
699       * key with respect to this map's key Equivalence, or {@code null}
700 <     * if no such mapping exists
700 >     * if no such mapping exists.
701       *
702       * @param  key   possible key
703       * @return the value associated with the key or <tt>null</tt> if
# Line 942 | Line 942 | public class CustomConcurrentHashMap<K,
942      }
943  
944      /**
945 <     * Remove node if its key or value are null
945 >     * Removes node if its key or value are null.
946       */
947      final void removeIfReclaimed(Node r) {
948          int hash = r.getLocator();
# Line 1010 | Line 1010 | public class CustomConcurrentHashMap<K,
1010              if (seg != null && seg.getTableForTraversal() != null)
1011                  sum += seg.count;
1012          }
1013 <        return sum >= Integer.MAX_VALUE? Integer.MAX_VALUE : (int)sum;
1013 >        return (sum >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) sum;
1014      }
1015  
1016      /**
# Line 1069 | Line 1069 | public class CustomConcurrentHashMap<K,
1069      /**
1070       * If the specified key is not already associated with a value,
1071       * computes its value using the given mappingFunction, and if
1072 <     * nonnull, enters it into the map.  This is equivalent to
1072 >     * non-null, enters it into the map.  This is equivalent to
1073       *
1074       * <pre>
1075       *   if (map.containsKey(key))
# Line 1158 | Line 1158 | public class CustomConcurrentHashMap<K,
1158       * <pre>
1159       * map.compute(word, new RemappingFunction&lt;String,Integer&gt;() {
1160       *   public Integer remap(String k, Integer v) {
1161 <     *     return v == null? 1 : v + 1;
1161 >     *     return (v == null) ? 1 : v + 1;
1162       *   }});
1163       * </pre>
1164       *
# Line 1414 | Line 1414 | public class CustomConcurrentHashMap<K,
1414              if (!(o instanceof Map.Entry))
1415                  return false;
1416              Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1417 <            return CustomConcurrentHashMap.this.remove(e.getKey(), e.getValue());
1417 >            return CustomConcurrentHashMap.this.remove(e.getKey(),
1418 >                                                       e.getValue());
1419          }
1420          public int size() {
1421              return CustomConcurrentHashMap.this.size();
# Line 1550 | Line 1551 | public class CustomConcurrentHashMap<K,
1551      }
1552  
1553      /**
1554 <     * Save the state of the instance to a stream (i.e., serialize
1555 <     * it).
1554 >     * Saves the state of the instance to a stream (i.e., serializes it).
1555 >     *
1556       * @param s the stream
1557       * @serialData
1558       * the key (Object) and value (Object)
# Line 1569 | Line 1570 | public class CustomConcurrentHashMap<K,
1570      }
1571  
1572      /**
1573 <     * Reconstitute the instance from a stream (i.e., deserialize it).
1573 >     * Reconstitutes the instance from a stream (that is, deserializes it).
1574       * @param s the stream
1575       */
1576      private void readObject(java.io.ObjectInputStream s)
# Line 1599 | Line 1600 | public class CustomConcurrentHashMap<K,
1600          final CustomConcurrentHashMap<K,K> cchm;
1601  
1602          /**
1603 <         * Creates a set with the given parameters
1603 >         * Creates a set with the given parameters.
1604           * @param strength the strength of elements
1605           * @param equivalence the Equivalence to use
1606           * @param expectedSize an estimate of the number of elements
# Line 1620 | Line 1621 | public class CustomConcurrentHashMap<K,
1621           * exists, else adds and returns the given element.
1622           *
1623           * @param e the element
1624 <         * @return e, or an element equivalent to e.
1624 >         * @return e, or an element equivalent to e
1625           */
1626          public K intern(K e) {
1627              K oldElement = cchm.doPut(e, e, true);
# Line 1975 | Line 1976 | public class CustomConcurrentHashMap<K,
1976          }
1977          public final Object getValue() {
1978              EmbeddedWeakReference vr = valueRef;
1979 <            return vr == null? null : vr.get();
1979 >            return (vr == null) ? null : vr.get();
1980          }
1981          public final void setValue(Object value) {
1982              if (value == null)
# Line 2043 | Line 2044 | public class CustomConcurrentHashMap<K,
2044          }
2045          public final Object getValue() {
2046              EmbeddedSoftReference vr = valueRef;
2047 <            return vr == null? null : vr.get();
2047 >            return (vr == null) ? null : vr.get();
2048          }
2049          public final void setValue(Object value) {
2050              if (value == null)
# Line 2114 | Line 2115 | public class CustomConcurrentHashMap<K,
2115  
2116      static abstract class WeakKeySelfValueNode
2117          extends WeakKeyNode {
2118 <        WeakKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) {
2118 >        WeakKeySelfValueNode(int locator, Object key,
2119 >                             CustomConcurrentHashMap cchm) {
2120              super(locator, key, cchm);
2121          }
2122          public final Object getValue() { return get(); }
# Line 2123 | Line 2125 | public class CustomConcurrentHashMap<K,
2125  
2126      static final class TerminalWeakKeySelfValueNode
2127          extends WeakKeySelfValueNode {
2128 <        TerminalWeakKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) {
2128 >        TerminalWeakKeySelfValueNode(int locator, Object key,
2129 >                                     CustomConcurrentHashMap cchm) {
2130              super(locator, key, cchm);
2131          }
2132          public final Node getLinkage() { return null; }
# Line 2133 | Line 2136 | public class CustomConcurrentHashMap<K,
2136      static final class LinkedWeakKeySelfValueNode
2137          extends WeakKeySelfValueNode {
2138          volatile Node linkage;
2139 <        LinkedWeakKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm,
2139 >        LinkedWeakKeySelfValueNode(int locator, Object key,
2140 >                                   CustomConcurrentHashMap cchm,
2141                                     Node linkage) {
2142              super(locator, key, cchm);
2143              this.linkage = linkage;
# Line 2162 | Line 2166 | public class CustomConcurrentHashMap<K,
2166      static abstract class WeakKeyStrongValueNode
2167          extends WeakKeyNode {
2168          volatile Object value;
2169 <        WeakKeyStrongValueNode(int locator, Object key, Object value, CustomConcurrentHashMap cchm) {
2169 >        WeakKeyStrongValueNode(int locator, Object key, Object value,
2170 >                               CustomConcurrentHashMap cchm) {
2171              super(locator, key, cchm);
2172              this.value = value;
2173          }
# Line 2173 | Line 2178 | public class CustomConcurrentHashMap<K,
2178      static final class TerminalWeakKeyStrongValueNode
2179          extends WeakKeyStrongValueNode {
2180          TerminalWeakKeyStrongValueNode(int locator,
2181 <                                       Object key, Object value, CustomConcurrentHashMap cchm) {
2181 >                                       Object key, Object value,
2182 >                                       CustomConcurrentHashMap cchm) {
2183              super(locator, key, value, cchm);
2184          }
2185          public final Node getLinkage() { return null; }
# Line 2184 | Line 2190 | public class CustomConcurrentHashMap<K,
2190          extends WeakKeyStrongValueNode {
2191          volatile Node linkage;
2192          LinkedWeakKeyStrongValueNode(int locator,
2193 <                                     Object key, Object value, CustomConcurrentHashMap cchm,
2193 >                                     Object key, Object value,
2194 >                                     CustomConcurrentHashMap cchm,
2195                                       Node linkage) {
2196              super(locator, key, value, cchm);
2197              this.linkage = linkage;
# Line 2275 | Line 2282 | public class CustomConcurrentHashMap<K,
2282          }
2283          public final Object getValue() {
2284              EmbeddedWeakReference vr = valueRef;
2285 <            return vr == null? null : vr.get();
2285 >            return (vr == null) ? null : vr.get();
2286          }
2287          public final void setValue(Object value) {
2288              if (value == null)
# Line 2338 | Line 2345 | public class CustomConcurrentHashMap<K,
2345          }
2346          public final Object getValue() {
2347              EmbeddedSoftReference vr = valueRef;
2348 <            return vr == null? null : vr.get();
2348 >            return (vr == null) ? null : vr.get();
2349          }
2350          public final void setValue(Object value) {
2351              if (value == null)
# Line 2409 | Line 2416 | public class CustomConcurrentHashMap<K,
2416  
2417      static abstract class SoftKeySelfValueNode
2418          extends SoftKeyNode {
2419 <        SoftKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) {
2419 >        SoftKeySelfValueNode(int locator, Object key,
2420 >                             CustomConcurrentHashMap cchm) {
2421              super(locator, key, cchm);
2422          }
2423          public final Object getValue() { return get(); }
# Line 2418 | Line 2426 | public class CustomConcurrentHashMap<K,
2426  
2427      static final class TerminalSoftKeySelfValueNode
2428          extends SoftKeySelfValueNode {
2429 <        TerminalSoftKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm) {
2429 >        TerminalSoftKeySelfValueNode(int locator, Object key,
2430 >                                     CustomConcurrentHashMap cchm) {
2431              super(locator, key, cchm);
2432          }
2433          public final Node getLinkage() { return null; }
# Line 2428 | Line 2437 | public class CustomConcurrentHashMap<K,
2437      static final class LinkedSoftKeySelfValueNode
2438          extends SoftKeySelfValueNode {
2439          volatile Node linkage;
2440 <        LinkedSoftKeySelfValueNode(int locator, Object key, CustomConcurrentHashMap cchm,
2440 >        LinkedSoftKeySelfValueNode(int locator, Object key,
2441 >                                   CustomConcurrentHashMap cchm,
2442                                     Node linkage) {
2443              super(locator, key, cchm);
2444              this.linkage = linkage;
# Line 2457 | Line 2467 | public class CustomConcurrentHashMap<K,
2467      static abstract class SoftKeyStrongValueNode
2468          extends SoftKeyNode {
2469          volatile Object value;
2470 <        SoftKeyStrongValueNode(int locator, Object key, Object value, CustomConcurrentHashMap cchm) {
2470 >        SoftKeyStrongValueNode(int locator, Object key, Object value,
2471 >                               CustomConcurrentHashMap cchm) {
2472              super(locator, key, cchm);
2473              this.value = value;
2474          }
# Line 2468 | Line 2479 | public class CustomConcurrentHashMap<K,
2479      static final class TerminalSoftKeyStrongValueNode
2480          extends SoftKeyStrongValueNode {
2481          TerminalSoftKeyStrongValueNode(int locator,
2482 <                                       Object key, Object value, CustomConcurrentHashMap cchm) {
2482 >                                       Object key, Object value,
2483 >                                       CustomConcurrentHashMap cchm) {
2484              super(locator, key, value, cchm);
2485          }
2486          public final Node getLinkage() { return null; }
# Line 2479 | Line 2491 | public class CustomConcurrentHashMap<K,
2491          extends SoftKeyStrongValueNode {
2492          volatile Node linkage;
2493          LinkedSoftKeyStrongValueNode(int locator,
2494 <                                     Object key, Object value, CustomConcurrentHashMap cchm,
2494 >                                     Object key, Object value,
2495 >                                     CustomConcurrentHashMap cchm,
2496                                       Node linkage) {
2497              super(locator, key, value, cchm);
2498              this.linkage = linkage;
# Line 2570 | Line 2583 | public class CustomConcurrentHashMap<K,
2583          }
2584          public final Object getValue() {
2585              EmbeddedWeakReference vr = valueRef;
2586 <            return vr == null? null : vr.get();
2586 >            return (vr == null) ? null : vr.get();
2587          }
2588          public final void setValue(Object value) {
2589              if (value == null)
# Line 2633 | Line 2646 | public class CustomConcurrentHashMap<K,
2646          }
2647          public final Object getValue() {
2648              EmbeddedSoftReference vr = valueRef;
2649 <            return vr == null? null : vr.get();
2649 >            return (vr == null) ? null : vr.get();
2650          }
2651          public final void setValue(Object value) {
2652              if (value == null)
# Line 2861 | Line 2874 | public class CustomConcurrentHashMap<K,
2874          }
2875          public final Object getValue() {
2876              EmbeddedWeakReference vr = valueRef;
2877 <            return vr == null? null : vr.get();
2877 >            return (vr == null) ? null : vr.get();
2878          }
2879          public final void setValue(Object value) {
2880              if (value == null)
# Line 2918 | Line 2931 | public class CustomConcurrentHashMap<K,
2931          volatile EmbeddedSoftReference valueRef;
2932          final CustomConcurrentHashMap cchm;
2933          IntKeySoftValueNode(int locator, Object key, Object value,
2934 <                               CustomConcurrentHashMap cchm) {
2934 >                            CustomConcurrentHashMap cchm) {
2935              super(locator, key);
2936              this.cchm = cchm;
2937              if (value != null)
# Line 2929 | Line 2942 | public class CustomConcurrentHashMap<K,
2942          }
2943          public final Object getValue() {
2944              EmbeddedSoftReference vr = valueRef;
2945 <            return vr == null? null : vr.get();
2945 >            return (vr == null) ? null : vr.get();
2946          }
2947          public final void setValue(Object value) {
2948              if (value == null)
# Line 2990 | Line 3003 | public class CustomConcurrentHashMap<K,
3003      static final long segmentsBase;
3004      static final int segmentsShift;
3005  
2993    private static Unsafe getUnsafe() throws Throwable {
2994        try {
2995            return Unsafe.getUnsafe();
2996        } catch (SecurityException se) {
2997            try {
2998                return java.security.AccessController.doPrivileged
2999                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
3000                        public Unsafe run() throws Exception {
3001                            return getUnsafePrivileged();
3002                        }});
3003            } catch (java.security.PrivilegedActionException e) {
3004                throw e.getCause();
3005            }
3006        }
3007    }
3008
3009    private static Unsafe getUnsafePrivileged()
3010        throws NoSuchFieldException, IllegalAccessException {
3011        Field f = Unsafe.class.getDeclaredField("theUnsafe");
3012        f.setAccessible(true);
3013        return (Unsafe) f.get(null);
3014    }
3015
3006      static {
3007          try {
3008              UNSAFE = getUnsafe();
# Line 3032 | Line 3022 | public class CustomConcurrentHashMap<K,
3022      }
3023  
3024      // Fenced store into segment table array. Unneeded when we have Fences
3025 <    static final  void storeNode(Node[] table,
3026 <                                 int i, Node r) {
3025 >    static final void storeNode(Node[] table,
3026 >                                int i, Node r) {
3027          long nodeOffset = ((long) i << tableShift) + tableBase;
3028          UNSAFE.putOrderedObject(table, nodeOffset, r);
3029      }
3030  
3031 <    static final  void storeSegment(Segment[] segs,
3032 <                                    int i, Segment s) {
3031 >    static final void storeSegment(Segment[] segs,
3032 >                                   int i, Segment s) {
3033          long segmentOffset = ((long) i << segmentsShift) + segmentsBase;
3034          UNSAFE.putOrderedObject(segs, segmentOffset, s);
3035      }
3036  
3037 <
3037 >    /**
3038 >     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
3039 >     * Replace with a simple call to Unsafe.getUnsafe when integrating
3040 >     * into a jdk.
3041 >     *
3042 >     * @return a sun.misc.Unsafe
3043 >     */
3044 >    private static sun.misc.Unsafe getUnsafe() {
3045 >        try {
3046 >            return sun.misc.Unsafe.getUnsafe();
3047 >        } catch (SecurityException tryReflectionInstead) {}
3048 >        try {
3049 >            return java.security.AccessController.doPrivileged
3050 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
3051 >                public sun.misc.Unsafe run() throws Exception {
3052 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
3053 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
3054 >                        f.setAccessible(true);
3055 >                        Object x = f.get(null);
3056 >                        if (k.isInstance(x))
3057 >                            return k.cast(x);
3058 >                    }
3059 >                    throw new NoSuchFieldError("the Unsafe");
3060 >                }});
3061 >        } catch (java.security.PrivilegedActionException e) {
3062 >            throw new RuntimeException("Could not initialize intrinsics",
3063 >                                       e.getCause());
3064 >        }
3065 >    }
3066   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines