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

Comparing jsr166/src/main/java/util/Collections.java (file contents):
Revision 1.5 by jsr166, Mon May 2 08:35:49 2005 UTC vs.
Revision 1.22 by jsr166, Fri Mar 3 17:09:56 2006 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
# Line 63 | Line 63 | public class Collections {
63       * two implementations, one of which is appropriate for RandomAccess
64       * lists, the other for "sequential."  Often, the random access variant
65       * yields better performance on small sequential access lists.  The
66 <     * tuning  parameters below determine the cutoff point for what constitutes
66 >     * tuning parameters below determine the cutoff point for what constitutes
67       * a "small" sequential access list for each algorithm.  The values below
68       * were empirically determined to work well for LinkedList. Hopefully
69       * they should be reasonable for other sequential access List
# Line 408 | Line 408 | public class Collections {
408       *         its list-iterator does not support the <tt>set</tt> operation.
409       */
410      public static void shuffle(List<?> list) {
411 +        if (r == null) {
412 +            r = new Random();
413 +        }
414          shuffle(list, r);
415      }
416 <    private static Random r = new Random();
416 >    private static Random r;
417  
418      /**
419       * Randomly permute the specified list using the specified source of
# Line 569 | Line 572 | public class Collections {
572          Iterator<? extends T> i = coll.iterator();
573          T candidate = i.next();
574  
575 <        while(i.hasNext()) {
575 >        while (i.hasNext()) {
576              T next = i.next();
577              if (next.compareTo(candidate) < 0)
578                  candidate = next;
# Line 606 | Line 609 | public class Collections {
609          Iterator<? extends T> i = coll.iterator();
610          T candidate = i.next();
611  
612 <        while(i.hasNext()) {
612 >        while (i.hasNext()) {
613              T next = i.next();
614              if (comp.compare(next, candidate) < 0)
615                  candidate = next;
# Line 639 | Line 642 | public class Collections {
642          Iterator<? extends T> i = coll.iterator();
643          T candidate = i.next();
644  
645 <        while(i.hasNext()) {
645 >        while (i.hasNext()) {
646              T next = i.next();
647              if (next.compareTo(candidate) > 0)
648                  candidate = next;
# Line 676 | Line 679 | public class Collections {
679          Iterator<? extends T> i = coll.iterator();
680          T candidate = i.next();
681  
682 <        while(i.hasNext()) {
682 >        while (i.hasNext()) {
683              T next = i.next();
684              if (comp.compare(next, candidate) > 0)
685                  candidate = next;
# Line 1051 | Line 1054 | public class Collections {
1054       * @param  s the set for which an unmodifiable view is to be returned.
1055       * @return an unmodifiable view of the specified set.
1056       */
1054
1057      public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1058          return new UnmodifiableSet<T>(s);
1059      }
# Line 1288 | Line 1290 | public class Collections {
1290          public V remove(Object key) {
1291              throw new UnsupportedOperationException();
1292          }
1293 <        public void putAll(Map<? extends K, ? extends V> t) {
1293 >        public void putAll(Map<? extends K, ? extends V> m) {
1294              throw new UnsupportedOperationException();
1295          }
1296          public void clear() {
# Line 1363 | Line 1365 | public class Collections {
1365                  // We don't pass a to c.toArray, to avoid window of
1366                  // vulnerability wherein an unscrupulous multithreaded client
1367                  // could get his hands on raw (unwrapped) Entries from c.
1368 <                Object[] arr =
1367 <                    c.toArray(
1368 <                        a.length==0 ? a :
1369 <                        (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), 0));
1368 >                Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
1369  
1370                  for (int i=0; i<arr.length; i++)
1371                      arr[i] = new UnmodifiableEntry<K,V>((Map.Entry<K,V>)arr[i]);
# Line 2221 | Line 2220 | public class Collections {
2220          public Object[] toArray()           { return c.toArray(); }
2221          public <T> T[] toArray(T[] a)       { return c.toArray(a); }
2222          public String toString()            { return c.toString(); }
2224        public Iterator<E> iterator()       { return c.iterator(); }
2223          public boolean remove(Object o)     { return c.remove(o); }
2224          public boolean containsAll(Collection<?> coll) {
2225              return c.containsAll(coll);
# Line 2236 | Line 2234 | public class Collections {
2234              c.clear();
2235          }
2236  
2237 <        public boolean add(E e){
2237 >        public Iterator<E> iterator() {
2238 >            return new Iterator<E>() {
2239 >                private final Iterator<E> it = c.iterator();
2240 >                public boolean hasNext() { return it.hasNext(); }
2241 >                public E next()          { return it.next(); }
2242 >                public void remove()     {        it.remove(); }};
2243 >        }
2244 >
2245 >        public boolean add(E e){
2246              typeCheck(e);
2247              return c.add(e);
2248          }
# Line 2246 | Line 2252 | public class Collections {
2252               * Dump coll into an array of the required type.  This serves
2253               * three purposes: it insulates us from concurrent changes in
2254               * the contents of coll, it type-checks all of the elements in
2255 <             * coll, and it provides all-or-nothing semantics(which we
2255 >             * coll, and it provides all-or-nothing semantics (which we
2256               * wouldn't get if we type-checked each element as we added it).
2257               */
2258              E[] a = null;
2259              try {
2260                  a = coll.toArray(zeroLengthElementArray());
2261 <            } catch(ArrayStoreException e) {
2261 >            } catch (ArrayStoreException e) {
2262                  throw new ClassCastException();
2263              }
2264  
# Line 2436 | Line 2442 | public class Collections {
2442              E[] a = null;
2443              try {
2444                  a = c.toArray(zeroLengthElementArray());
2445 <            } catch(ArrayStoreException e) {
2445 >            } catch (ArrayStoreException e) {
2446                  throw new ClassCastException();
2447              }
2448  
# Line 2582 | Line 2588 | public class Collections {
2588              K[] keys = null;
2589              try {
2590                  keys = t.keySet().toArray(zeroLengthKeyArray());
2591 <            } catch(ArrayStoreException e) {
2591 >            } catch (ArrayStoreException e) {
2592                  throw new ClassCastException();
2593              }
2594              V[] values = null;
2595              try {
2596                  values = t.values().toArray(zeroLengthValueArray());
2597 <            } catch(ArrayStoreException e) {
2597 >            } catch (ArrayStoreException e) {
2598                  throw new ClassCastException();
2599              }
2600  
# Line 2700 | Line 2706 | public class Collections {
2706                  // We don't pass a to s.toArray, to avoid window of
2707                  // vulnerability wherein an unscrupulous multithreaded client
2708                  // could get his hands on raw (unwrapped) Entries from s.
2709 <                Object[] arr = s.toArray(a.length==0 ? a :
2704 <                   (T[])Array.newInstance(a.getClass().getComponentType(), 0));
2709 >                Object[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
2710  
2711                  for (int i=0; i<arr.length; i++)
2712                      arr[i] = new CheckedEntry<K,V>((Map.Entry<K,V>)arr[i],
# Line 3197 | Line 3202 | public class Collections {
3202       * @see    List#addAll(int, Collection)
3203       */
3204      public static <T> List<T> nCopies(int n, T o) {
3205 +        if (n < 0)
3206 +            throw new IllegalArgumentException("List length = " + n);
3207          return new CopiesList<T>(n, o);
3208      }
3209  
# Line 3209 | Line 3216 | public class Collections {
3216      {
3217          static final long serialVersionUID = 2739099268398711800L;
3218  
3219 <        int n;
3220 <        E element;
3219 >        final int n;
3220 >        final E element;
3221  
3222          CopiesList(int n, E e) {
3223 <            if (n < 0)
3217 <                throw new IllegalArgumentException("List length = " + n);
3223 >            assert n >= 0;
3224              this.n = n;
3225              element = e;
3226          }
# Line 3227 | Line 3233 | public class Collections {
3233              return n != 0 && eq(obj, element);
3234          }
3235  
3236 +        public int indexOf(Object o) {
3237 +            return contains(o) ? 0 : -1;
3238 +        }
3239 +
3240 +        public int lastIndexOf(Object o) {
3241 +            return contains(o) ? n - 1 : -1;
3242 +        }
3243 +
3244          public E get(int index) {
3245 <            if (index<0 || index>=n)
3245 >            if (index < 0 || index >= n)
3246                  throw new IndexOutOfBoundsException("Index: "+index+
3247                                                      ", Size: "+n);
3248              return element;
3249          }
3250 +
3251 +        public Object[] toArray() {
3252 +            final Object[] a = new Object[n];
3253 +            if (element != null)
3254 +                Arrays.fill(a, 0, n, element);
3255 +            return a;
3256 +        }
3257 +
3258 +        public <T> T[] toArray(T[] a) {
3259 +            final int n = this.n;
3260 +            if (a.length < n) {
3261 +                a = (T[])java.lang.reflect.Array
3262 +                    .newInstance(a.getClass().getComponentType(), n);
3263 +                if (element != null)
3264 +                    Arrays.fill(a, 0, n, element);
3265 +            } else {
3266 +                Arrays.fill(a, 0, n, element);
3267 +                if (a.length > n)
3268 +                    a[n] = null;
3269 +            }
3270 +            return a;
3271 +        }
3272 +
3273 +        public List<E> subList(int fromIndex, int toIndex) {
3274 +            if (fromIndex < 0)
3275 +                throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
3276 +            if (toIndex > n)
3277 +                throw new IndexOutOfBoundsException("toIndex = " + toIndex);
3278 +            if (fromIndex > toIndex)
3279 +                throw new IllegalArgumentException("fromIndex(" + fromIndex +
3280 +                                                   ") > toIndex(" + toIndex + ")");
3281 +            return new CopiesList(toIndex - fromIndex, element);
3282 +        }
3283      }
3284  
3285      /**
# Line 3272 | Line 3319 | public class Collections {
3319          public int compare(Comparable<Object> c1, Comparable<Object> c2) {
3320              return c2.compareTo(c1);
3321          }
3322 +
3323 +        private Object readResolve() { return reverseOrder(); }
3324      }
3325  
3326      /**
# Line 3290 | Line 3339 | public class Collections {
3339       */
3340      public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
3341          if (cmp == null)
3342 <            return new ReverseComparator();  // Unchecked warning!!
3342 >            return reverseOrder();
3343  
3344          return new ReverseComparator2<T>(cmp);
3345      }
# Line 3457 | Line 3506 | public class Collections {
3506       * </pre>
3507       *
3508       * @param c the collection into which <tt>elements</tt> are to be inserted
3509 <     * @param a the elements to insert into <tt>c</tt>
3509 >     * @param elements the elements to insert into <tt>c</tt>
3510       * @return <tt>true</tt> if the collection changed as a result of the call
3511       * @throws UnsupportedOperationException if <tt>c</tt> does not support
3512 <     *         the <tt>add</tt> operation.
3512 >     *         the <tt>add</tt> operation
3513       * @throws NullPointerException if <tt>elements</tt> contains one or more
3514 <     *         null values and <tt>c</tt> does not support null elements, or
3514 >     *         null values and <tt>c</tt> does not permit null elements, or
3515       *         if <tt>c</tt> or <tt>elements</tt> are <tt>null</tt>
3516 <     * @throws IllegalArgumentException if some aspect of a value in
3516 >     * @throws IllegalArgumentException if some property of a value in
3517       *         <tt>elements</tt> prevents it from being added to <tt>c</tt>
3518       * @see Collection#addAll(Collection)
3519       * @since 1.5
3520       */
3521 <    public static <T> boolean addAll(Collection<? super T> c, T... a) {
3521 >    public static <T> boolean addAll(Collection<? super T> c, T... elements) {
3522          boolean result = false;
3523 <        for (T e : a)
3524 <            result |= c.add(e);
3523 >        for (T element : elements)
3524 >            result |= c.add(element);
3525          return result;
3526      }
3527  
# Line 3496 | Line 3545 | public class Collections {
3545       * to this method, and no reference to the map is retained, as illustrated
3546       * in the following code fragment:
3547       * <pre>
3548 <     *    Set&lt;Object&gt; weakHashSet = Collections.asSet(
3548 >     *    Set&lt;Object&gt; weakHashSet = Collections.newSetFromMap(
3549       *        new WeakHashMap&lt;Object, Boolean&gt;());
3550       * </pre>
3551       *
3552       * @param map the backing map
3553       * @return the set backed by the map
3554       * @throws IllegalArgumentException if <tt>map</tt> is not empty
3555 +     * @since 1.6
3556       */
3557 <    public static <E> Set<E> asSet(Map<E, Boolean> map) {
3558 <        return new MapAsSet<E>(map);
3557 >    public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
3558 >        return new SetFromMap<E>(map);
3559      }
3560  
3561 <    private static class MapAsSet<E> extends AbstractSet<E>
3561 >    private static class SetFromMap<E> extends AbstractSet<E>
3562          implements Set<E>, Serializable
3563      {
3564          private final Map<E, Boolean> m;  // The backing map
3565          private transient Set<E> keySet;  // Its keySet
3566  
3567 <        MapAsSet(Map<E, Boolean> map) {
3567 >        SetFromMap(Map<E, Boolean> map) {
3568              if (!map.isEmpty())
3569                  throw new IllegalArgumentException("Map is non-empty");
3570              m = map;
# Line 3558 | Line 3608 | public class Collections {
3608       * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
3609       * view can be useful when you would like to use a method
3610       * requiring a <tt>Queue</tt> but you need Lifo ordering.
3611 <     * @param deque the Deque
3611 >     *
3612 >     * @param deque the deque
3613       * @return the queue
3614       * @since  1.6
3615       */
# Line 3568 | Line 3619 | public class Collections {
3619  
3620      static class AsLIFOQueue<E> extends AbstractQueue<E>
3621          implements Queue<E>, Serializable {
3622 +        private static final long serialVersionUID = 1802017725587941708L;
3623          private final Deque<E> q;
3624          AsLIFOQueue(Deque<E> q)            { this.q = q; }
3625 +        public boolean add(E e)            { q.addFirst(e); return true; }
3626          public boolean offer(E e)          { return q.offerFirst(e); }
3627          public E poll()                    { return q.pollFirst(); }
3628          public E remove()                  { return q.removeFirst(); }
# Line 3581 | Line 3634 | public class Collections {
3634          public Iterator<E> iterator()      { return q.iterator(); }
3635          public Object[] toArray()          { return q.toArray(); }
3636          public <T> T[] toArray(T[] a)      { return q.toArray(a); }
3584        public boolean add(E e)            { return q.offerFirst(e); }
3637          public boolean remove(Object o)    { return q.remove(o); }
3638          public void clear()                { q.clear(); }
3639      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines