13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27 import java.io.Serializable;
28 import java.io.ObjectOutputStream;
29 import java.io.IOException;
30 import java.lang.reflect.Array;
31 import java.util.function.BiConsumer;
32 import java.util.function.BiFunction;
33 import java.util.function.Function;
34
35 /**
36 * This class consists exclusively of static methods that operate on or return
37 * collections. It contains polymorphic algorithms that operate on
38 * collections, "wrappers", which return a new collection backed by a
39 * specified collection, and a few other odds and ends.
40 *
41 * <p>The methods of this class all throw a <tt>NullPointerException</tt>
42 * if the collections or class objects provided to them are null.
43 *
44 * <p>The documentation for the polymorphic algorithms contained in this class
45 * generally includes a brief description of the <i>implementation</i>. Such
46 * descriptions should be regarded as <i>implementation notes</i>, rather than
47 * parts of the <i>specification</i>. Implementors should feel free to
48 * substitute other algorithms, so long as the specification itself is adhered
49 * to. (For example, the algorithm used by <tt>sort</tt> does not have to be
50 * a mergesort, but it does have to be <i>stable</i>.)
51 *
52 * <p>The "destructive" algorithms contained in this class, that is, the
53 * algorithms that modify the collection on which they operate, are specified
1093 }
1094 public boolean remove(Object o) {
1095 throw new UnsupportedOperationException();
1096 }
1097
1098 public boolean containsAll(Collection<?> coll) {
1099 return c.containsAll(coll);
1100 }
1101 public boolean addAll(Collection<? extends E> coll) {
1102 throw new UnsupportedOperationException();
1103 }
1104 public boolean removeAll(Collection<?> coll) {
1105 throw new UnsupportedOperationException();
1106 }
1107 public boolean retainAll(Collection<?> coll) {
1108 throw new UnsupportedOperationException();
1109 }
1110 public void clear() {
1111 throw new UnsupportedOperationException();
1112 }
1113 }
1114
1115 /**
1116 * Returns an unmodifiable view of the specified set. This method allows
1117 * modules to provide users with "read-only" access to internal sets.
1118 * Query operations on the returned set "read through" to the specified
1119 * set, and attempts to modify the returned set, whether direct or via its
1120 * iterator, result in an <tt>UnsupportedOperationException</tt>.<p>
1121 *
1122 * The returned set will be serializable if the specified set
1123 * is serializable.
1124 *
1125 * @param s the set for which an unmodifiable view is to be returned.
1126 * @return an unmodifiable view of the specified set.
1127 */
1128 public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1129 return new UnmodifiableSet<>(s);
1130 }
1131
1132 /**
1223 }
1224
1225 public boolean equals(Object o) {return o == this || list.equals(o);}
1226 public int hashCode() {return list.hashCode();}
1227
1228 public E get(int index) {return list.get(index);}
1229 public E set(int index, E element) {
1230 throw new UnsupportedOperationException();
1231 }
1232 public void add(int index, E element) {
1233 throw new UnsupportedOperationException();
1234 }
1235 public E remove(int index) {
1236 throw new UnsupportedOperationException();
1237 }
1238 public int indexOf(Object o) {return list.indexOf(o);}
1239 public int lastIndexOf(Object o) {return list.lastIndexOf(o);}
1240 public boolean addAll(int index, Collection<? extends E> c) {
1241 throw new UnsupportedOperationException();
1242 }
1243 public ListIterator<E> listIterator() {return listIterator(0);}
1244
1245 public ListIterator<E> listIterator(final int index) {
1246 return new ListIterator<E>() {
1247 private final ListIterator<? extends E> i
1248 = list.listIterator(index);
1249
1250 public boolean hasNext() {return i.hasNext();}
1251 public E next() {return i.next();}
1252 public boolean hasPrevious() {return i.hasPrevious();}
1253 public E previous() {return i.previous();}
1254 public int nextIndex() {return i.nextIndex();}
1255 public int previousIndex() {return i.previousIndex();}
1256
1257 public void remove() {
1258 throw new UnsupportedOperationException();
1259 }
1260 public void set(E e) {
1261 throw new UnsupportedOperationException();
1262 }
1725 synchronized (mutex) {return c.containsAll(coll);}
1726 }
1727 public boolean addAll(Collection<? extends E> coll) {
1728 synchronized (mutex) {return c.addAll(coll);}
1729 }
1730 public boolean removeAll(Collection<?> coll) {
1731 synchronized (mutex) {return c.removeAll(coll);}
1732 }
1733 public boolean retainAll(Collection<?> coll) {
1734 synchronized (mutex) {return c.retainAll(coll);}
1735 }
1736 public void clear() {
1737 synchronized (mutex) {c.clear();}
1738 }
1739 public String toString() {
1740 synchronized (mutex) {return c.toString();}
1741 }
1742 private void writeObject(ObjectOutputStream s) throws IOException {
1743 synchronized (mutex) {s.defaultWriteObject();}
1744 }
1745 }
1746
1747 /**
1748 * Returns a synchronized (thread-safe) set backed by the specified
1749 * set. In order to guarantee serial access, it is critical that
1750 * <strong>all</strong> access to the backing set is accomplished
1751 * through the returned set.<p>
1752 *
1753 * It is imperative that the user manually synchronize on the returned
1754 * set when iterating over it:
1755 * <pre>
1756 * Set s = Collections.synchronizedSet(new HashSet());
1757 * ...
1758 * synchronized (s) {
1759 * Iterator i = s.iterator(); // Must be in the synchronized block
1760 * while (i.hasNext())
1761 * foo(i.next());
1762 * }
1763 * </pre>
1764 * Failure to follow this advice may result in non-deterministic behavior.
1979
1980 public boolean addAll(int index, Collection<? extends E> c) {
1981 synchronized (mutex) {return list.addAll(index, c);}
1982 }
1983
1984 public ListIterator<E> listIterator() {
1985 return list.listIterator(); // Must be manually synched by user
1986 }
1987
1988 public ListIterator<E> listIterator(int index) {
1989 return list.listIterator(index); // Must be manually synched by user
1990 }
1991
1992 public List<E> subList(int fromIndex, int toIndex) {
1993 synchronized (mutex) {
1994 return new SynchronizedList<>(list.subList(fromIndex, toIndex),
1995 mutex);
1996 }
1997 }
1998
1999 /**
2000 * SynchronizedRandomAccessList instances are serialized as
2001 * SynchronizedList instances to allow them to be deserialized
2002 * in pre-1.4 JREs (which do not have SynchronizedRandomAccessList).
2003 * This method inverts the transformation. As a beneficial
2004 * side-effect, it also grafts the RandomAccess marker onto
2005 * SynchronizedList instances that were serialized in pre-1.4 JREs.
2006 *
2007 * Note: Unfortunately, SynchronizedRandomAccessList instances
2008 * serialized in 1.4.1 and deserialized in 1.4 will become
2009 * SynchronizedList instances, as this method was missing in 1.4.
2010 */
2011 private Object readResolve() {
2012 return (list instanceof RandomAccess
2013 ? new SynchronizedRandomAccessList<>(list)
2014 : this);
2015 }
2016 }
2017
2018 /**
2475 // To get better and consistent diagnostics,
2476 // we call typeCheck explicitly on each element.
2477 // We call clone() to defend against coll retaining a
2478 // reference to the returned array and storing a bad
2479 // element into it after it has been type checked.
2480 a = coll.toArray().clone();
2481 for (Object o : a)
2482 typeCheck(o);
2483 }
2484 // A slight abuse of the type system, but safe here.
2485 return (Collection<E>) Arrays.asList(a);
2486 }
2487
2488 public boolean addAll(Collection<? extends E> coll) {
2489 // Doing things this way insulates us from concurrent changes
2490 // in the contents of coll and provides all-or-nothing
2491 // semantics (which we wouldn't get if we type-checked each
2492 // element as we added it)
2493 return c.addAll(checkedCopyOf(coll));
2494 }
2495 }
2496
2497 /**
2498 * Returns a dynamically typesafe view of the specified queue.
2499 * Any attempt to insert an element of the wrong type will result in
2500 * an immediate {@link ClassCastException}. Assuming a queue contains
2501 * no incorrectly typed elements prior to the time a dynamically typesafe
2502 * view is generated, and that all subsequent access to the queue
2503 * takes place through the view, it is <i>guaranteed</i> that the
2504 * queue cannot contain an incorrectly typed element.
2505 *
2506 * <p>A discussion of the use of dynamically typesafe views may be
2507 * found in the documentation for the {@link #checkedCollection
2508 * checkedCollection} method.
2509 *
2510 * <p>The returned queue will be serializable if the specified queue
2511 * is serializable.
2512 *
2513 * <p>Since {@code null} is considered to be a value of any reference
2514 * type, the returned queue permits insertion of {@code null} elements
2736 public E previous() { return i.previous(); }
2737 public int nextIndex() { return i.nextIndex(); }
2738 public int previousIndex() { return i.previousIndex(); }
2739 public void remove() { i.remove(); }
2740
2741 public void set(E e) {
2742 typeCheck(e);
2743 i.set(e);
2744 }
2745
2746 public void add(E e) {
2747 typeCheck(e);
2748 i.add(e);
2749 }
2750 };
2751 }
2752
2753 public List<E> subList(int fromIndex, int toIndex) {
2754 return new CheckedList<>(list.subList(fromIndex, toIndex), type);
2755 }
2756 }
2757
2758 /**
2759 * @serial include
2760 */
2761 static class CheckedRandomAccessList<E> extends CheckedList<E>
2762 implements RandomAccess
2763 {
2764 private static final long serialVersionUID = 1638200125423088369L;
2765
2766 CheckedRandomAccessList(List<E> list, Class<E> type) {
2767 super(list, type);
2768 }
2769
2770 public List<E> subList(int fromIndex, int toIndex) {
2771 return new CheckedRandomAccessList<>(
2772 list.subList(fromIndex, toIndex), type);
2773 }
2774 }
2775
3399 implements Serializable
3400 {
3401 private static final long serialVersionUID = 1582296315990362920L;
3402
3403 public Iterator<E> iterator() { return emptyIterator(); }
3404
3405 public int size() {return 0;}
3406 public boolean isEmpty() {return true;}
3407
3408 public boolean contains(Object obj) {return false;}
3409 public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
3410
3411 public Object[] toArray() { return new Object[0]; }
3412
3413 public <T> T[] toArray(T[] a) {
3414 if (a.length > 0)
3415 a[0] = null;
3416 return a;
3417 }
3418
3419 // Preserves singleton property
3420 private Object readResolve() {
3421 return EMPTY_SET;
3422 }
3423 }
3424
3425 /**
3426 * Returns the empty sorted set (immutable). This set is serializable.
3427 *
3428 * <p>This example illustrates the type-safe way to obtain an empty sorted
3429 * set:
3430 * <pre>
3431 * SortedSet<String> s = Collections.emptySortedSet();
3432 * </pre>
3433 * Implementation note: Implementations of this method need not
3434 * create a separate <tt>SortedSet</tt> object for each call.
3435 *
3436 * @since 1.8
3437 */
3438 @SuppressWarnings("unchecked")
3506 @Override
3507 public SortedSet<E> tailSet(Object fromElement) {
3508 Objects.requireNonNull(fromElement);
3509
3510 if (!(fromElement instanceof Comparable)) {
3511 throw new ClassCastException();
3512 }
3513
3514 return emptySortedSet();
3515 }
3516
3517 @Override
3518 public E first() {
3519 throw new NoSuchElementException();
3520 }
3521
3522 @Override
3523 public E last() {
3524 throw new NoSuchElementException();
3525 }
3526 }
3527
3528 /**
3529 * The empty list (immutable). This list is serializable.
3530 *
3531 * @see #emptyList()
3532 */
3533 @SuppressWarnings("rawtypes")
3534 public static final List EMPTY_LIST = new EmptyList<>();
3535
3536 /**
3537 * Returns the empty list (immutable). This list is serializable.
3538 *
3539 * <p>This example illustrates the type-safe way to obtain an empty list:
3540 * <pre>
3541 * List<String> s = Collections.emptyList();
3542 * </pre>
3543 * Implementation note: Implementations of this method need not
3544 * create a separate <tt>List</tt> object for each call. Using this
3545 * method is likely to have comparable cost to using the like-named
3575 public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
3576
3577 public Object[] toArray() { return new Object[0]; }
3578
3579 public <T> T[] toArray(T[] a) {
3580 if (a.length > 0)
3581 a[0] = null;
3582 return a;
3583 }
3584
3585 public E get(int index) {
3586 throw new IndexOutOfBoundsException("Index: "+index);
3587 }
3588
3589 public boolean equals(Object o) {
3590 return (o instanceof List) && ((List<?>)o).isEmpty();
3591 }
3592
3593 public int hashCode() { return 1; }
3594
3595 // Preserves singleton property
3596 private Object readResolve() {
3597 return EMPTY_LIST;
3598 }
3599 }
3600
3601 /**
3602 * The empty map (immutable). This map is serializable.
3603 *
3604 * @see #emptyMap()
3605 * @since 1.3
3606 */
3607 @SuppressWarnings("rawtypes")
3608 public static final Map EMPTY_MAP = new EmptyMap<>();
3609
3610 /**
3611 * Returns the empty map (immutable). This map is serializable.
3612 *
3613 * <p>This example illustrates the type-safe way to obtain an empty set:
3614 * <pre>
3753 /**
3754 * @serial include
3755 */
3756 private static class SingletonSet<E>
3757 extends AbstractSet<E>
3758 implements Serializable
3759 {
3760 private static final long serialVersionUID = 3193687207550431679L;
3761
3762 private final E element;
3763
3764 SingletonSet(E e) {element = e;}
3765
3766 public Iterator<E> iterator() {
3767 return singletonIterator(element);
3768 }
3769
3770 public int size() {return 1;}
3771
3772 public boolean contains(Object o) {return eq(o, element);}
3773 }
3774
3775 /**
3776 * Returns an immutable list containing only the specified object.
3777 * The returned list is serializable.
3778 *
3779 * @param o the sole object to be stored in the returned list.
3780 * @return an immutable list containing only the specified object.
3781 * @since 1.3
3782 */
3783 public static <T> List<T> singletonList(T o) {
3784 return new SingletonList<>(o);
3785 }
3786
3787 /**
3788 * @serial include
3789 */
3790 private static class SingletonList<E>
3791 extends AbstractList<E>
3792 implements RandomAccess, Serializable {
3793
3794 private static final long serialVersionUID = 3093736618740652951L;
3795
3796 private final E element;
3797
3798 SingletonList(E obj) {element = obj;}
3799
3800 public Iterator<E> iterator() {
3801 return singletonIterator(element);
3802 }
3803
3804 public int size() {return 1;}
3805
3806 public boolean contains(Object obj) {return eq(obj, element);}
3807
3808 public E get(int index) {
3809 if (index != 0)
3810 throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
3811 return element;
3812 }
3813 }
3814
3815 /**
3816 * Returns an immutable map, mapping only the specified key to the
3817 * specified value. The returned map is serializable.
3818 *
3819 * @param key the sole key to be stored in the returned map.
3820 * @param value the value to which the returned map maps <tt>key</tt>.
3821 * @return an immutable map containing only the specified key-value
3822 * mapping.
3823 * @since 1.3
3824 */
3825 public static <K,V> Map<K,V> singletonMap(K key, V value) {
3826 return new SingletonMap<>(key, value);
3827 }
3828
3829 /**
3830 * @serial include
3831 */
3832 private static class SingletonMap<K,V>
4391 s = map.keySet();
4392 }
4393
4394 public void clear() { m.clear(); }
4395 public int size() { return m.size(); }
4396 public boolean isEmpty() { return m.isEmpty(); }
4397 public boolean contains(Object o) { return m.containsKey(o); }
4398 public boolean remove(Object o) { return m.remove(o) != null; }
4399 public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
4400 public Iterator<E> iterator() { return s.iterator(); }
4401 public Object[] toArray() { return s.toArray(); }
4402 public <T> T[] toArray(T[] a) { return s.toArray(a); }
4403 public String toString() { return s.toString(); }
4404 public int hashCode() { return s.hashCode(); }
4405 public boolean equals(Object o) { return o == this || s.equals(o); }
4406 public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
4407 public boolean removeAll(Collection<?> c) {return s.removeAll(c);}
4408 public boolean retainAll(Collection<?> c) {return s.retainAll(c);}
4409 // addAll is the only inherited implementation
4410
4411 private static final long serialVersionUID = 2454657854757543876L;
4412
4413 private void readObject(java.io.ObjectInputStream stream)
4414 throws IOException, ClassNotFoundException
4415 {
4416 stream.defaultReadObject();
4417 s = m.keySet();
4418 }
4419 }
4420
4421 /**
4422 * Returns a view of a {@link Deque} as a Last-in-first-out (Lifo)
4423 * {@link Queue}. Method <tt>add</tt> is mapped to <tt>push</tt>,
4424 * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
4425 * view can be useful when you would like to use a method
4426 * requiring a <tt>Queue</tt> but you need Lifo ordering.
4427 *
4428 * <p>Each method invocation on the queue returned by this method
4429 * results in exactly one method invocation on the backing deque, with
4430 * one exception. The {@link Queue#addAll addAll} method is
4449 AsLIFOQueue(Deque<E> q) { this.q = q; }
4450 public boolean add(E e) { q.addFirst(e); return true; }
4451 public boolean offer(E e) { return q.offerFirst(e); }
4452 public E poll() { return q.pollFirst(); }
4453 public E remove() { return q.removeFirst(); }
4454 public E peek() { return q.peekFirst(); }
4455 public E element() { return q.getFirst(); }
4456 public void clear() { q.clear(); }
4457 public int size() { return q.size(); }
4458 public boolean isEmpty() { return q.isEmpty(); }
4459 public boolean contains(Object o) { return q.contains(o); }
4460 public boolean remove(Object o) { return q.remove(o); }
4461 public Iterator<E> iterator() { return q.iterator(); }
4462 public Object[] toArray() { return q.toArray(); }
4463 public <T> T[] toArray(T[] a) { return q.toArray(a); }
4464 public String toString() { return q.toString(); }
4465 public boolean containsAll(Collection<?> c) {return q.containsAll(c);}
4466 public boolean removeAll(Collection<?> c) {return q.removeAll(c);}
4467 public boolean retainAll(Collection<?> c) {return q.retainAll(c);}
4468 // We use inherited addAll; forwarding addAll would be wrong
4469 }
4470 }
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27 import java.io.Serializable;
28 import java.io.ObjectOutputStream;
29 import java.io.IOException;
30 import java.lang.reflect.Array;
31 import java.util.function.BiConsumer;
32 import java.util.function.BiFunction;
33 import java.util.function.Consumer;
34 import java.util.function.Function;
35 import java.util.function.Predicate;
36 import java.util.function.UnaryOperator;
37
38 /**
39 * This class consists exclusively of static methods that operate on or return
40 * collections. It contains polymorphic algorithms that operate on
41 * collections, "wrappers", which return a new collection backed by a
42 * specified collection, and a few other odds and ends.
43 *
44 * <p>The methods of this class all throw a <tt>NullPointerException</tt>
45 * if the collections or class objects provided to them are null.
46 *
47 * <p>The documentation for the polymorphic algorithms contained in this class
48 * generally includes a brief description of the <i>implementation</i>. Such
49 * descriptions should be regarded as <i>implementation notes</i>, rather than
50 * parts of the <i>specification</i>. Implementors should feel free to
51 * substitute other algorithms, so long as the specification itself is adhered
52 * to. (For example, the algorithm used by <tt>sort</tt> does not have to be
53 * a mergesort, but it does have to be <i>stable</i>.)
54 *
55 * <p>The "destructive" algorithms contained in this class, that is, the
56 * algorithms that modify the collection on which they operate, are specified
1096 }
1097 public boolean remove(Object o) {
1098 throw new UnsupportedOperationException();
1099 }
1100
1101 public boolean containsAll(Collection<?> coll) {
1102 return c.containsAll(coll);
1103 }
1104 public boolean addAll(Collection<? extends E> coll) {
1105 throw new UnsupportedOperationException();
1106 }
1107 public boolean removeAll(Collection<?> coll) {
1108 throw new UnsupportedOperationException();
1109 }
1110 public boolean retainAll(Collection<?> coll) {
1111 throw new UnsupportedOperationException();
1112 }
1113 public void clear() {
1114 throw new UnsupportedOperationException();
1115 }
1116
1117 @Override
1118 public void forEach(Consumer<? super E> action) {
1119 c.forEach(action);
1120 }
1121 @Override
1122 public boolean removeIf(Predicate<? super E> filter) {
1123 throw new UnsupportedOperationException();
1124 }
1125 }
1126
1127 /**
1128 * Returns an unmodifiable view of the specified set. This method allows
1129 * modules to provide users with "read-only" access to internal sets.
1130 * Query operations on the returned set "read through" to the specified
1131 * set, and attempts to modify the returned set, whether direct or via its
1132 * iterator, result in an <tt>UnsupportedOperationException</tt>.<p>
1133 *
1134 * The returned set will be serializable if the specified set
1135 * is serializable.
1136 *
1137 * @param s the set for which an unmodifiable view is to be returned.
1138 * @return an unmodifiable view of the specified set.
1139 */
1140 public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1141 return new UnmodifiableSet<>(s);
1142 }
1143
1144 /**
1235 }
1236
1237 public boolean equals(Object o) {return o == this || list.equals(o);}
1238 public int hashCode() {return list.hashCode();}
1239
1240 public E get(int index) {return list.get(index);}
1241 public E set(int index, E element) {
1242 throw new UnsupportedOperationException();
1243 }
1244 public void add(int index, E element) {
1245 throw new UnsupportedOperationException();
1246 }
1247 public E remove(int index) {
1248 throw new UnsupportedOperationException();
1249 }
1250 public int indexOf(Object o) {return list.indexOf(o);}
1251 public int lastIndexOf(Object o) {return list.lastIndexOf(o);}
1252 public boolean addAll(int index, Collection<? extends E> c) {
1253 throw new UnsupportedOperationException();
1254 }
1255
1256 @Override
1257 public void replaceAll(UnaryOperator<E> operator) {
1258 throw new UnsupportedOperationException();
1259 }
1260 @Override
1261 public void sort(Comparator<? super E> c) {
1262 throw new UnsupportedOperationException();
1263 }
1264
1265 public ListIterator<E> listIterator() {return listIterator(0);}
1266
1267 public ListIterator<E> listIterator(final int index) {
1268 return new ListIterator<E>() {
1269 private final ListIterator<? extends E> i
1270 = list.listIterator(index);
1271
1272 public boolean hasNext() {return i.hasNext();}
1273 public E next() {return i.next();}
1274 public boolean hasPrevious() {return i.hasPrevious();}
1275 public E previous() {return i.previous();}
1276 public int nextIndex() {return i.nextIndex();}
1277 public int previousIndex() {return i.previousIndex();}
1278
1279 public void remove() {
1280 throw new UnsupportedOperationException();
1281 }
1282 public void set(E e) {
1283 throw new UnsupportedOperationException();
1284 }
1747 synchronized (mutex) {return c.containsAll(coll);}
1748 }
1749 public boolean addAll(Collection<? extends E> coll) {
1750 synchronized (mutex) {return c.addAll(coll);}
1751 }
1752 public boolean removeAll(Collection<?> coll) {
1753 synchronized (mutex) {return c.removeAll(coll);}
1754 }
1755 public boolean retainAll(Collection<?> coll) {
1756 synchronized (mutex) {return c.retainAll(coll);}
1757 }
1758 public void clear() {
1759 synchronized (mutex) {c.clear();}
1760 }
1761 public String toString() {
1762 synchronized (mutex) {return c.toString();}
1763 }
1764 private void writeObject(ObjectOutputStream s) throws IOException {
1765 synchronized (mutex) {s.defaultWriteObject();}
1766 }
1767
1768 @Override
1769 public void forEach(Consumer<? super E> action) {
1770 synchronized (mutex) {c.forEach(action);}
1771 }
1772 @Override
1773 public boolean removeIf(Predicate<? super E> filter) {
1774 synchronized (mutex) {return c.removeIf(filter);}
1775 }
1776 }
1777
1778 /**
1779 * Returns a synchronized (thread-safe) set backed by the specified
1780 * set. In order to guarantee serial access, it is critical that
1781 * <strong>all</strong> access to the backing set is accomplished
1782 * through the returned set.<p>
1783 *
1784 * It is imperative that the user manually synchronize on the returned
1785 * set when iterating over it:
1786 * <pre>
1787 * Set s = Collections.synchronizedSet(new HashSet());
1788 * ...
1789 * synchronized (s) {
1790 * Iterator i = s.iterator(); // Must be in the synchronized block
1791 * while (i.hasNext())
1792 * foo(i.next());
1793 * }
1794 * </pre>
1795 * Failure to follow this advice may result in non-deterministic behavior.
2010
2011 public boolean addAll(int index, Collection<? extends E> c) {
2012 synchronized (mutex) {return list.addAll(index, c);}
2013 }
2014
2015 public ListIterator<E> listIterator() {
2016 return list.listIterator(); // Must be manually synched by user
2017 }
2018
2019 public ListIterator<E> listIterator(int index) {
2020 return list.listIterator(index); // Must be manually synched by user
2021 }
2022
2023 public List<E> subList(int fromIndex, int toIndex) {
2024 synchronized (mutex) {
2025 return new SynchronizedList<>(list.subList(fromIndex, toIndex),
2026 mutex);
2027 }
2028 }
2029
2030 @Override
2031 public void replaceAll(UnaryOperator<E> operator) {
2032 synchronized (mutex) {list.replaceAll(operator);}
2033 }
2034 @Override
2035 public void sort(Comparator<? super E> c) {
2036 synchronized (mutex) {list.sort(c);}
2037 }
2038
2039 /**
2040 * SynchronizedRandomAccessList instances are serialized as
2041 * SynchronizedList instances to allow them to be deserialized
2042 * in pre-1.4 JREs (which do not have SynchronizedRandomAccessList).
2043 * This method inverts the transformation. As a beneficial
2044 * side-effect, it also grafts the RandomAccess marker onto
2045 * SynchronizedList instances that were serialized in pre-1.4 JREs.
2046 *
2047 * Note: Unfortunately, SynchronizedRandomAccessList instances
2048 * serialized in 1.4.1 and deserialized in 1.4 will become
2049 * SynchronizedList instances, as this method was missing in 1.4.
2050 */
2051 private Object readResolve() {
2052 return (list instanceof RandomAccess
2053 ? new SynchronizedRandomAccessList<>(list)
2054 : this);
2055 }
2056 }
2057
2058 /**
2515 // To get better and consistent diagnostics,
2516 // we call typeCheck explicitly on each element.
2517 // We call clone() to defend against coll retaining a
2518 // reference to the returned array and storing a bad
2519 // element into it after it has been type checked.
2520 a = coll.toArray().clone();
2521 for (Object o : a)
2522 typeCheck(o);
2523 }
2524 // A slight abuse of the type system, but safe here.
2525 return (Collection<E>) Arrays.asList(a);
2526 }
2527
2528 public boolean addAll(Collection<? extends E> coll) {
2529 // Doing things this way insulates us from concurrent changes
2530 // in the contents of coll and provides all-or-nothing
2531 // semantics (which we wouldn't get if we type-checked each
2532 // element as we added it)
2533 return c.addAll(checkedCopyOf(coll));
2534 }
2535
2536 @Override
2537 public void forEach(Consumer<? super E> action) {
2538 c.forEach(action);
2539 }
2540 @Override
2541 public boolean removeIf(Predicate<? super E> filter) {
2542 return c.removeIf(filter);
2543 }
2544 }
2545
2546 /**
2547 * Returns a dynamically typesafe view of the specified queue.
2548 * Any attempt to insert an element of the wrong type will result in
2549 * an immediate {@link ClassCastException}. Assuming a queue contains
2550 * no incorrectly typed elements prior to the time a dynamically typesafe
2551 * view is generated, and that all subsequent access to the queue
2552 * takes place through the view, it is <i>guaranteed</i> that the
2553 * queue cannot contain an incorrectly typed element.
2554 *
2555 * <p>A discussion of the use of dynamically typesafe views may be
2556 * found in the documentation for the {@link #checkedCollection
2557 * checkedCollection} method.
2558 *
2559 * <p>The returned queue will be serializable if the specified queue
2560 * is serializable.
2561 *
2562 * <p>Since {@code null} is considered to be a value of any reference
2563 * type, the returned queue permits insertion of {@code null} elements
2785 public E previous() { return i.previous(); }
2786 public int nextIndex() { return i.nextIndex(); }
2787 public int previousIndex() { return i.previousIndex(); }
2788 public void remove() { i.remove(); }
2789
2790 public void set(E e) {
2791 typeCheck(e);
2792 i.set(e);
2793 }
2794
2795 public void add(E e) {
2796 typeCheck(e);
2797 i.add(e);
2798 }
2799 };
2800 }
2801
2802 public List<E> subList(int fromIndex, int toIndex) {
2803 return new CheckedList<>(list.subList(fromIndex, toIndex), type);
2804 }
2805
2806 @Override
2807 public void replaceAll(UnaryOperator<E> operator) {
2808 list.replaceAll(operator);
2809 }
2810 @Override
2811 public void sort(Comparator<? super E> c) {
2812 list.sort(c);
2813 }
2814 }
2815
2816 /**
2817 * @serial include
2818 */
2819 static class CheckedRandomAccessList<E> extends CheckedList<E>
2820 implements RandomAccess
2821 {
2822 private static final long serialVersionUID = 1638200125423088369L;
2823
2824 CheckedRandomAccessList(List<E> list, Class<E> type) {
2825 super(list, type);
2826 }
2827
2828 public List<E> subList(int fromIndex, int toIndex) {
2829 return new CheckedRandomAccessList<>(
2830 list.subList(fromIndex, toIndex), type);
2831 }
2832 }
2833
3457 implements Serializable
3458 {
3459 private static final long serialVersionUID = 1582296315990362920L;
3460
3461 public Iterator<E> iterator() { return emptyIterator(); }
3462
3463 public int size() {return 0;}
3464 public boolean isEmpty() {return true;}
3465
3466 public boolean contains(Object obj) {return false;}
3467 public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
3468
3469 public Object[] toArray() { return new Object[0]; }
3470
3471 public <T> T[] toArray(T[] a) {
3472 if (a.length > 0)
3473 a[0] = null;
3474 return a;
3475 }
3476
3477 @Override
3478 public void forEach(Consumer<? super E> action) {
3479 Objects.requireNonNull(action);
3480 }
3481 @Override
3482 public boolean removeIf(Predicate<? super E> filter) {
3483 Objects.requireNonNull(filter);
3484 return false;
3485 }
3486
3487 // Preserves singleton property
3488 private Object readResolve() {
3489 return EMPTY_SET;
3490 }
3491 }
3492
3493 /**
3494 * Returns the empty sorted set (immutable). This set is serializable.
3495 *
3496 * <p>This example illustrates the type-safe way to obtain an empty sorted
3497 * set:
3498 * <pre>
3499 * SortedSet<String> s = Collections.emptySortedSet();
3500 * </pre>
3501 * Implementation note: Implementations of this method need not
3502 * create a separate <tt>SortedSet</tt> object for each call.
3503 *
3504 * @since 1.8
3505 */
3506 @SuppressWarnings("unchecked")
3574 @Override
3575 public SortedSet<E> tailSet(Object fromElement) {
3576 Objects.requireNonNull(fromElement);
3577
3578 if (!(fromElement instanceof Comparable)) {
3579 throw new ClassCastException();
3580 }
3581
3582 return emptySortedSet();
3583 }
3584
3585 @Override
3586 public E first() {
3587 throw new NoSuchElementException();
3588 }
3589
3590 @Override
3591 public E last() {
3592 throw new NoSuchElementException();
3593 }
3594
3595 @Override
3596 public void forEach(Consumer<? super E> action) {
3597 Objects.requireNonNull(action);
3598 }
3599 @Override
3600 public boolean removeIf(Predicate<? super E> filter) {
3601 Objects.requireNonNull(filter);
3602 return false;
3603 }
3604 }
3605
3606 /**
3607 * The empty list (immutable). This list is serializable.
3608 *
3609 * @see #emptyList()
3610 */
3611 @SuppressWarnings("rawtypes")
3612 public static final List EMPTY_LIST = new EmptyList<>();
3613
3614 /**
3615 * Returns the empty list (immutable). This list is serializable.
3616 *
3617 * <p>This example illustrates the type-safe way to obtain an empty list:
3618 * <pre>
3619 * List<String> s = Collections.emptyList();
3620 * </pre>
3621 * Implementation note: Implementations of this method need not
3622 * create a separate <tt>List</tt> object for each call. Using this
3623 * method is likely to have comparable cost to using the like-named
3653 public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
3654
3655 public Object[] toArray() { return new Object[0]; }
3656
3657 public <T> T[] toArray(T[] a) {
3658 if (a.length > 0)
3659 a[0] = null;
3660 return a;
3661 }
3662
3663 public E get(int index) {
3664 throw new IndexOutOfBoundsException("Index: "+index);
3665 }
3666
3667 public boolean equals(Object o) {
3668 return (o instanceof List) && ((List<?>)o).isEmpty();
3669 }
3670
3671 public int hashCode() { return 1; }
3672
3673 @Override
3674 public void forEach(Consumer<? super E> action) {
3675 Objects.requireNonNull(action);
3676 }
3677 @Override
3678 public boolean removeIf(Predicate<? super E> filter) {
3679 Objects.requireNonNull(filter);
3680 return false;
3681 }
3682 @Override
3683 public void replaceAll(UnaryOperator<E> operator) {
3684 Objects.requireNonNull(operator);
3685 }
3686 @Override
3687 public void sort(Comparator<? super E> c) {
3688 Objects.requireNonNull(c);
3689 }
3690
3691 // Preserves singleton property
3692 private Object readResolve() {
3693 return EMPTY_LIST;
3694 }
3695 }
3696
3697 /**
3698 * The empty map (immutable). This map is serializable.
3699 *
3700 * @see #emptyMap()
3701 * @since 1.3
3702 */
3703 @SuppressWarnings("rawtypes")
3704 public static final Map EMPTY_MAP = new EmptyMap<>();
3705
3706 /**
3707 * Returns the empty map (immutable). This map is serializable.
3708 *
3709 * <p>This example illustrates the type-safe way to obtain an empty set:
3710 * <pre>
3849 /**
3850 * @serial include
3851 */
3852 private static class SingletonSet<E>
3853 extends AbstractSet<E>
3854 implements Serializable
3855 {
3856 private static final long serialVersionUID = 3193687207550431679L;
3857
3858 private final E element;
3859
3860 SingletonSet(E e) {element = e;}
3861
3862 public Iterator<E> iterator() {
3863 return singletonIterator(element);
3864 }
3865
3866 public int size() {return 1;}
3867
3868 public boolean contains(Object o) {return eq(o, element);}
3869
3870 @Override
3871 public void forEach(Consumer<? super E> action) {
3872 action.accept(element);
3873 }
3874 @Override
3875 public boolean removeIf(Predicate<? super E> filter) {
3876 throw new UnsupportedOperationException();
3877 }
3878 }
3879
3880 /**
3881 * Returns an immutable list containing only the specified object.
3882 * The returned list is serializable.
3883 *
3884 * @param o the sole object to be stored in the returned list.
3885 * @return an immutable list containing only the specified object.
3886 * @since 1.3
3887 */
3888 public static <T> List<T> singletonList(T o) {
3889 return new SingletonList<>(o);
3890 }
3891
3892 /**
3893 * @serial include
3894 */
3895 private static class SingletonList<E>
3896 extends AbstractList<E>
3897 implements RandomAccess, Serializable {
3898
3899 private static final long serialVersionUID = 3093736618740652951L;
3900
3901 private final E element;
3902
3903 SingletonList(E obj) {element = obj;}
3904
3905 public Iterator<E> iterator() {
3906 return singletonIterator(element);
3907 }
3908
3909 public int size() {return 1;}
3910
3911 public boolean contains(Object obj) {return eq(obj, element);}
3912
3913 public E get(int index) {
3914 if (index != 0)
3915 throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
3916 return element;
3917 }
3918
3919 @Override
3920 public void forEach(Consumer<? super E> action) {
3921 action.accept(element);
3922 }
3923 @Override
3924 public boolean removeIf(Predicate<? super E> filter) {
3925 throw new UnsupportedOperationException();
3926 }
3927 @Override
3928 public void replaceAll(UnaryOperator<E> operator) {
3929 throw new UnsupportedOperationException();
3930 }
3931 @Override
3932 public void sort(Comparator<? super E> c) {
3933 }
3934 }
3935
3936 /**
3937 * Returns an immutable map, mapping only the specified key to the
3938 * specified value. The returned map is serializable.
3939 *
3940 * @param key the sole key to be stored in the returned map.
3941 * @param value the value to which the returned map maps <tt>key</tt>.
3942 * @return an immutable map containing only the specified key-value
3943 * mapping.
3944 * @since 1.3
3945 */
3946 public static <K,V> Map<K,V> singletonMap(K key, V value) {
3947 return new SingletonMap<>(key, value);
3948 }
3949
3950 /**
3951 * @serial include
3952 */
3953 private static class SingletonMap<K,V>
4512 s = map.keySet();
4513 }
4514
4515 public void clear() { m.clear(); }
4516 public int size() { return m.size(); }
4517 public boolean isEmpty() { return m.isEmpty(); }
4518 public boolean contains(Object o) { return m.containsKey(o); }
4519 public boolean remove(Object o) { return m.remove(o) != null; }
4520 public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
4521 public Iterator<E> iterator() { return s.iterator(); }
4522 public Object[] toArray() { return s.toArray(); }
4523 public <T> T[] toArray(T[] a) { return s.toArray(a); }
4524 public String toString() { return s.toString(); }
4525 public int hashCode() { return s.hashCode(); }
4526 public boolean equals(Object o) { return o == this || s.equals(o); }
4527 public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
4528 public boolean removeAll(Collection<?> c) {return s.removeAll(c);}
4529 public boolean retainAll(Collection<?> c) {return s.retainAll(c);}
4530 // addAll is the only inherited implementation
4531
4532 @Override
4533 public void forEach(Consumer<? super E> action) {
4534 s.forEach(action);
4535 }
4536 @Override
4537 public boolean removeIf(Predicate<? super E> filter) {
4538 return s.removeIf(filter);
4539 }
4540
4541 private static final long serialVersionUID = 2454657854757543876L;
4542
4543 private void readObject(java.io.ObjectInputStream stream)
4544 throws IOException, ClassNotFoundException
4545 {
4546 stream.defaultReadObject();
4547 s = m.keySet();
4548 }
4549 }
4550
4551 /**
4552 * Returns a view of a {@link Deque} as a Last-in-first-out (Lifo)
4553 * {@link Queue}. Method <tt>add</tt> is mapped to <tt>push</tt>,
4554 * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
4555 * view can be useful when you would like to use a method
4556 * requiring a <tt>Queue</tt> but you need Lifo ordering.
4557 *
4558 * <p>Each method invocation on the queue returned by this method
4559 * results in exactly one method invocation on the backing deque, with
4560 * one exception. The {@link Queue#addAll addAll} method is
4579 AsLIFOQueue(Deque<E> q) { this.q = q; }
4580 public boolean add(E e) { q.addFirst(e); return true; }
4581 public boolean offer(E e) { return q.offerFirst(e); }
4582 public E poll() { return q.pollFirst(); }
4583 public E remove() { return q.removeFirst(); }
4584 public E peek() { return q.peekFirst(); }
4585 public E element() { return q.getFirst(); }
4586 public void clear() { q.clear(); }
4587 public int size() { return q.size(); }
4588 public boolean isEmpty() { return q.isEmpty(); }
4589 public boolean contains(Object o) { return q.contains(o); }
4590 public boolean remove(Object o) { return q.remove(o); }
4591 public Iterator<E> iterator() { return q.iterator(); }
4592 public Object[] toArray() { return q.toArray(); }
4593 public <T> T[] toArray(T[] a) { return q.toArray(a); }
4594 public String toString() { return q.toString(); }
4595 public boolean containsAll(Collection<?> c) {return q.containsAll(c);}
4596 public boolean removeAll(Collection<?> c) {return q.removeAll(c);}
4597 public boolean retainAll(Collection<?> c) {return q.retainAll(c);}
4598 // We use inherited addAll; forwarding addAll would be wrong
4599
4600 @Override
4601 public void forEach(Consumer<? super E> action) {
4602 q.forEach(action);
4603 }
4604 @Override
4605 public boolean removeIf(Predicate<? super E> filter) {
4606 return q.removeIf(filter);
4607 }
4608 }
4609 }
|