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 |
|
/** |
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; |