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.7 by jsr166, Wed May 18 03:45:35 2005 UTC vs.
Revision 1.18 by jsr166, Mon Dec 5 02:56:59 2005 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 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 1287 | 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 1362 | 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 =
1366 <                    c.toArray(
1367 <                        a.length==0 ? a :
1368 <                        (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 2699 | Line 2700 | public class Collections {
2700                  // We don't pass a to s.toArray, to avoid window of
2701                  // vulnerability wherein an unscrupulous multithreaded client
2702                  // could get his hands on raw (unwrapped) Entries from s.
2703 <                Object[] arr = s.toArray(a.length==0 ? a :
2703 <                   (T[])Array.newInstance(a.getClass().getComponentType(), 0));
2703 >                Object[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
2704  
2705                  for (int i=0; i<arr.length; i++)
2706                      arr[i] = new CheckedEntry<K,V>((Map.Entry<K,V>)arr[i],
# Line 3196 | Line 3196 | public class Collections {
3196       * @see    List#addAll(int, Collection)
3197       */
3198      public static <T> List<T> nCopies(int n, T o) {
3199 +        if (n < 0)
3200 +            throw new IllegalArgumentException("List length = " + n);
3201          return new CopiesList<T>(n, o);
3202      }
3203  
# Line 3208 | Line 3210 | public class Collections {
3210      {
3211          static final long serialVersionUID = 2739099268398711800L;
3212  
3213 <        int n;
3214 <        E element;
3213 >        final int n;
3214 >        final E element;
3215  
3216          CopiesList(int n, E e) {
3217 <            if (n < 0)
3216 <                throw new IllegalArgumentException("List length = " + n);
3217 >            assert n >= 0;
3218              this.n = n;
3219              element = e;
3220          }
# Line 3226 | Line 3227 | public class Collections {
3227              return n != 0 && eq(obj, element);
3228          }
3229  
3230 +        public int indexOf(Object o) {
3231 +            return contains(o) ? 0 : -1;
3232 +        }
3233 +
3234 +        public int lastIndexOf(Object o) {
3235 +            return contains(o) ? n - 1 : -1;
3236 +        }
3237 +
3238          public E get(int index) {
3239 <            if (index<0 || index>=n)
3239 >            if (index < 0 || index >= n)
3240                  throw new IndexOutOfBoundsException("Index: "+index+
3241                                                      ", Size: "+n);
3242              return element;
3243          }
3244 +
3245 +        public Object[] toArray() {
3246 +            final Object[] a = new Object[n];
3247 +            if (element != null)
3248 +                Arrays.fill(a, 0, n, element);
3249 +            return a;
3250 +        }
3251 +
3252 +        public <T> T[] toArray(T[] a) {
3253 +            final int n = this.n;
3254 +            if (a.length < n) {
3255 +                a = (T[])java.lang.reflect.Array
3256 +                    .newInstance(a.getClass().getComponentType(), n);
3257 +                if (element != null)
3258 +                    Arrays.fill(a, 0, n, element);
3259 +            } else {
3260 +                Arrays.fill(a, 0, n, element);
3261 +                if (a.length > n)
3262 +                    a[n] = null;
3263 +            }
3264 +            return a;
3265 +        }
3266 +
3267 +        public List<E> subList(int fromIndex, int toIndex) {
3268 +            if (fromIndex < 0)
3269 +                throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
3270 +            if (toIndex > n)
3271 +                throw new IndexOutOfBoundsException("toIndex = " + toIndex);
3272 +            if (fromIndex > toIndex)
3273 +                throw new IllegalArgumentException("fromIndex(" + fromIndex +
3274 +                                                   ") > toIndex(" + toIndex + ")");
3275 +            return new CopiesList(toIndex - fromIndex, element);
3276 +        }
3277      }
3278  
3279      /**
# Line 3495 | Line 3537 | public class Collections {
3537       * to this method, and no reference to the map is retained, as illustrated
3538       * in the following code fragment:
3539       * <pre>
3540 <     *    Set&lt;Object&gt; weakHashSet = Collections.asSet(
3540 >     *    Set&lt;Object&gt; weakHashSet = Collections.newSetFromMap(
3541       *        new WeakHashMap&lt;Object, Boolean&gt;());
3542       * </pre>
3543       *
3544       * @param map the backing map
3545       * @return the set backed by the map
3546       * @throws IllegalArgumentException if <tt>map</tt> is not empty
3547 +     * @since 1.6
3548       */
3549 <    public static <E> Set<E> asSet(Map<E, Boolean> map) {
3550 <        return new MapAsSet<E>(map);
3549 >    public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
3550 >        return new SetFromMap<E>(map);
3551      }
3552  
3553 <    private static class MapAsSet<E> extends AbstractSet<E>
3553 >    private static class SetFromMap<E> extends AbstractSet<E>
3554          implements Set<E>, Serializable
3555      {
3556          private final Map<E, Boolean> m;  // The backing map
3557          private transient Set<E> keySet;  // Its keySet
3558  
3559 <        MapAsSet(Map<E, Boolean> map) {
3559 >        SetFromMap(Map<E, Boolean> map) {
3560              if (!map.isEmpty())
3561                  throw new IllegalArgumentException("Map is non-empty");
3562              m = map;
# Line 3557 | Line 3600 | public class Collections {
3600       * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
3601       * view can be useful when you would like to use a method
3602       * requiring a <tt>Queue</tt> but you need Lifo ordering.
3603 <     * @param deque the Deque
3603 >     *
3604 >     * @param deque the deque
3605       * @return the queue
3606       * @since  1.6
3607       */
# Line 3567 | Line 3611 | public class Collections {
3611  
3612      static class AsLIFOQueue<E> extends AbstractQueue<E>
3613          implements Queue<E>, Serializable {
3614 +        private static final long serialVersionUID = 1802017725587941708L;
3615          private final Deque<E> q;
3616          AsLIFOQueue(Deque<E> q)            { this.q = q; }
3617          public boolean offer(E e)          { return q.offerFirst(e); }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines