--- jsr166/src/main/java/util/Collections.java 2005/08/11 08:52:39 1.13 +++ jsr166/src/main/java/util/Collections.java 2005/08/24 04:55:09 1.14 @@ -3196,6 +3196,8 @@ public class Collections { * @see List#addAll(int, Collection) */ public static List nCopies(int n, T o) { + if (n < 0) + throw new IllegalArgumentException("List length = " + n); return new CopiesList(n, o); } @@ -3208,12 +3210,11 @@ public class Collections { { static final long serialVersionUID = 2739099268398711800L; - int n; - E element; + final int n; + final E element; CopiesList(int n, E e) { - if (n < 0) - throw new IllegalArgumentException("List length = " + n); + assert n >= 0; this.n = n; element = e; } @@ -3226,12 +3227,53 @@ public class Collections { return n != 0 && eq(obj, element); } + public int indexOf(Object o) { + return contains(o) ? 0 : -1; + } + + public int lastIndexOf(Object o) { + return contains(o) ? n - 1 : -1; + } + public E get(int index) { - if (index<0 || index>=n) + if (index < 0 || index >= n) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+n); return element; } + + public Object[] toArray() { + final Object[] a = new Object[n]; + if (element != null) + Arrays.fill(a, 0, n, element); + return a; + } + + public T[] toArray(T[] a) { + final int n = this.n; + if (a.length < n) { + a = (T[])java.lang.reflect.Array + .newInstance(a.getClass().getComponentType(), n); + if (element != null) + Arrays.fill(a, 0, n, element); + } else { + Arrays.fill(a, 0, n, element); + if (a.length > n) + a[n] = null; + } + return a; + } + + public List subList(int fromIndex, int toIndex) { + if (fromIndex < 0) + throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); + if (toIndex > n) + throw new IndexOutOfBoundsException("toIndex = " + toIndex); + if (fromIndex > toIndex) + throw new IllegalArgumentException("fromIndex(" + fromIndex + + ") > toIndex(" + toIndex + ")"); + return new CopiesList(toIndex - fromIndex, element); + } } /** @@ -3504,17 +3546,17 @@ public class Collections { * @throws IllegalArgumentException if map is not empty * @since 1.6 */ - public static Set asSet(Map map) { - return new MapAsSet(map); + public static Set newSetFromMap(Map map) { + return new SetFromMap(map); } - private static class MapAsSet extends AbstractSet + private static class SetFromMap extends AbstractSet implements Set, Serializable { private final Map m; // The backing map private transient Set keySet; // Its keySet - MapAsSet(Map map) { + SetFromMap(Map map) { if (!map.isEmpty()) throw new IllegalArgumentException("Map is non-empty"); m = map;