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 |
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; |
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() { |
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 |
|
|
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 |
|
} |
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 |
|
/** |
3498 |
|
* </pre> |
3499 |
|
* |
3500 |
|
* @param c the collection into which <tt>elements</tt> are to be inserted |
3501 |
< |
* @param a the elements to insert into <tt>c</tt> |
3501 |
> |
* @param elements the elements to insert into <tt>c</tt> |
3502 |
|
* @return <tt>true</tt> if the collection changed as a result of the call |
3503 |
|
* @throws UnsupportedOperationException if <tt>c</tt> does not support |
3504 |
< |
* the <tt>add</tt> operation. |
3504 |
> |
* the <tt>add</tt> operation |
3505 |
|
* @throws NullPointerException if <tt>elements</tt> contains one or more |
3506 |
|
* null values and <tt>c</tt> does not permit null elements, or |
3507 |
|
* if <tt>c</tt> or <tt>elements</tt> are <tt>null</tt> |
3510 |
|
* @see Collection#addAll(Collection) |
3511 |
|
* @since 1.5 |
3512 |
|
*/ |
3513 |
< |
public static <T> boolean addAll(Collection<? super T> c, T... a) { |
3513 |
> |
public static <T> boolean addAll(Collection<? super T> c, T... elements) { |
3514 |
|
boolean result = false; |
3515 |
< |
for (T e : a) |
3516 |
< |
result |= c.add(e); |
3515 |
> |
for (T element : elements) |
3516 |
> |
result |= c.add(element); |
3517 |
|
return result; |
3518 |
|
} |
3519 |
|
|
3537 |
|
* to this method, and no reference to the map is retained, as illustrated |
3538 |
|
* in the following code fragment: |
3539 |
|
* <pre> |
3540 |
< |
* Set<Object> weakHashSet = Collections.asSet( |
3540 |
> |
* Set<Object> weakHashSet = Collections.newSetFromMap( |
3541 |
|
* new WeakHashMap<Object, Boolean>()); |
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; |
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 |
|
*/ |