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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines