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
1068 throw new NullPointerException();
1069 this.c = c;
1070 }
1071
1072 public int size() {return c.size();}
1073 public boolean isEmpty() {return c.isEmpty();}
1074 public boolean contains(Object o) {return c.contains(o);}
1075 public Object[] toArray() {return c.toArray();}
1076 public <T> T[] toArray(T[] a) {return c.toArray(a);}
1077 public String toString() {return c.toString();}
1078
1079 public Iterator<E> iterator() {
1080 return new Iterator<E>() {
1081 private final Iterator<? extends E> i = c.iterator();
1082
1083 public boolean hasNext() {return i.hasNext();}
1084 public E next() {return i.next();}
1085 public void remove() {
1086 throw new UnsupportedOperationException();
1087 }
1088 };
1089 }
1090
1091 public boolean add(E e) {
1092 throw new UnsupportedOperationException();
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) {
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 }
1263 public void add(E e) {
1264 throw new UnsupportedOperationException();
1265 }
1266 };
1267 }
1268
1269 public List<E> subList(int fromIndex, int toIndex) {
1270 return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
1271 }
1272
1273 /**
1274 * UnmodifiableRandomAccessList instances are serialized as
1275 * UnmodifiableList instances to allow them to be deserialized
1276 * in pre-1.4 JREs (which do not have UnmodifiableRandomAccessList).
1277 * This method inverts the transformation. As a beneficial
1278 * side-effect, it also grafts the RandomAccess marker onto
1279 * UnmodifiableList instances that were serialized in pre-1.4 JREs.
1280 *
1281 * Note: Unfortunately, UnmodifiableRandomAccessList instances
1282 * serialized in 1.4.1 and deserialized in 1.4 will become
1283 * UnmodifiableList instances, as this method was missing in 1.4.
1284 */
1285 private Object readResolve() {
2730 final ListIterator<E> i = list.listIterator(index);
2731
2732 return new ListIterator<E>() {
2733 public boolean hasNext() { return i.hasNext(); }
2734 public E next() { return i.next(); }
2735 public boolean hasPrevious() { return i.hasPrevious(); }
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
3259 * </ul>
3260 *
3261 * <p>Implementations of this method are permitted, but not
3262 * required, to return the same object from multiple invocations.
3263 *
3264 * @return an empty iterator
3265 * @since 1.7
3266 */
3267 @SuppressWarnings("unchecked")
3268 public static <T> Iterator<T> emptyIterator() {
3269 return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR;
3270 }
3271
3272 private static class EmptyIterator<E> implements Iterator<E> {
3273 static final EmptyIterator<Object> EMPTY_ITERATOR
3274 = new EmptyIterator<>();
3275
3276 public boolean hasNext() { return false; }
3277 public E next() { throw new NoSuchElementException(); }
3278 public void remove() { throw new IllegalStateException(); }
3279 }
3280
3281 /**
3282 * Returns a list iterator that has no elements. More precisely,
3283 *
3284 * <ul compact>
3285 *
3286 * <li>{@link Iterator#hasNext hasNext} and {@link
3287 * ListIterator#hasPrevious hasPrevious} always return {@code
3288 * false}.
3289 *
3290 * <li>{@link Iterator#next next} and {@link ListIterator#previous
3291 * previous} always throw {@link NoSuchElementException}.
3292 *
3293 * <li>{@link Iterator#remove remove} and {@link ListIterator#set
3294 * set} always throw {@link IllegalStateException}.
3295 *
3296 * <li>{@link ListIterator#add add} always throws {@link
3297 * UnsupportedOperationException}.
3298 *
3729 */
3730 public static <T> Set<T> singleton(T o) {
3731 return new SingletonSet<>(o);
3732 }
3733
3734 static <E> Iterator<E> singletonIterator(final E e) {
3735 return new Iterator<E>() {
3736 private boolean hasNext = true;
3737 public boolean hasNext() {
3738 return hasNext;
3739 }
3740 public E next() {
3741 if (hasNext) {
3742 hasNext = false;
3743 return e;
3744 }
3745 throw new NoSuchElementException();
3746 }
3747 public void remove() {
3748 throw new UnsupportedOperationException();
3749 }
3750 };
3751 }
3752
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 }
|
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
36 /**
37 * This class consists exclusively of static methods that operate on or return
38 * collections. It contains polymorphic algorithms that operate on
39 * collections, "wrappers", which return a new collection backed by a
40 * specified collection, and a few other odds and ends.
41 *
42 * <p>The methods of this class all throw a <tt>NullPointerException</tt>
43 * if the collections or class objects provided to them are null.
44 *
45 * <p>The documentation for the polymorphic algorithms contained in this class
46 * generally includes a brief description of the <i>implementation</i>. Such
47 * descriptions should be regarded as <i>implementation notes</i>, rather than
48 * parts of the <i>specification</i>. Implementors should feel free to
49 * substitute other algorithms, so long as the specification itself is adhered
50 * to. (For example, the algorithm used by <tt>sort</tt> does not have to be
51 * a mergesort, but it does have to be <i>stable</i>.)
52 *
53 * <p>The "destructive" algorithms contained in this class, that is, the
1069 throw new NullPointerException();
1070 this.c = c;
1071 }
1072
1073 public int size() {return c.size();}
1074 public boolean isEmpty() {return c.isEmpty();}
1075 public boolean contains(Object o) {return c.contains(o);}
1076 public Object[] toArray() {return c.toArray();}
1077 public <T> T[] toArray(T[] a) {return c.toArray(a);}
1078 public String toString() {return c.toString();}
1079
1080 public Iterator<E> iterator() {
1081 return new Iterator<E>() {
1082 private final Iterator<? extends E> i = c.iterator();
1083
1084 public boolean hasNext() {return i.hasNext();}
1085 public E next() {return i.next();}
1086 public void remove() {
1087 throw new UnsupportedOperationException();
1088 }
1089 @Override
1090 public void forEachRemaining(Consumer<? super E> action) {
1091 i.forEachRemaining(action);
1092 }
1093 };
1094 }
1095
1096 public boolean add(E e) {
1097 throw new UnsupportedOperationException();
1098 }
1099 public boolean remove(Object o) {
1100 throw new UnsupportedOperationException();
1101 }
1102
1103 public boolean containsAll(Collection<?> coll) {
1104 return c.containsAll(coll);
1105 }
1106 public boolean addAll(Collection<? extends E> coll) {
1107 throw new UnsupportedOperationException();
1108 }
1109 public boolean removeAll(Collection<?> coll) {
1110 throw new UnsupportedOperationException();
1111 }
1112 public boolean retainAll(Collection<?> coll) {
1251 return new ListIterator<E>() {
1252 private final ListIterator<? extends E> i
1253 = list.listIterator(index);
1254
1255 public boolean hasNext() {return i.hasNext();}
1256 public E next() {return i.next();}
1257 public boolean hasPrevious() {return i.hasPrevious();}
1258 public E previous() {return i.previous();}
1259 public int nextIndex() {return i.nextIndex();}
1260 public int previousIndex() {return i.previousIndex();}
1261
1262 public void remove() {
1263 throw new UnsupportedOperationException();
1264 }
1265 public void set(E e) {
1266 throw new UnsupportedOperationException();
1267 }
1268 public void add(E e) {
1269 throw new UnsupportedOperationException();
1270 }
1271
1272 @Override
1273 public void forEachRemaining(Consumer<? super E> action) {
1274 i.forEachRemaining(action);
1275 }
1276 };
1277 }
1278
1279 public List<E> subList(int fromIndex, int toIndex) {
1280 return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
1281 }
1282
1283 /**
1284 * UnmodifiableRandomAccessList instances are serialized as
1285 * UnmodifiableList instances to allow them to be deserialized
1286 * in pre-1.4 JREs (which do not have UnmodifiableRandomAccessList).
1287 * This method inverts the transformation. As a beneficial
1288 * side-effect, it also grafts the RandomAccess marker onto
1289 * UnmodifiableList instances that were serialized in pre-1.4 JREs.
1290 *
1291 * Note: Unfortunately, UnmodifiableRandomAccessList instances
1292 * serialized in 1.4.1 and deserialized in 1.4 will become
1293 * UnmodifiableList instances, as this method was missing in 1.4.
1294 */
1295 private Object readResolve() {
2740 final ListIterator<E> i = list.listIterator(index);
2741
2742 return new ListIterator<E>() {
2743 public boolean hasNext() { return i.hasNext(); }
2744 public E next() { return i.next(); }
2745 public boolean hasPrevious() { return i.hasPrevious(); }
2746 public E previous() { return i.previous(); }
2747 public int nextIndex() { return i.nextIndex(); }
2748 public int previousIndex() { return i.previousIndex(); }
2749 public void remove() { i.remove(); }
2750
2751 public void set(E e) {
2752 typeCheck(e);
2753 i.set(e);
2754 }
2755
2756 public void add(E e) {
2757 typeCheck(e);
2758 i.add(e);
2759 }
2760
2761 @Override
2762 public void forEachRemaining(Consumer<? super E> action) {
2763 i.forEachRemaining(action);
2764 }
2765 };
2766 }
2767
2768 public List<E> subList(int fromIndex, int toIndex) {
2769 return new CheckedList<>(list.subList(fromIndex, toIndex), type);
2770 }
2771 }
2772
2773 /**
2774 * @serial include
2775 */
2776 static class CheckedRandomAccessList<E> extends CheckedList<E>
2777 implements RandomAccess
2778 {
2779 private static final long serialVersionUID = 1638200125423088369L;
2780
2781 CheckedRandomAccessList(List<E> list, Class<E> type) {
2782 super(list, type);
2783 }
2784
3274 * </ul>
3275 *
3276 * <p>Implementations of this method are permitted, but not
3277 * required, to return the same object from multiple invocations.
3278 *
3279 * @return an empty iterator
3280 * @since 1.7
3281 */
3282 @SuppressWarnings("unchecked")
3283 public static <T> Iterator<T> emptyIterator() {
3284 return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR;
3285 }
3286
3287 private static class EmptyIterator<E> implements Iterator<E> {
3288 static final EmptyIterator<Object> EMPTY_ITERATOR
3289 = new EmptyIterator<>();
3290
3291 public boolean hasNext() { return false; }
3292 public E next() { throw new NoSuchElementException(); }
3293 public void remove() { throw new IllegalStateException(); }
3294 @Override
3295 public void forEachRemaining(Consumer<? super E> action) {
3296 Objects.requireNonNull(action);
3297 }
3298 }
3299
3300 /**
3301 * Returns a list iterator that has no elements. More precisely,
3302 *
3303 * <ul compact>
3304 *
3305 * <li>{@link Iterator#hasNext hasNext} and {@link
3306 * ListIterator#hasPrevious hasPrevious} always return {@code
3307 * false}.
3308 *
3309 * <li>{@link Iterator#next next} and {@link ListIterator#previous
3310 * previous} always throw {@link NoSuchElementException}.
3311 *
3312 * <li>{@link Iterator#remove remove} and {@link ListIterator#set
3313 * set} always throw {@link IllegalStateException}.
3314 *
3315 * <li>{@link ListIterator#add add} always throws {@link
3316 * UnsupportedOperationException}.
3317 *
3748 */
3749 public static <T> Set<T> singleton(T o) {
3750 return new SingletonSet<>(o);
3751 }
3752
3753 static <E> Iterator<E> singletonIterator(final E e) {
3754 return new Iterator<E>() {
3755 private boolean hasNext = true;
3756 public boolean hasNext() {
3757 return hasNext;
3758 }
3759 public E next() {
3760 if (hasNext) {
3761 hasNext = false;
3762 return e;
3763 }
3764 throw new NoSuchElementException();
3765 }
3766 public void remove() {
3767 throw new UnsupportedOperationException();
3768 }
3769 @Override
3770 public void forEachRemaining(Consumer<? super E> action) {
3771 Objects.requireNonNull(action);
3772 if (hasNext) {
3773 action.accept(e);
3774 hasNext = false;
3775 }
3776 }
3777 };
3778 }
3779
3780 /**
3781 * @serial include
3782 */
3783 private static class SingletonSet<E>
3784 extends AbstractSet<E>
3785 implements Serializable
3786 {
3787 private static final long serialVersionUID = 3193687207550431679L;
3788
3789 private final E element;
3790
3791 SingletonSet(E e) {element = e;}
3792
3793 public Iterator<E> iterator() {
3794 return singletonIterator(element);
3795 }
|