ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java (file contents):
Revision 1.141 by jsr166, Tue Oct 30 16:54:26 2012 UTC vs.
Revision 1.142 by dl, Wed Oct 31 12:49:18 2012 UTC

# Line 113 | Line 113 | import java.io.Serializable;
113   * when computing a snapshot summary of the values in a shared
114   * registry.  There are three kinds of operation, each with four
115   * forms, accepting functions with Keys, Values, Entries, and (Key,
116 < * Value) arguments and/or return values. Because the elements of a
116 > * Value) arguments and/or return values. (The first three forms are
117 > * also available via the {@link #keySet()}, {@link #values()} and
118 > * {@link #entrySet()} views). Because the elements of a
119   * ConcurrentHashMap are not ordered in any particular way, and may be
120   * processed in different orders in different parallel executions, the
121   * correctness of supplied functions should not depend on any
# Line 288 | Line 290 | public class ConcurrentHashMap<K, V>
290          Spliterator<T> split();
291      }
292  
291    /**
292     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
293     * which additions may optionally be enabled by mapping to a
294     * common value.  This class cannot be directly instantiated. See
295     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
296     * {@link #newKeySet(int)}.
297     *
298     * <p>The view's {@code iterator} is a "weakly consistent" iterator
299     * that will never throw {@link ConcurrentModificationException},
300     * and guarantees to traverse elements as they existed upon
301     * construction of the iterator, and may (but is not guaranteed to)
302     * reflect any modifications subsequent to construction.
303     */
304    public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable {
305        private static final long serialVersionUID = 7249069246763182397L;
306        private final V value;
307        KeySetView(ConcurrentHashMap<K, V> map, V value) {  // non-public
308            super(map);
309            this.value = value;
310        }
311
312        /**
313         * Returns the map backing this view.
314         *
315         * @return the map backing this view
316         */
317        public ConcurrentHashMap<K,V> getMap() { return map; }
318
319        /**
320         * Returns the default mapped value for additions,
321         * or {@code null} if additions are not supported.
322         *
323         * @return the default mapped value for additions, or {@code null}
324         * if not supported.
325         */
326        public V getMappedValue() { return value; }
327
328        // implement Set API
329
330        public boolean contains(Object o) { return map.containsKey(o); }
331        public boolean remove(Object o)   { return map.remove(o) != null; }
332        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
333        public boolean add(K e) {
334            V v;
335            if ((v = value) == null)
336                throw new UnsupportedOperationException();
337            if (e == null)
338                throw new NullPointerException();
339            return map.internalPutIfAbsent(e, v) == null;
340        }
341        public boolean addAll(Collection<? extends K> c) {
342            boolean added = false;
343            V v;
344            if ((v = value) == null)
345                throw new UnsupportedOperationException();
346            for (K e : c) {
347                if (e == null)
348                    throw new NullPointerException();
349                if (map.internalPutIfAbsent(e, v) == null)
350                    added = true;
351            }
352            return added;
353        }
354        public boolean equals(Object o) {
355            Set<?> c;
356            return ((o instanceof Set) &&
357                    ((c = (Set<?>)o) == this ||
358                     (containsAll(c) && c.containsAll(this))));
359        }
360    }
293  
294      /*
295       * Overview:
# Line 642 | Line 574 | public class ConcurrentHashMap<K, V>
574  
575      // views
576      private transient KeySetView<K,V> keySet;
577 <    private transient Values<K,V> values;
578 <    private transient EntrySet<K,V> entrySet;
577 >    private transient ValuesView<K,V> values;
578 >    private transient EntrySetView<K,V> entrySet;
579  
580      /** For serialization compatibility. Null unless serialized; see below */
581      private Segment<K,V>[] segments;
# Line 742 | Line 674 | public class ConcurrentHashMap<K, V>
674                                  try {
675                                      wait();
676                                  } catch (InterruptedException ie) {
677 <                                    Thread.currentThread().interrupt();
677 >                                    try {
678 >                                        Thread.currentThread().interrupt();
679 >                                    } catch (SecurityException ignore) {
680 >                                    }
681                                  }
682                              }
683                              else
# Line 3081 | Line 3016 | public class ConcurrentHashMap<K, V>
3016      /**
3017       * Returns a {@link Collection} view of the values contained in this map.
3018       * The collection is backed by the map, so changes to the map are
3019 <     * reflected in the collection, and vice-versa.  The collection
3085 <     * supports element removal, which removes the corresponding
3086 <     * mapping from this map, via the {@code Iterator.remove},
3087 <     * {@code Collection.remove}, {@code removeAll},
3088 <     * {@code retainAll}, and {@code clear} operations.  It does not
3089 <     * support the {@code add} or {@code addAll} operations.
3090 <     *
3091 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
3092 <     * that will never throw {@link ConcurrentModificationException},
3093 <     * and guarantees to traverse elements as they existed upon
3094 <     * construction of the iterator, and may (but is not guaranteed to)
3095 <     * reflect any modifications subsequent to construction.
3019 >     * reflected in the collection, and vice-versa.
3020       */
3021 <    public Collection<V> values() {
3022 <        Values<K,V> vs = values;
3023 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
3021 >    public ValuesView<K,V> values() {
3022 >        ValuesView<K,V> vs = values;
3023 >        return (vs != null) ? vs : (values = new ValuesView<K,V>(this));
3024      }
3025  
3026      /**
# Line 3116 | Line 3040 | public class ConcurrentHashMap<K, V>
3040       * reflect any modifications subsequent to construction.
3041       */
3042      public Set<Map.Entry<K,V>> entrySet() {
3043 <        EntrySet<K,V> es = entrySet;
3044 <        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
3043 >        EntrySetView<K,V> es = entrySet;
3044 >        return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this));
3045      }
3046  
3047      /**
# Line 3360 | Line 3284 | public class ConcurrentHashMap<K, V>
3284          }
3285      }
3286  
3363    /* ----------------Views -------------- */
3364
3365    /**
3366     * Base class for views.
3367     */
3368    static abstract class CHMView<K, V> {
3369        final ConcurrentHashMap<K, V> map;
3370        CHMView(ConcurrentHashMap<K, V> map)  { this.map = map; }
3371        public final int size()                 { return map.size(); }
3372        public final boolean isEmpty()          { return map.isEmpty(); }
3373        public final void clear()               { map.clear(); }
3374
3375        // implementations below rely on concrete classes supplying these
3376        abstract public Iterator<?> iterator();
3377        abstract public boolean contains(Object o);
3378        abstract public boolean remove(Object o);
3379
3380        private static final String oomeMsg = "Required array size too large";
3381
3382        public final Object[] toArray() {
3383            long sz = map.mappingCount();
3384            if (sz > (long)(MAX_ARRAY_SIZE))
3385                throw new OutOfMemoryError(oomeMsg);
3386            int n = (int)sz;
3387            Object[] r = new Object[n];
3388            int i = 0;
3389            Iterator<?> it = iterator();
3390            while (it.hasNext()) {
3391                if (i == n) {
3392                    if (n >= MAX_ARRAY_SIZE)
3393                        throw new OutOfMemoryError(oomeMsg);
3394                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3395                        n = MAX_ARRAY_SIZE;
3396                    else
3397                        n += (n >>> 1) + 1;
3398                    r = Arrays.copyOf(r, n);
3399                }
3400                r[i++] = it.next();
3401            }
3402            return (i == n) ? r : Arrays.copyOf(r, i);
3403        }
3404
3405        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
3406            long sz = map.mappingCount();
3407            if (sz > (long)(MAX_ARRAY_SIZE))
3408                throw new OutOfMemoryError(oomeMsg);
3409            int m = (int)sz;
3410            T[] r = (a.length >= m) ? a :
3411                (T[])java.lang.reflect.Array
3412                .newInstance(a.getClass().getComponentType(), m);
3413            int n = r.length;
3414            int i = 0;
3415            Iterator<?> it = iterator();
3416            while (it.hasNext()) {
3417                if (i == n) {
3418                    if (n >= MAX_ARRAY_SIZE)
3419                        throw new OutOfMemoryError(oomeMsg);
3420                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3421                        n = MAX_ARRAY_SIZE;
3422                    else
3423                        n += (n >>> 1) + 1;
3424                    r = Arrays.copyOf(r, n);
3425                }
3426                r[i++] = (T)it.next();
3427            }
3428            if (a == r && i < n) {
3429                r[i] = null; // null-terminate
3430                return r;
3431            }
3432            return (i == n) ? r : Arrays.copyOf(r, i);
3433        }
3434
3435        public final int hashCode() {
3436            int h = 0;
3437            for (Iterator<?> it = iterator(); it.hasNext();)
3438                h += it.next().hashCode();
3439            return h;
3440        }
3441
3442        public final String toString() {
3443            StringBuilder sb = new StringBuilder();
3444            sb.append('[');
3445            Iterator<?> it = iterator();
3446            if (it.hasNext()) {
3447                for (;;) {
3448                    Object e = it.next();
3449                    sb.append(e == this ? "(this Collection)" : e);
3450                    if (!it.hasNext())
3451                        break;
3452                    sb.append(',').append(' ');
3453                }
3454            }
3455            return sb.append(']').toString();
3456        }
3457
3458        public final boolean containsAll(Collection<?> c) {
3459            if (c != this) {
3460                for (Iterator<?> it = c.iterator(); it.hasNext();) {
3461                    Object e = it.next();
3462                    if (e == null || !contains(e))
3463                        return false;
3464                }
3465            }
3466            return true;
3467        }
3468
3469        public final boolean removeAll(Collection<?> c) {
3470            boolean modified = false;
3471            for (Iterator<?> it = iterator(); it.hasNext();) {
3472                if (c.contains(it.next())) {
3473                    it.remove();
3474                    modified = true;
3475                }
3476            }
3477            return modified;
3478        }
3479
3480        public final boolean retainAll(Collection<?> c) {
3481            boolean modified = false;
3482            for (Iterator<?> it = iterator(); it.hasNext();) {
3483                if (!c.contains(it.next())) {
3484                    it.remove();
3485                    modified = true;
3486                }
3487            }
3488            return modified;
3489        }
3490
3491    }
3492
3493    static final class Values<K,V> extends CHMView<K,V>
3494        implements Collection<V> {
3495        Values(ConcurrentHashMap<K, V> map)   { super(map); }
3496        public final boolean contains(Object o) { return map.containsValue(o); }
3497        public final boolean remove(Object o) {
3498            if (o != null) {
3499                Iterator<V> it = new ValueIterator<K,V>(map);
3500                while (it.hasNext()) {
3501                    if (o.equals(it.next())) {
3502                        it.remove();
3503                        return true;
3504                    }
3505                }
3506            }
3507            return false;
3508        }
3509        public final Iterator<V> iterator() {
3510            return new ValueIterator<K,V>(map);
3511        }
3512        public final boolean add(V e) {
3513            throw new UnsupportedOperationException();
3514        }
3515        public final boolean addAll(Collection<? extends V> c) {
3516            throw new UnsupportedOperationException();
3517        }
3518
3519    }
3520
3521    static final class EntrySet<K,V> extends CHMView<K,V>
3522        implements Set<Map.Entry<K,V>> {
3523        EntrySet(ConcurrentHashMap<K, V> map) { super(map); }
3524        public final boolean contains(Object o) {
3525            Object k, v, r; Map.Entry<?,?> e;
3526            return ((o instanceof Map.Entry) &&
3527                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3528                    (r = map.get(k)) != null &&
3529                    (v = e.getValue()) != null &&
3530                    (v == r || v.equals(r)));
3531        }
3532        public final boolean remove(Object o) {
3533            Object k, v; Map.Entry<?,?> e;
3534            return ((o instanceof Map.Entry) &&
3535                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3536                    (v = e.getValue()) != null &&
3537                    map.remove(k, v));
3538        }
3539        public final Iterator<Map.Entry<K,V>> iterator() {
3540            return new EntryIterator<K,V>(map);
3541        }
3542        public final boolean add(Entry<K,V> e) {
3543            throw new UnsupportedOperationException();
3544        }
3545        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3546            throw new UnsupportedOperationException();
3547        }
3548        public boolean equals(Object o) {
3549            Set<?> c;
3550            return ((o instanceof Set) &&
3551                    ((c = (Set<?>)o) == this ||
3552                     (containsAll(c) && c.containsAll(this))));
3553        }
3554    }
3555
3287      /* ---------------- Serialization Support -------------- */
3288  
3289      /**
# Line 4223 | Line 3954 | public class ConcurrentHashMap<K, V>
3954              (this, transformer, basis, reducer).invoke();
3955      }
3956  
3957 +    /* ----------------Views -------------- */
3958 +
3959 +    /**
3960 +     * Base class for views.
3961 +     */
3962 +    static abstract class CHMView<K, V> {
3963 +        final ConcurrentHashMap<K, V> map;
3964 +        CHMView(ConcurrentHashMap<K, V> map)  { this.map = map; }
3965 +
3966 +        /**
3967 +         * Returns the map backing this view.
3968 +         *
3969 +         * @return the map backing this view
3970 +         */
3971 +        public ConcurrentHashMap<K,V> getMap() { return map; }
3972 +
3973 +        public final int size()                 { return map.size(); }
3974 +        public final boolean isEmpty()          { return map.isEmpty(); }
3975 +        public final void clear()               { map.clear(); }
3976 +
3977 +        // implementations below rely on concrete classes supplying these
3978 +        abstract public Iterator<?> iterator();
3979 +        abstract public boolean contains(Object o);
3980 +        abstract public boolean remove(Object o);
3981 +
3982 +        private static final String oomeMsg = "Required array size too large";
3983 +
3984 +        public final Object[] toArray() {
3985 +            long sz = map.mappingCount();
3986 +            if (sz > (long)(MAX_ARRAY_SIZE))
3987 +                throw new OutOfMemoryError(oomeMsg);
3988 +            int n = (int)sz;
3989 +            Object[] r = new Object[n];
3990 +            int i = 0;
3991 +            Iterator<?> it = iterator();
3992 +            while (it.hasNext()) {
3993 +                if (i == n) {
3994 +                    if (n >= MAX_ARRAY_SIZE)
3995 +                        throw new OutOfMemoryError(oomeMsg);
3996 +                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3997 +                        n = MAX_ARRAY_SIZE;
3998 +                    else
3999 +                        n += (n >>> 1) + 1;
4000 +                    r = Arrays.copyOf(r, n);
4001 +                }
4002 +                r[i++] = it.next();
4003 +            }
4004 +            return (i == n) ? r : Arrays.copyOf(r, i);
4005 +        }
4006 +
4007 +        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
4008 +            long sz = map.mappingCount();
4009 +            if (sz > (long)(MAX_ARRAY_SIZE))
4010 +                throw new OutOfMemoryError(oomeMsg);
4011 +            int m = (int)sz;
4012 +            T[] r = (a.length >= m) ? a :
4013 +                (T[])java.lang.reflect.Array
4014 +                .newInstance(a.getClass().getComponentType(), m);
4015 +            int n = r.length;
4016 +            int i = 0;
4017 +            Iterator<?> it = iterator();
4018 +            while (it.hasNext()) {
4019 +                if (i == n) {
4020 +                    if (n >= MAX_ARRAY_SIZE)
4021 +                        throw new OutOfMemoryError(oomeMsg);
4022 +                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4023 +                        n = MAX_ARRAY_SIZE;
4024 +                    else
4025 +                        n += (n >>> 1) + 1;
4026 +                    r = Arrays.copyOf(r, n);
4027 +                }
4028 +                r[i++] = (T)it.next();
4029 +            }
4030 +            if (a == r && i < n) {
4031 +                r[i] = null; // null-terminate
4032 +                return r;
4033 +            }
4034 +            return (i == n) ? r : Arrays.copyOf(r, i);
4035 +        }
4036 +
4037 +        public final int hashCode() {
4038 +            int h = 0;
4039 +            for (Iterator<?> it = iterator(); it.hasNext();)
4040 +                h += it.next().hashCode();
4041 +            return h;
4042 +        }
4043 +
4044 +        public final String toString() {
4045 +            StringBuilder sb = new StringBuilder();
4046 +            sb.append('[');
4047 +            Iterator<?> it = iterator();
4048 +            if (it.hasNext()) {
4049 +                for (;;) {
4050 +                    Object e = it.next();
4051 +                    sb.append(e == this ? "(this Collection)" : e);
4052 +                    if (!it.hasNext())
4053 +                        break;
4054 +                    sb.append(',').append(' ');
4055 +                }
4056 +            }
4057 +            return sb.append(']').toString();
4058 +        }
4059 +
4060 +        public final boolean containsAll(Collection<?> c) {
4061 +            if (c != this) {
4062 +                for (Iterator<?> it = c.iterator(); it.hasNext();) {
4063 +                    Object e = it.next();
4064 +                    if (e == null || !contains(e))
4065 +                        return false;
4066 +                }
4067 +            }
4068 +            return true;
4069 +        }
4070 +
4071 +        public final boolean removeAll(Collection<?> c) {
4072 +            boolean modified = false;
4073 +            for (Iterator<?> it = iterator(); it.hasNext();) {
4074 +                if (c.contains(it.next())) {
4075 +                    it.remove();
4076 +                    modified = true;
4077 +                }
4078 +            }
4079 +            return modified;
4080 +        }
4081 +
4082 +        public final boolean retainAll(Collection<?> c) {
4083 +            boolean modified = false;
4084 +            for (Iterator<?> it = iterator(); it.hasNext();) {
4085 +                if (!c.contains(it.next())) {
4086 +                    it.remove();
4087 +                    modified = true;
4088 +                }
4089 +            }
4090 +            return modified;
4091 +        }
4092 +
4093 +    }
4094 +
4095 +    /**
4096 +     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
4097 +     * which additions may optionally be enabled by mapping to a
4098 +     * common value.  This class cannot be directly instantiated. See
4099 +     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
4100 +     * {@link #newKeySet(int)}.
4101 +     */
4102 +    public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable {
4103 +        private static final long serialVersionUID = 7249069246763182397L;
4104 +        private final V value;
4105 +        KeySetView(ConcurrentHashMap<K, V> map, V value) {  // non-public
4106 +            super(map);
4107 +            this.value = value;
4108 +        }
4109 +
4110 +        /**
4111 +         * Returns the default mapped value for additions,
4112 +         * or {@code null} if additions are not supported.
4113 +         *
4114 +         * @return the default mapped value for additions, or {@code null}
4115 +         * if not supported.
4116 +         */
4117 +        public V getMappedValue() { return value; }
4118 +
4119 +        // implement Set API
4120 +
4121 +        public boolean contains(Object o) { return map.containsKey(o); }
4122 +        public boolean remove(Object o)   { return map.remove(o) != null; }
4123 +
4124 +        /**
4125 +         * Returns a "weakly consistent" iterator that will never
4126 +         * throw {@link ConcurrentModificationException}, and
4127 +         * guarantees to traverse elements as they existed upon
4128 +         * construction of the iterator, and may (but is not
4129 +         * guaranteed to) reflect any modifications subsequent to
4130 +         * construction.
4131 +         *
4132 +         * @return an iterator over the keys of this map
4133 +         */
4134 +        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
4135 +        public boolean add(K e) {
4136 +            V v;
4137 +            if ((v = value) == null)
4138 +                throw new UnsupportedOperationException();
4139 +            if (e == null)
4140 +                throw new NullPointerException();
4141 +            return map.internalPutIfAbsent(e, v) == null;
4142 +        }
4143 +        public boolean addAll(Collection<? extends K> c) {
4144 +            boolean added = false;
4145 +            V v;
4146 +            if ((v = value) == null)
4147 +                throw new UnsupportedOperationException();
4148 +            for (K e : c) {
4149 +                if (e == null)
4150 +                    throw new NullPointerException();
4151 +                if (map.internalPutIfAbsent(e, v) == null)
4152 +                    added = true;
4153 +            }
4154 +            return added;
4155 +        }
4156 +        public boolean equals(Object o) {
4157 +            Set<?> c;
4158 +            return ((o instanceof Set) &&
4159 +                    ((c = (Set<?>)o) == this ||
4160 +                     (containsAll(c) && c.containsAll(this))));
4161 +        }
4162 +
4163 +        /**
4164 +         * Performs the given action for each key.
4165 +         *
4166 +         * @param action the action
4167 +         */
4168 +        public void forEach(Action<K> action) {
4169 +            ForkJoinTasks.forEachKey
4170 +                (map, action).invoke();
4171 +        }
4172 +
4173 +        /**
4174 +         * Performs the given action for each non-null transformation
4175 +         * of each key.
4176 +         *
4177 +         * @param transformer a function returning the transformation
4178 +         * for an element, or null of there is no transformation (in
4179 +         * which case the action is not applied).
4180 +         * @param action the action
4181 +         */
4182 +        public <U> void forEach(Fun<? super K, ? extends U> transformer,
4183 +                                Action<U> action) {
4184 +            ForkJoinTasks.forEachKey
4185 +                (map, transformer, action).invoke();
4186 +        }
4187 +
4188 +        /**
4189 +         * Returns a non-null result from applying the given search
4190 +         * function on each key, or null if none. Upon success,
4191 +         * further element processing is suppressed and the results of
4192 +         * any other parallel invocations of the search function are
4193 +         * ignored.
4194 +         *
4195 +         * @param searchFunction a function returning a non-null
4196 +         * result on success, else null
4197 +         * @return a non-null result from applying the given search
4198 +         * function on each key, or null if none
4199 +         */
4200 +        public <U> U search(Fun<? super K, ? extends U> searchFunction) {
4201 +            return ForkJoinTasks.searchKeys
4202 +                (map, searchFunction).invoke();
4203 +        }
4204 +
4205 +        /**
4206 +         * Returns the result of accumulating all keys using the given
4207 +         * reducer to combine values, or null if none.
4208 +         *
4209 +         * @param reducer a commutative associative combining function
4210 +         * @return the result of accumulating all keys using the given
4211 +         * reducer to combine values, or null if none
4212 +         */
4213 +        public K reduce(BiFun<? super K, ? super K, ? extends K> reducer) {
4214 +            return ForkJoinTasks.reduceKeys
4215 +                (map, reducer).invoke();
4216 +        }
4217 +
4218 +        /**
4219 +         * Returns the result of accumulating the given transformation
4220 +         * of all keys using the given reducer to combine values, and
4221 +         * the given basis as an identity value.
4222 +         *
4223 +         * @param transformer a function returning the transformation
4224 +         * for an element
4225 +         * @param basis the identity (initial default value) for the reduction
4226 +         * @param reducer a commutative associative combining function
4227 +         * @return  the result of accumulating the given transformation
4228 +         * of all keys
4229 +         */
4230 +        public double reduceToDouble(ObjectToDouble<? super K> transformer,
4231 +                                     double basis,
4232 +                                     DoubleByDoubleToDouble reducer) {
4233 +            return ForkJoinTasks.reduceKeysToDouble
4234 +                (map, transformer, basis, reducer).invoke();
4235 +        }
4236 +
4237 +
4238 +        /**
4239 +         * Returns the result of accumulating the given transformation
4240 +         * of all keys using the given reducer to combine values, and
4241 +         * the given basis as an identity value.
4242 +         *
4243 +         * @param transformer a function returning the transformation
4244 +         * for an element
4245 +         * @param basis the identity (initial default value) for the reduction
4246 +         * @param reducer a commutative associative combining function
4247 +         * @return the result of accumulating the given transformation
4248 +         * of all keys
4249 +         */
4250 +        public long reduceToLong(ObjectToLong<? super K> transformer,
4251 +                                 long basis,
4252 +                                 LongByLongToLong reducer) {
4253 +            return ForkJoinTasks.reduceKeysToLong
4254 +                (map, transformer, basis, reducer).invoke();
4255 +        }
4256 +        
4257 +        /**
4258 +         * Returns the result of accumulating the given transformation
4259 +         * of all keys using the given reducer to combine values, and
4260 +         * the given basis as an identity value.
4261 +         *
4262 +         * @param transformer a function returning the transformation
4263 +         * for an element
4264 +         * @param basis the identity (initial default value) for the reduction
4265 +         * @param reducer a commutative associative combining function
4266 +         * @return the result of accumulating the given transformation
4267 +         * of all keys
4268 +         */
4269 +        public int reduceToInt(ObjectToInt<? super K> transformer,
4270 +                               int basis,
4271 +                               IntByIntToInt reducer) {
4272 +            return ForkJoinTasks.reduceKeysToInt
4273 +                (map, transformer, basis, reducer).invoke();
4274 +        }
4275 +        
4276 +    }
4277 +
4278 +    /**
4279 +     * A view of a ConcurrentHashMap as a {@link Collection} of
4280 +     * values, in which additions are disabled. This class cannot be
4281 +     * directly instantiated. See {@link #values},
4282 +     *
4283 +     * <p>The view's {@code iterator} is a "weakly consistent" iterator
4284 +     * that will never throw {@link ConcurrentModificationException},
4285 +     * and guarantees to traverse elements as they existed upon
4286 +     * construction of the iterator, and may (but is not guaranteed to)
4287 +     * reflect any modifications subsequent to construction.
4288 +     */
4289 +    public static final class ValuesView<K,V> extends CHMView<K,V>
4290 +        implements Collection<V> {
4291 +        ValuesView(ConcurrentHashMap<K, V> map)   { super(map); }
4292 +        public final boolean contains(Object o) { return map.containsValue(o); }
4293 +        public final boolean remove(Object o) {
4294 +            if (o != null) {
4295 +                Iterator<V> it = new ValueIterator<K,V>(map);
4296 +                while (it.hasNext()) {
4297 +                    if (o.equals(it.next())) {
4298 +                        it.remove();
4299 +                        return true;
4300 +                    }
4301 +                }
4302 +            }
4303 +            return false;
4304 +        }
4305 +
4306 +        /**
4307 +         * Returns a "weakly consistent" iterator that will never
4308 +         * throw {@link ConcurrentModificationException}, and
4309 +         * guarantees to traverse elements as they existed upon
4310 +         * construction of the iterator, and may (but is not
4311 +         * guaranteed to) reflect any modifications subsequent to
4312 +         * construction.
4313 +         *
4314 +         * @return an iterator over the values of this map
4315 +         */
4316 +        public final Iterator<V> iterator() {
4317 +            return new ValueIterator<K,V>(map);
4318 +        }
4319 +        public final boolean add(V e) {
4320 +            throw new UnsupportedOperationException();
4321 +        }
4322 +        public final boolean addAll(Collection<? extends V> c) {
4323 +            throw new UnsupportedOperationException();
4324 +        }
4325 +
4326 +        /**
4327 +         * Performs the given action for each value.
4328 +         *
4329 +         * @param action the action
4330 +         */
4331 +        public void forEach(Action<V> action) {
4332 +            ForkJoinTasks.forEachValue
4333 +                (map, action).invoke();
4334 +        }
4335 +
4336 +        /**
4337 +         * Performs the given action for each non-null transformation
4338 +         * of each value.
4339 +         *
4340 +         * @param transformer a function returning the transformation
4341 +         * for an element, or null of there is no transformation (in
4342 +         * which case the action is not applied).
4343 +         */
4344 +        public <U> void forEach(Fun<? super V, ? extends U> transformer,
4345 +                                     Action<U> action) {
4346 +            ForkJoinTasks.forEachValue
4347 +                (map, transformer, action).invoke();
4348 +        }
4349 +
4350 +        /**
4351 +         * Returns a non-null result from applying the given search
4352 +         * function on each value, or null if none.  Upon success,
4353 +         * further element processing is suppressed and the results of
4354 +         * any other parallel invocations of the search function are
4355 +         * ignored.
4356 +         *
4357 +         * @param searchFunction a function returning a non-null
4358 +         * result on success, else null
4359 +         * @return a non-null result from applying the given search
4360 +         * function on each value, or null if none
4361 +         *
4362 +         */
4363 +        public <U> U search(Fun<? super V, ? extends U> searchFunction) {
4364 +            return ForkJoinTasks.searchValues
4365 +                (map, searchFunction).invoke();
4366 +        }
4367 +
4368 +        /**
4369 +         * Returns the result of accumulating all values using the
4370 +         * given reducer to combine values, or null if none.
4371 +         *
4372 +         * @param reducer a commutative associative combining function
4373 +         * @return  the result of accumulating all values
4374 +         */
4375 +        public V reduce(BiFun<? super V, ? super V, ? extends V> reducer) {
4376 +            return ForkJoinTasks.reduceValues
4377 +                (map, reducer).invoke();
4378 +        }
4379 +
4380 +        /**
4381 +         * Returns the result of accumulating the given transformation
4382 +         * of all values using the given reducer to combine values, or
4383 +         * null if none.
4384 +         *
4385 +         * @param transformer a function returning the transformation
4386 +         * for an element, or null of there is no transformation (in
4387 +         * which case it is not combined).
4388 +         * @param reducer a commutative associative combining function
4389 +         * @return the result of accumulating the given transformation
4390 +         * of all values
4391 +         */
4392 +        public <U> U reduce(Fun<? super V, ? extends U> transformer,
4393 +                            BiFun<? super U, ? super U, ? extends U> reducer) {
4394 +            return ForkJoinTasks.reduceValues
4395 +                (map, transformer, reducer).invoke();
4396 +        }
4397 +        
4398 +        /**
4399 +         * Returns the result of accumulating the given transformation
4400 +         * of all values using the given reducer to combine values,
4401 +         * and the given basis as an identity value.
4402 +         *
4403 +         * @param transformer a function returning the transformation
4404 +         * for an element
4405 +         * @param basis the identity (initial default value) for the reduction
4406 +         * @param reducer a commutative associative combining function
4407 +         * @return the result of accumulating the given transformation
4408 +         * of all values
4409 +         */
4410 +        public double reduceToDouble(ObjectToDouble<? super V> transformer,
4411 +                                     double basis,
4412 +                                     DoubleByDoubleToDouble reducer) {
4413 +            return ForkJoinTasks.reduceValuesToDouble
4414 +                (map, transformer, basis, reducer).invoke();
4415 +        }
4416 +
4417 +        /**
4418 +         * Returns the result of accumulating the given transformation
4419 +         * of all values using the given reducer to combine values,
4420 +         * and the given basis as an identity value.
4421 +         *
4422 +         * @param transformer a function returning the transformation
4423 +         * for an element
4424 +         * @param basis the identity (initial default value) for the reduction
4425 +         * @param reducer a commutative associative combining function
4426 +         * @return the result of accumulating the given transformation
4427 +         * of all values
4428 +         */
4429 +        public long reduceToLong(ObjectToLong<? super V> transformer,
4430 +                                 long basis,
4431 +                                 LongByLongToLong reducer) {
4432 +            return ForkJoinTasks.reduceValuesToLong
4433 +                (map, transformer, basis, reducer).invoke();
4434 +        }
4435 +
4436 +        /**
4437 +         * Returns the result of accumulating the given transformation
4438 +         * of all values using the given reducer to combine values,
4439 +         * and the given basis as an identity value.
4440 +         *
4441 +         * @param transformer a function returning the transformation
4442 +         * for an element
4443 +         * @param basis the identity (initial default value) for the reduction
4444 +         * @param reducer a commutative associative combining function
4445 +         * @return the result of accumulating the given transformation
4446 +         * of all values
4447 +         */
4448 +        public int reduceToInt(ObjectToInt<? super V> transformer,
4449 +                               int basis,
4450 +                               IntByIntToInt reducer) {
4451 +            return ForkJoinTasks.reduceValuesToInt
4452 +                (map, transformer, basis, reducer).invoke();
4453 +        }
4454 +
4455 +    }
4456 +
4457 +    /**
4458 +     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
4459 +     * entries.  This class cannot be directly instantiated. See
4460 +     * {@link #entrySet}.
4461 +     */
4462 +    public static final class EntrySetView<K,V> extends CHMView<K,V>
4463 +        implements Set<Map.Entry<K,V>> {
4464 +        EntrySetView(ConcurrentHashMap<K, V> map) { super(map); }
4465 +        public final boolean contains(Object o) {
4466 +            Object k, v, r; Map.Entry<?,?> e;
4467 +            return ((o instanceof Map.Entry) &&
4468 +                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4469 +                    (r = map.get(k)) != null &&
4470 +                    (v = e.getValue()) != null &&
4471 +                    (v == r || v.equals(r)));
4472 +        }
4473 +        public final boolean remove(Object o) {
4474 +            Object k, v; Map.Entry<?,?> e;
4475 +            return ((o instanceof Map.Entry) &&
4476 +                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4477 +                    (v = e.getValue()) != null &&
4478 +                    map.remove(k, v));
4479 +        }
4480 +
4481 +        /**
4482 +         * Returns a "weakly consistent" iterator that will never
4483 +         * throw {@link ConcurrentModificationException}, and
4484 +         * guarantees to traverse elements as they existed upon
4485 +         * construction of the iterator, and may (but is not
4486 +         * guaranteed to) reflect any modifications subsequent to
4487 +         * construction.
4488 +         *
4489 +         * @return an iterator over the entries of this map
4490 +         */
4491 +        public final Iterator<Map.Entry<K,V>> iterator() {
4492 +            return new EntryIterator<K,V>(map);
4493 +        }
4494 +
4495 +        public final boolean add(Entry<K,V> e) {
4496 +            K key = e.getKey();
4497 +            V value = e.getValue();
4498 +            if (key == null || value == null)
4499 +                throw new NullPointerException();
4500 +            return map.internalPut(key, value) == null;
4501 +        }
4502 +        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
4503 +            boolean added = false;
4504 +            for (Entry<K,V> e : c) {
4505 +                if (add(e))
4506 +                    added = true;
4507 +            }
4508 +            return added;
4509 +        }
4510 +        public boolean equals(Object o) {
4511 +            Set<?> c;
4512 +            return ((o instanceof Set) &&
4513 +                    ((c = (Set<?>)o) == this ||
4514 +                     (containsAll(c) && c.containsAll(this))));
4515 +        }
4516 +
4517 +        /**
4518 +         * Performs the given action for each entry.
4519 +         *
4520 +         * @param action the action
4521 +         */
4522 +        public void forEach(Action<Map.Entry<K,V>> action) {
4523 +            ForkJoinTasks.forEachEntry
4524 +                (map, action).invoke();
4525 +        }
4526 +
4527 +        /**
4528 +         * Performs the given action for each non-null transformation
4529 +         * of each entry.
4530 +         *
4531 +         * @param transformer a function returning the transformation
4532 +         * for an element, or null of there is no transformation (in
4533 +         * which case the action is not applied).
4534 +         * @param action the action
4535 +         */
4536 +        public <U> void forEach(Fun<Map.Entry<K,V>, ? extends U> transformer,
4537 +                                Action<U> action) {
4538 +            ForkJoinTasks.forEachEntry
4539 +                (map, transformer, action).invoke();
4540 +        }
4541 +
4542 +        /**
4543 +         * Returns a non-null result from applying the given search
4544 +         * function on each entry, or null if none.  Upon success,
4545 +         * further element processing is suppressed and the results of
4546 +         * any other parallel invocations of the search function are
4547 +         * ignored.
4548 +         *
4549 +         * @param searchFunction a function returning a non-null
4550 +         * result on success, else null
4551 +         * @return a non-null result from applying the given search
4552 +         * function on each entry, or null if none
4553 +         */
4554 +        public <U> U search(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4555 +            return ForkJoinTasks.searchEntries
4556 +                (map, searchFunction).invoke();
4557 +        }
4558 +
4559 +        /**
4560 +         * Returns the result of accumulating all entries using the
4561 +         * given reducer to combine values, or null if none.
4562 +         *
4563 +         * @param reducer a commutative associative combining function
4564 +         * @return the result of accumulating all entries
4565 +         */
4566 +        public Map.Entry<K,V> reduce(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4567 +            return ForkJoinTasks.reduceEntries
4568 +                (map, reducer).invoke();
4569 +        }
4570 +
4571 +        /**
4572 +         * Returns the result of accumulating the given transformation
4573 +         * of all entries using the given reducer to combine values,
4574 +         * or null if none.
4575 +         *
4576 +         * @param transformer a function returning the transformation
4577 +         * for an element, or null of there is no transformation (in
4578 +         * which case it is not combined).
4579 +         * @param reducer a commutative associative combining function
4580 +         * @return the result of accumulating the given transformation
4581 +         * of all entries
4582 +         */
4583 +        public <U> U reduce(Fun<Map.Entry<K,V>, ? extends U> transformer,
4584 +                            BiFun<? super U, ? super U, ? extends U> reducer) {
4585 +            return ForkJoinTasks.reduceEntries
4586 +                (map, transformer, reducer).invoke();
4587 +        }
4588 +
4589 +        /**
4590 +         * Returns the result of accumulating the given transformation
4591 +         * of all entries using the given reducer to combine values,
4592 +         * and the given basis as an identity value.
4593 +         *
4594 +         * @param transformer a function returning the transformation
4595 +         * for an element
4596 +         * @param basis the identity (initial default value) for the reduction
4597 +         * @param reducer a commutative associative combining function
4598 +         * @return the result of accumulating the given transformation
4599 +         * of all entries
4600 +         */
4601 +        public double reduceToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4602 +                                     double basis,
4603 +                                     DoubleByDoubleToDouble reducer) {
4604 +            return ForkJoinTasks.reduceEntriesToDouble
4605 +                (map, transformer, basis, reducer).invoke();
4606 +        }
4607 +
4608 +        /**
4609 +         * Returns the result of accumulating the given transformation
4610 +         * of all entries using the given reducer to combine values,
4611 +         * and the given basis as an identity value.
4612 +         *
4613 +         * @param transformer a function returning the transformation
4614 +         * for an element
4615 +         * @param basis the identity (initial default value) for the reduction
4616 +         * @param reducer a commutative associative combining function
4617 +         * @return  the result of accumulating the given transformation
4618 +         * of all entries
4619 +         */
4620 +        public long reduceToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4621 +                                 long basis,
4622 +                                 LongByLongToLong reducer) {
4623 +            return ForkJoinTasks.reduceEntriesToLong
4624 +                (map, transformer, basis, reducer).invoke();
4625 +        }
4626 +
4627 +        /**
4628 +         * Returns the result of accumulating the given transformation
4629 +         * of all entries using the given reducer to combine values,
4630 +         * and the given basis as an identity value.
4631 +         *
4632 +         * @param transformer a function returning the transformation
4633 +         * for an element
4634 +         * @param basis the identity (initial default value) for the reduction
4635 +         * @param reducer a commutative associative combining function
4636 +         * @return the result of accumulating the given transformation
4637 +         * of all entries
4638 +         */
4639 +        public int reduceToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4640 +                               int basis,
4641 +                               IntByIntToInt reducer) {
4642 +            return ForkJoinTasks.reduceEntriesToInt
4643 +                (map, transformer, basis, reducer).invoke();
4644 +        }
4645 +
4646 +    }
4647 +
4648      // ---------------------------------------------------------------------
4649  
4650      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines