jdk/src/java.base/share/classes/java/util/Collections.java
author redestad
Thu, 12 Jan 2017 13:38:27 +0100
changeset 43068 df01087b2c43
parent 42435 349ac3a65124
child 43069 6653ea6dad6e
permissions -rw-r--r--
8166365: Small immutable collections should provide optimized implementations when possible Reviewed-by: smarks, psandoz, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
     2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Array;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
    31
import java.util.function.BiConsumer;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
    32
import java.util.function.BiFunction;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
    33
import java.util.function.Consumer;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
    34
import java.util.function.Function;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
    35
import java.util.function.Predicate;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
    36
import java.util.function.UnaryOperator;
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
    37
import java.util.stream.IntStream;
18794
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
    38
import java.util.stream.Stream;
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
    39
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class consists exclusively of static methods that operate on or return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * collections.  It contains polymorphic algorithms that operate on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * collections, "wrappers", which return a new collection backed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * specified collection, and a few other odds and ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
    47
 * <p>The methods of this class all throw a {@code NullPointerException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * if the collections or class objects provided to them are null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>The documentation for the polymorphic algorithms contained in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * generally includes a brief description of the <i>implementation</i>.  Such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * descriptions should be regarded as <i>implementation notes</i>, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * parts of the <i>specification</i>.  Implementors should feel free to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * substitute other algorithms, so long as the specification itself is adhered
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
    55
 * to.  (For example, the algorithm used by {@code sort} does not have to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * a mergesort, but it does have to be <i>stable</i>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>The "destructive" algorithms contained in this class, that is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * algorithms that modify the collection on which they operate, are specified
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
    60
 * to throw {@code UnsupportedOperationException} if the collection does not
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
    61
 * support the appropriate mutation primitive(s), such as the {@code set}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * method.  These algorithms may, but are not required to, throw this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * exception if an invocation would have no effect on the collection.  For
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
    64
 * example, invoking the {@code sort} method on an unmodifiable list that is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
    65
 * already sorted may or may not throw {@code UnsupportedOperationException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see     Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @see     List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public class Collections {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Suppresses default constructor, ensuring non-instantiability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private Collections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Tuning parameters for algorithms - Many of the List algorithms have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * two implementations, one of which is appropriate for RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * lists, the other for "sequential."  Often, the random access variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * yields better performance on small sequential access lists.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * tuning parameters below determine the cutoff point for what constitutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * a "small" sequential access list for each algorithm.  The values below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * were empirically determined to work well for LinkedList. Hopefully
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * they should be reasonable for other sequential access List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * implementations.  Those doing performance work on this code would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * do well to validate the values of these parameters from time to time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * (The first word of each tuning parameter name is the algorithm to which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * it applies.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int BINARYSEARCH_THRESHOLD   = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final int REVERSE_THRESHOLD        =   18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final int SHUFFLE_THRESHOLD        =    5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final int FILL_THRESHOLD           =   25;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int ROTATE_THRESHOLD         =  100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final int COPY_THRESHOLD           =   10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final int REPLACEALL_THRESHOLD     =   11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final int INDEXOFSUBLIST_THRESHOLD =   35;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Sorts the specified list into ascending order, according to the
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   112
     * {@linkplain Comparable natural ordering} of its elements.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   113
     * All elements in the list must implement the {@link Comparable}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   114
     * interface.  Furthermore, all elements in the list must be
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   115
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   116
     * must not throw a {@code ClassCastException} for any elements
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   117
     * {@code e1} and {@code e2} in the list).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   119
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   120
     * not be reordered as a result of the sort.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   121
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   122
     * <p>The specified list must be modifiable, but need not be resizable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   124
     * @implNote
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   125
     * This implementation defers to the {@link List#sort(Comparator)}
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   126
     * method using the specified list and a {@code null} comparator.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   128
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param  list the list to be sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *         <i>mutually comparable</i> (for example, strings and integers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws UnsupportedOperationException if the specified list's
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   133
     *         list-iterator does not support the {@code set} operation.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   134
     * @throws IllegalArgumentException (optional) if the implementation
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   135
     *         detects that the natural ordering of the list elements is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   136
     *         found to violate the {@link Comparable} contract
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   137
     * @see List#sort(Comparator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   139
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static <T extends Comparable<? super T>> void sort(List<T> list) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   141
        list.sort(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Sorts the specified list according to the order induced by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * specified comparator.  All elements in the list must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * comparable</i> using the specified comparator (that is,
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   148
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   149
     * for any elements {@code e1} and {@code e2} in the list).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   151
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   152
     * not be reordered as a result of the sort.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   153
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   154
     * <p>The specified list must be modifiable, but need not be resizable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   156
     * @implNote
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   157
     * This implementation defers to the {@link List#sort(Comparator)}
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   158
     * method using the specified list and comparator.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   160
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param  list the list to be sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param  c the comparator to determine the order of the list.  A
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   163
     *        {@code null} value indicates that the elements' <i>natural
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *        ordering</i> should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *         <i>mutually comparable</i> using the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws UnsupportedOperationException if the specified list's
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   168
     *         list-iterator does not support the {@code set} operation.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   169
     * @throws IllegalArgumentException (optional) if the comparator is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents: 2
diff changeset
   170
     *         found to violate the {@link Comparator} contract
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   171
     * @see List#sort(Comparator)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   173
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public static <T> void sort(List<T> list, Comparator<? super T> c) {
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
   175
        list.sort(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Searches the specified list for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * search algorithm.  The list must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * according to the {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * elements (as by the {@link #sort(List)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * call.  If it is not sorted, the results are undefined.  If the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * contains multiple elements equal to the specified object, there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>This method runs in log(n) time for a "random access" list (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * provides near-constant-time positional access).  If the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * does not implement the {@link RandomAccess} interface and is large,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * this method will do an iterator-based binary search that performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * O(n) link traversals and O(log n) element comparisons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   194
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param  list the list to be searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param  key the key to be searched for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return the index of the search key, if it is contained in the list;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   198
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *         key would be inserted into the list: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   201
     *         element greater than the key, or {@code list.size()} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *         elements in the list are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *         <i>mutually comparable</i> (for example, strings and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *         integers), or the search key is not mutually comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         with the elements of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static <T>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    int binarySearch(List<? extends Comparable<? super T>> list, T key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return Collections.indexedBinarySearch(list, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return Collections.iteratorBinarySearch(list, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static <T>
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   219
    int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int high = list.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            Comparable<? super T> midVal = list.get(mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            int cmp = midVal.compareTo(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static <T>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int high = list.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        ListIterator<? extends Comparable<? super T>> i = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Comparable<? super T> midVal = get(i, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            int cmp = midVal.compareTo(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Gets the ith element from the given list by repositioning the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * list listIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private static <T> T get(ListIterator<? extends T> i, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        T obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int pos = i.nextIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (pos <= index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                obj = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } while (pos++ < index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                obj = i.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } while (--pos > index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Searches the specified list for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * search algorithm.  The list must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * {@link #sort(List, Comparator) sort(List, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * method), prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * not sorted, the results are undefined.  If the list contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>This method runs in log(n) time for a "random access" list (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * provides near-constant-time positional access).  If the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * does not implement the {@link RandomAccess} interface and is large,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * this method will do an iterator-based binary search that performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * O(n) link traversals and O(log n) element comparisons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   295
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param  list the list to be searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param  key the key to be searched for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param  c the comparator by which the list is ordered.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   299
     *         A {@code null} value indicates that the elements'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return the index of the search key, if it is contained in the list;
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   302
     *         otherwise, <code>(-(<i>insertion point</i>) - 1)</code>.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *         key would be inserted into the list: the index of the first
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   305
     *         element greater than the key, or {@code list.size()} if all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *         elements in the list are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *         or the search key is not mutually comparable with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *         elements of the list using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   314
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (c==null)
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   317
            return binarySearch((List<? extends Comparable<? super T>>) list, key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return Collections.indexedBinarySearch(list, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return Collections.iteratorBinarySearch(list, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private static <T> int indexedBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int high = l.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            T midVal = l.get(mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private static <T> int iteratorBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int high = l.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        ListIterator<? extends T> i = l.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            T midVal = get(i, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Reverses the order of the elements in the specified list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * This method runs in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param  list the list whose elements are to be reversed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @throws UnsupportedOperationException if the specified list or
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   371
     *         its list-iterator does not support the {@code set} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   373
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public static void reverse(List<?> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                swap(list, i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   380
            // instead of using a raw type here, it's possible to capture
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   381
            // the wildcard but it will require a call to a supplementary
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   382
            // private method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            ListIterator fwd = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            ListIterator rev = list.listIterator(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            for (int i=0, mid=list.size()>>1; i<mid; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                Object tmp = fwd.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                fwd.set(rev.previous());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                rev.set(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Randomly permutes the specified list using a default source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * randomness.  All permutations occur with approximately equal
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   396
     * likelihood.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   398
     * <p>The hedge "approximately" is used in the foregoing description because
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * default source of randomness is only approximately an unbiased source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * of independently chosen bits. If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * chosen bits, then the algorithm would choose permutations with perfect
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   402
     * uniformity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   404
     * <p>This implementation traverses the list backwards, from the last
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   405
     * element up to the second, repeatedly swapping a randomly selected element
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   406
     * into the "current position".  Elements are randomly selected from the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * portion of the list that runs from the first element to the current
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   408
     * position, inclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   410
     * <p>This method runs in linear time.  If the specified list does not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * implement the {@link RandomAccess} interface and is large, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * implementation dumps the specified list into an array before shuffling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * it, and dumps the shuffled array back into the list.  This avoids the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * quadratic behavior that would result from shuffling a "sequential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * access" list in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param  list the list to be shuffled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @throws UnsupportedOperationException if the specified list or
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   419
     *         its list-iterator does not support the {@code set} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public static void shuffle(List<?> list) {
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   422
        Random rnd = r;
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   423
        if (rnd == null)
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   424
            r = rnd = new Random(); // harmless race.
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   425
        shuffle(list, rnd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
   427
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private static Random r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Randomly permute the specified list using the specified source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * randomness.  All permutations occur with equal likelihood
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * assuming that the source of randomness is fair.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * This implementation traverses the list backwards, from the last element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * up to the second, repeatedly swapping a randomly selected element into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * the "current position".  Elements are randomly selected from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * portion of the list that runs from the first element to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * position, inclusive.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * This method runs in linear time.  If the specified list does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * implement the {@link RandomAccess} interface and is large, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * implementation dumps the specified list into an array before shuffling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * it, and dumps the shuffled array back into the list.  This avoids the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * quadratic behavior that would result from shuffling a "sequential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * access" list in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param  list the list to be shuffled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param  rnd the source of randomness to use to shuffle the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @throws UnsupportedOperationException if the specified list or its
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   451
     *         list-iterator does not support the {@code set} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   453
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public static void shuffle(List<?> list, Random rnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            for (int i=size; i>1; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                swap(list, i-1, rnd.nextInt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            Object arr[] = list.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            // Shuffle array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            for (int i=size; i>1; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                swap(arr, i-1, rnd.nextInt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // Dump array back into list
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   467
            // instead of using a raw type here, it's possible to capture
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   468
            // the wildcard but it will require a call to a supplementary
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   469
            // private method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            ListIterator it = list.listIterator();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19855
diff changeset
   471
            for (Object e : arr) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                it.next();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19855
diff changeset
   473
                it.set(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Swaps the elements at the specified positions in the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * (If the specified positions are equal, invoking this method leaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * the list unchanged.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param list The list in which to swap elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param i the index of one element to be swapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param j the index of the other element to be swapped.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   486
     * @throws IndexOutOfBoundsException if either {@code i} or {@code j}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *         is out of range (i &lt; 0 || i &gt;= list.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *         || j &lt; 0 || j &gt;= list.size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   491
    @SuppressWarnings({"rawtypes", "unchecked"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public static void swap(List<?> list, int i, int j) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   493
        // instead of using a raw type here, it's possible to capture
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   494
        // the wildcard but it will require a call to a supplementary
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   495
        // private method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        final List l = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        l.set(i, l.set(j, l.get(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Swaps the two specified elements in the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private static void swap(Object[] arr, int i, int j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        Object tmp = arr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        arr[i] = arr[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        arr[j] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Replaces all of the elements of the specified list with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * element. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * This method runs in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   515
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param  list the list to be filled with the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @param  obj The element with which to fill the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @throws UnsupportedOperationException if the specified list or its
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   519
     *         list-iterator does not support the {@code set} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public static <T> void fill(List<? super T> list, T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            for (int i=0; i<size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                list.set(i, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            ListIterator<? super T> itr = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                itr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                itr.set(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Copies all of the elements from one list into another.  After the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * operation, the index of each copied element in the destination list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * will be identical to its index in the source list.  The destination
32994
4dcdbdb9b2c3 8138938: Clarify javadoc for java.util.Collections.copy()
igerasim
parents: 32108
diff changeset
   540
     * list's size must be greater than or equal to the source list's size.
4dcdbdb9b2c3 8138938: Clarify javadoc for java.util.Collections.copy()
igerasim
parents: 32108
diff changeset
   541
     * If it is greater, the remaining elements in the destination list are
4dcdbdb9b2c3 8138938: Clarify javadoc for java.util.Collections.copy()
igerasim
parents: 32108
diff changeset
   542
     * unaffected. <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * This method runs in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   546
     * @param  <T> the class of the objects in the lists
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param  dest The destination list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param  src The source list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @throws IndexOutOfBoundsException if the destination list is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *         to contain the entire source List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @throws UnsupportedOperationException if the destination list's
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   552
     *         list-iterator does not support the {@code set} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public static <T> void copy(List<? super T> dest, List<? extends T> src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        int srcSize = src.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (srcSize > dest.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            throw new IndexOutOfBoundsException("Source does not fit in dest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (srcSize < COPY_THRESHOLD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            (src instanceof RandomAccess && dest instanceof RandomAccess)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            for (int i=0; i<srcSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                dest.set(i, src.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            ListIterator<? super T> di=dest.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            ListIterator<? extends T> si=src.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            for (int i=0; i<srcSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                di.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                di.set(si.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Returns the minimum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <i>natural ordering</i> of its elements.  All elements in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   576
     * collection must implement the {@code Comparable} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Furthermore, all elements in the collection must be <i>mutually
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   578
     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   579
     * {@code ClassCastException} for any elements {@code e1} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   580
     * {@code e2} in the collection).<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   585
     * @param  <T> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param  coll the collection whose minimum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @return the minimum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *         to the <i>natural ordering</i> of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *         not <i>mutually comparable</i> (for example, strings and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *         integers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            if (next.compareTo(candidate) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Returns the minimum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * order induced by the specified comparator.  All elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * collection must be <i>mutually comparable</i> by the specified
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   611
     * comparator (that is, {@code comp.compare(e1, e2)} must not throw a
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   612
     * {@code ClassCastException} for any elements {@code e1} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   613
     * {@code e2} in the collection).<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   618
     * @param  <T> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param  coll the collection whose minimum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param  comp the comparator with which to determine the minimum element.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   621
     *         A {@code null} value indicates that the elements' <i>natural
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *         ordering</i> should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @return the minimum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *         to the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *         not <i>mutually comparable</i> using the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   630
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (comp==null)
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   633
            return (T)min((Collection) coll);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if (comp.compare(next, candidate) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns the maximum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <i>natural ordering</i> of its elements.  All elements in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   649
     * collection must implement the {@code Comparable} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Furthermore, all elements in the collection must be <i>mutually
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   651
     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   652
     * {@code ClassCastException} for any elements {@code e1} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   653
     * {@code e2} in the collection).<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   658
     * @param  <T> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @param  coll the collection whose maximum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return the maximum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *         to the <i>natural ordering</i> of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *         not <i>mutually comparable</i> (for example, strings and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *         integers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (next.compareTo(candidate) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Returns the maximum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * order induced by the specified comparator.  All elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * collection must be <i>mutually comparable</i> by the specified
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   684
     * comparator (that is, {@code comp.compare(e1, e2)} must not throw a
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   685
     * {@code ClassCastException} for any elements {@code e1} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   686
     * {@code e2} in the collection).<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   691
     * @param  <T> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param  coll the collection whose maximum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param  comp the comparator with which to determine the maximum element.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   694
     *         A {@code null} value indicates that the elements' <i>natural
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *        ordering</i> should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @return the maximum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *         to the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *         not <i>mutually comparable</i> using the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   703
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (comp==null)
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
   706
            return (T)max((Collection) coll);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            if (comp.compare(next, candidate) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Rotates the elements in the specified list by the specified distance.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   721
     * After calling this method, the element at index {@code i} will be
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   722
     * the element previously at index {@code (i - distance)} mod
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   723
     * {@code list.size()}, for all values of {@code i} between {@code 0}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   724
     * and {@code list.size()-1}, inclusive.  (This method has no effect on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * the size of the list.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   727
     * <p>For example, suppose {@code list} comprises{@code  [t, a, n, k, s]}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   728
     * After invoking {@code Collections.rotate(list, 1)} (or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   729
     * {@code Collections.rotate(list, -4)}), {@code list} will comprise
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   730
     * {@code [s, t, a, n, k]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * <p>Note that this method can usefully be applied to sublists to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * move one or more elements within a list while preserving the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * order of the remaining elements.  For example, the following idiom
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   735
     * moves the element at index {@code j} forward to position
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   736
     * {@code k} (which must be greater than or equal to {@code j}):
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *     Collections.rotate(list.subList(j, k+1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * </pre>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   740
     * To make this concrete, suppose {@code list} comprises
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   741
     * {@code [a, b, c, d, e]}.  To move the element at index {@code 1}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   742
     * ({@code b}) forward two positions, perform the following invocation:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *     Collections.rotate(l.subList(1, 4), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * </pre>
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   746
     * The resulting list is {@code [a, c, d, b, e]}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * <p>To move more than one element forward, increase the absolute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * of the rotation distance.  To move elements backward, use a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * shift distance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * <p>If the specified list is small or implements the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * RandomAccess} interface, this implementation exchanges the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * element into the location it should go, and then repeatedly exchanges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * the displaced element into the location it should go until a displaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * element is swapped into the first element.  If necessary, the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * is repeated on the second and successive elements, until the rotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * is complete.  If the specified list is large and doesn't implement the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   759
     * {@code RandomAccess} interface, this implementation breaks the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   760
     * list into two sublist views around index {@code -distance mod size}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Then the {@link #reverse(List)} method is invoked on each sublist view,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * and finally it is invoked on the entire list.  For a more complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * description of both algorithms, see Section 2.3 of Jon Bentley's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * <i>Programming Pearls</i> (Addison-Wesley, 1986).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @param list the list to be rotated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @param distance the distance to rotate the list.  There are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *        constraints on this value; it may be zero, negative, or
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   769
     *        greater than {@code list.size()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * @throws UnsupportedOperationException if the specified list or
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   771
     *         its list-iterator does not support the {@code set} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public static void rotate(List<?> list, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            rotate1(list, distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            rotate2(list, distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    private static <T> void rotate1(List<T> list, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        distance = distance % size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if (distance < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            distance += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (distance == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        for (int cycleStart = 0, nMoved = 0; nMoved != size; cycleStart++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            T displaced = list.get(cycleStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            int i = cycleStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                i += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                if (i >= size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    i -= size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                displaced = list.set(i, displaced);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                nMoved ++;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
   800
            } while (i != cycleStart);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    private static void rotate2(List<?> list, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        int mid =  -distance % size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (mid < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            mid += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (mid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        reverse(list.subList(0, mid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        reverse(list.subList(mid, size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        reverse(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Replaces all occurrences of one specified value in a list with another.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   821
     * More formally, replaces with {@code newVal} each element {@code e}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   822
     * in {@code list} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   823
     * {@code (oldVal==null ? e==null : oldVal.equals(e))}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * (This method has no effect on the size of the list.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   826
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @param list the list in which replacement is to occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @param oldVal the old value to be replaced.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   829
     * @param newVal the new value with which {@code oldVal} is to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *        replaced.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   831
     * @return {@code true} if {@code list} contained one or more elements
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   832
     *         {@code e} such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   833
     *         {@code (oldVal==null ?  e==null : oldVal.equals(e))}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @throws UnsupportedOperationException if the specified list or
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   835
     *         its list-iterator does not support the {@code set} operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (oldVal==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    if (list.get(i)==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        list.set(i, newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    if (oldVal.equals(list.get(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        list.set(i, newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            ListIterator<T> itr=list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (oldVal==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    if (itr.next()==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        itr.set(newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    if (oldVal.equals(itr.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                        itr.set(newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Returns the starting position of the first occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * target list within the specified source list, or -1 if there is no
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   881
     * such occurrence.  More formally, returns the lowest index {@code i}
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17168
diff changeset
   882
     * such that {@code source.subList(i, i+target.size()).equals(target)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * or -1 if there is no such index.  (Returns -1 if
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17168
diff changeset
   884
     * {@code target.size() > source.size()})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * <p>This implementation uses the "brute force" technique of scanning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * over the source list, looking for a match with the target at each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * location in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @param source the list in which to search for the first occurrence
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   891
     *        of {@code target}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   892
     * @param target the list to search for as a subList of {@code source}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @return the starting position of the first occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *         target list within the specified source list, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *         is no such occurrence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    public static int indexOfSubList(List<?> source, List<?> target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        int sourceSize = source.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        int targetSize = target.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        int maxCandidate = sourceSize - targetSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            (source instanceof RandomAccess&&target instanceof RandomAccess)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            for (int candidate = 0; candidate <= maxCandidate; candidate++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                for (int i=0, j=candidate; i<targetSize; i++, j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    if (!eq(target.get(i), source.get(j)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        continue nextCand;  // Element mismatch, try next cand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                return candidate;  // All elements of candidate matched target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        } else {  // Iterator version of above algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            ListIterator<?> si = source.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            for (int candidate = 0; candidate <= maxCandidate; candidate++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                ListIterator<?> ti = target.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                for (int i=0; i<targetSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    if (!eq(ti.next(), si.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                        // Back up source iterator to next candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                        for (int j=0; j<i; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                            si.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                        continue nextCand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        return -1;  // No candidate matched the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Returns the starting position of the last occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * target list within the specified source list, or -1 if there is no such
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   934
     * occurrence.  More formally, returns the highest index {@code i}
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17168
diff changeset
   935
     * such that {@code source.subList(i, i+target.size()).equals(target)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * or -1 if there is no such index.  (Returns -1 if
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17168
diff changeset
   937
     * {@code target.size() > source.size()})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * <p>This implementation uses the "brute force" technique of iterating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * over the source list, looking for a match with the target at each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * location in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @param source the list in which to search for the last occurrence
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   944
     *        of {@code target}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   945
     * @param target the list to search for as a subList of {@code source}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @return the starting position of the last occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *         target list within the specified source list, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *         is no such occurrence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public static int lastIndexOfSubList(List<?> source, List<?> target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        int sourceSize = source.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        int targetSize = target.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        int maxCandidate = sourceSize - targetSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            source instanceof RandomAccess) {   // Index access version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            for (int candidate = maxCandidate; candidate >= 0; candidate--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                for (int i=0, j=candidate; i<targetSize; i++, j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    if (!eq(target.get(i), source.get(j)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                        continue nextCand;  // Element mismatch, try next cand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                return candidate;  // All elements of candidate matched target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        } else {  // Iterator version of above algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            if (maxCandidate < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            ListIterator<?> si = source.listIterator(maxCandidate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            for (int candidate = maxCandidate; candidate >= 0; candidate--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                ListIterator<?> ti = target.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                for (int i=0; i<targetSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    if (!eq(ti.next(), si.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                        if (candidate != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                            // Back up source iterator to next candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                            for (int j=0; j<=i+1; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                                si.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                        continue nextCand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        return -1;  // No candidate matched the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    // Unmodifiable Wrappers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Returns an unmodifiable view of the specified collection.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * collections.  Query operations on the returned collection "read through"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * to the specified collection, and attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * collection, whether direct or via its iterator, result in an
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
   997
     * {@code UnsupportedOperationException}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * The returned collection does <i>not</i> pass the hashCode and equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * operations through to the backing collection, but relies on
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1001
     * {@code Object}'s {@code equals} and {@code hashCode} methods.  This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * is necessary to preserve the contracts of these operations in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * that the backing collection is a set or a list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * The returned collection will be serializable if the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1008
     * @param  <T> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @param  c the collection for which an unmodifiable view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *         returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @return an unmodifiable view of the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1014
        return new UnmodifiableCollection<>(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        private static final long serialVersionUID = 1820017752578914078L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        final Collection<? extends E> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        UnmodifiableCollection(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        public int size()                   {return c.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        public boolean isEmpty()            {return c.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        public boolean contains(Object o)   {return c.contains(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        public Object[] toArray()           {return c.toArray();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        public <T> T[] toArray(T[] a)       {return c.toArray(a);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        public String toString()            {return c.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            return new Iterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                private final Iterator<? extends E> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                public E next()          {return i.next();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1047
                @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1048
                public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1049
                    // Use backing collection version
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1050
                    i.forEachRemaining(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1051
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            return c.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1077
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1078
        // Override default methods in Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1079
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1080
        public void forEach(Consumer<? super E> action) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1081
            c.forEach(action);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1082
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1083
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1084
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1085
            throw new UnsupportedOperationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1086
        }
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1087
        @SuppressWarnings("unchecked")
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1088
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1089
        public Spliterator<E> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1090
            return (Spliterator<E>)c.spliterator();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1091
        }
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1092
        @SuppressWarnings("unchecked")
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1093
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1094
        public Stream<E> stream() {
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1095
            return (Stream<E>)c.stream();
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1096
        }
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1097
        @SuppressWarnings("unchecked")
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1098
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1099
        public Stream<E> parallelStream() {
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1100
            return (Stream<E>)c.parallelStream();
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1101
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * Returns an unmodifiable view of the specified set.  This method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * modules to provide users with "read-only" access to internal sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * Query operations on the returned set "read through" to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * set, and attempts to modify the returned set, whether direct or via its
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1109
     * iterator, result in an {@code UnsupportedOperationException}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * The returned set will be serializable if the specified set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1114
     * @param  <T> the class of the objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @param  s the set for which an unmodifiable view is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @return an unmodifiable view of the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1119
        return new UnmodifiableSet<>(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                                 implements Set<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        private static final long serialVersionUID = -9215047833775013803L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        UnmodifiableSet(Set<? extends E> s)     {super(s);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        public boolean equals(Object o) {return o == this || c.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        public int hashCode()           {return c.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * Returns an unmodifiable view of the specified sorted set.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * sorted sets.  Query operations on the returned sorted set "read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * through" to the specified sorted set.  Attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * sorted set, whether direct, via its iterator, or via its
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1140
     * {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1141
     * an {@code UnsupportedOperationException}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * The returned sorted set will be serializable if the specified sorted set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1146
     * @param  <T> the class of the objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * @param s the sorted set for which an unmodifiable view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     *        returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * @return an unmodifiable view of the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1152
        return new UnmodifiableSortedSet<>(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    static class UnmodifiableSortedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                             extends UnmodifiableSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                             implements SortedSet<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        private static final long serialVersionUID = -4929149591599911165L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        private final SortedSet<E> ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        public Comparator<? super E> comparator() {return ss.comparator();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        public SortedSet<E> subSet(E fromElement, E toElement) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1169
            return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        public SortedSet<E> headSet(E toElement) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1172
            return new UnmodifiableSortedSet<>(ss.headSet(toElement));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        public SortedSet<E> tailSet(E fromElement) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1175
            return new UnmodifiableSortedSet<>(ss.tailSet(fromElement));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        public E first()                   {return ss.first();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        public E last()                    {return ss.last();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1183
     * Returns an unmodifiable view of the specified navigable set.  This method
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1184
     * allows modules to provide users with "read-only" access to internal
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1185
     * navigable sets.  Query operations on the returned navigable set "read
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1186
     * through" to the specified navigable set.  Attempts to modify the returned
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1187
     * navigable set, whether direct, via its iterator, or via its
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1188
     * {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1189
     * an {@code UnsupportedOperationException}.<p>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1190
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1191
     * The returned navigable set will be serializable if the specified
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1192
     * navigable set is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1193
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1194
     * @param  <T> the class of the objects in the set
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1195
     * @param s the navigable set for which an unmodifiable view is to be
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1196
     *        returned
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1197
     * @return an unmodifiable view of the specified navigable set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1198
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1199
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1200
    public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1201
        return new UnmodifiableNavigableSet<>(s);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1202
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1203
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1204
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1205
     * Wraps a navigable set and disables all of the mutative operations.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1206
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1207
     * @param <E> type of elements
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1208
     * @serial include
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1209
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1210
    static class UnmodifiableNavigableSet<E>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1211
                             extends UnmodifiableSortedSet<E>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1212
                             implements NavigableSet<E>, Serializable {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1213
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1214
        private static final long serialVersionUID = -6027448201786391929L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1215
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1216
        /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1217
         * A singleton empty unmodifiable navigable set used for
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1218
         * {@link #emptyNavigableSet()}.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1219
         *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1220
         * @param <E> type of elements, if there were any, and bounds
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1221
         */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1222
        private static class EmptyNavigableSet<E> extends UnmodifiableNavigableSet<E>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1223
            implements Serializable {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1224
            private static final long serialVersionUID = -6291252904449939134L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1225
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1226
            public EmptyNavigableSet() {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19855
diff changeset
  1227
                super(new TreeSet<>());
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1228
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1229
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1230
            private Object readResolve()        { return EMPTY_NAVIGABLE_SET; }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1231
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1232
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1233
        @SuppressWarnings("rawtypes")
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1234
        private static final NavigableSet<?> EMPTY_NAVIGABLE_SET =
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1235
                new EmptyNavigableSet<>();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1236
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1237
        /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1238
         * The instance we are protecting.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1239
         */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1240
        private final NavigableSet<E> ns;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1241
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1242
        UnmodifiableNavigableSet(NavigableSet<E> s)         {super(s); ns = s;}
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1243
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1244
        public E lower(E e)                             { return ns.lower(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1245
        public E floor(E e)                             { return ns.floor(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1246
        public E ceiling(E e)                         { return ns.ceiling(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1247
        public E higher(E e)                           { return ns.higher(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1248
        public E pollFirst()     { throw new UnsupportedOperationException(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1249
        public E pollLast()      { throw new UnsupportedOperationException(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1250
        public NavigableSet<E> descendingSet()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1251
                 { return new UnmodifiableNavigableSet<>(ns.descendingSet()); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1252
        public Iterator<E> descendingIterator()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1253
                                         { return descendingSet().iterator(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1254
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1255
        public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1256
            return new UnmodifiableNavigableSet<>(
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1257
                ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1258
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1259
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1260
        public NavigableSet<E> headSet(E toElement, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1261
            return new UnmodifiableNavigableSet<>(
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1262
                ns.headSet(toElement, inclusive));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1263
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1264
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1265
        public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1266
            return new UnmodifiableNavigableSet<>(
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1267
                ns.tailSet(fromElement, inclusive));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1268
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1269
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1270
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1271
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * Returns an unmodifiable view of the specified list.  This method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * lists.  Query operations on the returned list "read through" to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * specified list, and attempts to modify the returned list, whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * direct or via its iterator, result in an
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1277
     * {@code UnsupportedOperationException}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * The returned list will be serializable if the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * is serializable. Similarly, the returned list will implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * {@link RandomAccess} if the specified list does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1283
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * @param  list the list for which an unmodifiable view is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * @return an unmodifiable view of the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    public static <T> List<T> unmodifiableList(List<? extends T> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        return (list instanceof RandomAccess ?
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1289
                new UnmodifiableRandomAccessList<>(list) :
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1290
                new UnmodifiableList<>(list));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    static class UnmodifiableList<E> extends UnmodifiableCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                                  implements List<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        private static final long serialVersionUID = -283967356065247728L;
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1299
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        final List<? extends E> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        UnmodifiableList(List<? extends E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        public boolean equals(Object o) {return o == this || list.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        public int hashCode()           {return list.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        public E get(int index) {return list.get(index);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        public int indexOf(Object o)            {return list.indexOf(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        public int lastIndexOf(Object o)        {return list.lastIndexOf(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1325
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1326
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1327
        public void replaceAll(UnaryOperator<E> operator) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1328
            throw new UnsupportedOperationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1329
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1330
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1331
        public void sort(Comparator<? super E> c) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1332
            throw new UnsupportedOperationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1333
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  1334
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        public ListIterator<E> listIterator()   {return listIterator(0);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            return new ListIterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                private final ListIterator<? extends E> i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                    = list.listIterator(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                public boolean hasNext()     {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                public E next()              {return i.next();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                public boolean hasPrevious() {return i.hasPrevious();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                public E previous()          {return i.previous();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                public int nextIndex()       {return i.nextIndex();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                public int previousIndex()   {return i.previousIndex();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1358
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1359
                @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1360
                public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1361
                    i.forEachRemaining(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1362
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        public List<E> subList(int fromIndex, int toIndex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1367
            return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
         * UnmodifiableRandomAccessList instances are serialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
         * UnmodifiableList instances to allow them to be deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
         * in pre-1.4 JREs (which do not have UnmodifiableRandomAccessList).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
         * This method inverts the transformation.  As a beneficial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
         * side-effect, it also grafts the RandomAccess marker onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
         * UnmodifiableList instances that were serialized in pre-1.4 JREs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
         * Note: Unfortunately, UnmodifiableRandomAccessList instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         * serialized in 1.4.1 and deserialized in 1.4 will become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
         * UnmodifiableList instances, as this method was missing in 1.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            return (list instanceof RandomAccess
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1384
                    ? new UnmodifiableRandomAccessList<>(list)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    : this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                                              implements RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        UnmodifiableRandomAccessList(List<? extends E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        public List<E> subList(int fromIndex, int toIndex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1400
            return new UnmodifiableRandomAccessList<>(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                list.subList(fromIndex, toIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        private static final long serialVersionUID = -2542308836966382001L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
         * Allows instances to be deserialized in pre-1.4 JREs (which do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
         * not have UnmodifiableRandomAccessList).  UnmodifiableList has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
         * a readResolve method that inverts this transformation upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
         * deserialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        private Object writeReplace() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1413
            return new UnmodifiableList<>(list);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * Returns an unmodifiable view of the specified map.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * maps.  Query operations on the returned map "read through"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * to the specified map, and attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * map, whether direct or via its collection views, result in an
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1423
     * {@code UnsupportedOperationException}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * The returned map will be serializable if the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1428
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1429
     * @param <V> the class of the map values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * @param  m the map for which an unmodifiable view is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * @return an unmodifiable view of the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1434
        return new UnmodifiableMap<>(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        private static final long serialVersionUID = -1034234728574286014L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        private final Map<? extends K, ? extends V> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        UnmodifiableMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            if (m==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        public int size()                        {return m.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        public boolean isEmpty()                 {return m.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        public boolean containsKey(Object key)   {return m.containsKey(key);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        public boolean containsValue(Object val) {return m.containsValue(val);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        public V get(Object key)                 {return m.get(key);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  1470
        private transient Set<K> keySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  1471
        private transient Set<Map.Entry<K,V>> entrySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  1472
        private transient Collection<V> values;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            if (keySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                keySet = unmodifiableSet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            if (entrySet==null)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1482
                entrySet = new UnmodifiableEntrySet<>(m.entrySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            if (values==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                values = unmodifiableCollection(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        public boolean equals(Object o) {return o == this || m.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        public int hashCode()           {return m.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        public String toString()        {return m.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1496
        // Override default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1497
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1498
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1499
        public V getOrDefault(Object k, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1500
            // Safe cast as we don't change the value
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1501
            return ((Map<K, V>)m).getOrDefault(k, defaultValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1502
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1503
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1504
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1505
        public void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1506
            m.forEach(action);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1507
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1508
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1509
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1510
        public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1511
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1512
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1513
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1514
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1515
        public V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1516
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1517
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1518
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1519
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1520
        public boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1521
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1522
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1523
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1524
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1525
        public boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1526
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1527
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1528
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1529
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1530
        public V replace(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1531
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1532
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1533
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1534
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1535
        public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1536
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1537
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1538
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1539
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1540
        public V computeIfPresent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1541
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1542
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1543
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1544
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1545
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1546
        public V compute(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1547
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1548
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1549
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1550
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1551
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1552
        public V merge(K key, V value,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1553
                BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1554
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1555
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1556
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
         * We need this class in addition to UnmodifiableSet as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         * Map.Entries themselves permit modification of the backing Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         * via their setValue operation.  This class is subtle: there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
         * many possible attacks that must be thwarted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        static class UnmodifiableEntrySet<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            extends UnmodifiableSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            private static final long serialVersionUID = 7854390611657943733L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1569
            @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
  1571
                // Need to cast to raw in order to work around a limitation in the type system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                super((Set)s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            }
18794
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1574
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1575
            static <K, V> Consumer<Map.Entry<K, V>> entryConsumer(Consumer<? super Entry<K, V>> action) {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1576
                return e -> action.accept(new UnmodifiableEntry<>(e));
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1577
            }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1578
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1579
            public void forEach(Consumer<? super Entry<K, V>> action) {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1580
                Objects.requireNonNull(action);
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1581
                c.forEach(entryConsumer(action));
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1582
            }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1583
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1584
            static final class UnmodifiableEntrySetSpliterator<K, V>
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1585
                    implements Spliterator<Entry<K,V>> {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1586
                final Spliterator<Map.Entry<K, V>> s;
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1587
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1588
                UnmodifiableEntrySetSpliterator(Spliterator<Entry<K, V>> s) {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1589
                    this.s = s;
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1590
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1591
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1592
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1593
                public boolean tryAdvance(Consumer<? super Entry<K, V>> action) {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1594
                    Objects.requireNonNull(action);
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1595
                    return s.tryAdvance(entryConsumer(action));
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1596
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1597
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1598
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1599
                public void forEachRemaining(Consumer<? super Entry<K, V>> action) {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1600
                    Objects.requireNonNull(action);
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1601
                    s.forEachRemaining(entryConsumer(action));
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1602
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1603
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1604
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1605
                public Spliterator<Entry<K, V>> trySplit() {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1606
                    Spliterator<Entry<K, V>> split = s.trySplit();
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1607
                    return split == null
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1608
                           ? null
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1609
                           : new UnmodifiableEntrySetSpliterator<>(split);
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1610
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1611
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1612
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1613
                public long estimateSize() {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1614
                    return s.estimateSize();
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1615
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1616
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1617
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1618
                public long getExactSizeIfKnown() {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1619
                    return s.getExactSizeIfKnown();
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1620
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1621
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1622
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1623
                public int characteristics() {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1624
                    return s.characteristics();
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1625
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1626
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1627
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1628
                public boolean hasCharacteristics(int characteristics) {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1629
                    return s.hasCharacteristics(characteristics);
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1630
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1631
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1632
                @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1633
                public Comparator<? super Entry<K, V>> getComparator() {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1634
                    return s.getComparator();
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1635
                }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1636
            }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1637
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1638
            @SuppressWarnings("unchecked")
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1639
            public Spliterator<Entry<K,V>> spliterator() {
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1640
                return new UnmodifiableEntrySetSpliterator<>(
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1641
                        (Spliterator<Map.Entry<K, V>>) c.spliterator());
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1642
            }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1643
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1644
            @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1645
            public Stream<Entry<K,V>> stream() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18818
diff changeset
  1646
                return StreamSupport.stream(spliterator(), false);
18794
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1647
            }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1648
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1649
            @Override
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1650
            public Stream<Entry<K,V>> parallelStream() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18818
diff changeset
  1651
                return StreamSupport.stream(spliterator(), true);
18794
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1652
            }
17d9c2ec5e47 8017447: Unmodifiable map entry becomes modifiable if taken from a stream of map entries
psandoz
parents: 18571
diff changeset
  1653
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                return new Iterator<Map.Entry<K,V>>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                    private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                    public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                        return i.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                    public Map.Entry<K,V> next() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1662
                        return new UnmodifiableEntry<>(i.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
  1670
            @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                for (int i=0; i<a.length; i++)
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
  1674
                    a[i] = new UnmodifiableEntry<>((Map.Entry<? extends K, ? extends V>)a[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
  1678
            @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                // We don't pass a to c.toArray, to avoid window of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                // vulnerability wherein an unscrupulous multithreaded client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                // could get his hands on raw (unwrapped) Entries from c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                for (int i=0; i<arr.length; i++)
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
  1686
                    arr[i] = new UnmodifiableEntry<>((Map.Entry<? extends K, ? extends V>)arr[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                if (arr.length > a.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                    return (T[])arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                System.arraycopy(arr, 0, a, 0, arr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                if (a.length > arr.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    a[arr.length] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
             * This method is overridden to protect the backing set against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
             * an object with a nefarious equals function that senses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
             * that the equality-candidate is Map.Entry and calls its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
             * setValue method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                return c.contains(
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1707
                    new UnmodifiableEntry<>((Map.Entry<?,?>) o));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
             * The next two methods are overridden to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
             * an unscrupulous List whose contains(Object o) method senses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
             * when o is a Map.Entry, and calls o.setValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            public boolean containsAll(Collection<?> coll) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7816
diff changeset
  1716
                for (Object e : coll) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7816
diff changeset
  1717
                    if (!contains(e)) // Invokes safe contains() above
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                        return false;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7816
diff changeset
  1719
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
  1728
                Set<?> s = (Set<?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                if (s.size() != c.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                return containsAll(s); // Invokes safe containsAll() above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
             * This "wrapper class" serves two purposes: it prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
             * the client from modifying the backing Map, by short-circuiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
             * the setValue method, and it protects the backing Map against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
             * an ill-behaved Map.Entry that attempts to modify another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
             * Map Entry when asked to perform an equality check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            private static class UnmodifiableEntry<K,V> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                private Map.Entry<? extends K, ? extends V> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1744
                UnmodifiableEntry(Map.Entry<? extends K, ? extends V> e)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1745
                        {this.e = Objects.requireNonNull(e);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  1747
                public K getKey()        {return e.getKey();}
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  1748
                public V getValue()      {return e.getValue();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  1752
                public int hashCode()    {return e.hashCode();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                public boolean equals(Object o) {
11987
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  1754
                    if (this == o)
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  1755
                        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                    if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                        return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11987
diff changeset
  1758
                    Map.Entry<?,?> t = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    return eq(e.getKey(),   t.getKey()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                           eq(e.getValue(), t.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  1762
                public String toString() {return e.toString();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * Returns an unmodifiable view of the specified sorted map.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * sorted maps.  Query operations on the returned sorted map "read through"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * to the specified sorted map.  Attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * sorted map, whether direct, via its collection views, or via its
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1773
     * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  1774
     * an {@code UnsupportedOperationException}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * The returned sorted map will be serializable if the specified sorted map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1779
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1780
     * @param <V> the class of the map values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * @param m the sorted map for which an unmodifiable view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *        returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * @return an unmodifiable view of the specified sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1786
        return new UnmodifiableSortedMap<>(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    static class UnmodifiableSortedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
          extends UnmodifiableMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
          implements SortedMap<K,V>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        private static final long serialVersionUID = -8806743815996713206L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        private final SortedMap<K, ? extends V> sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1799
        UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1800
        public Comparator<? super K> comparator()   { return sm.comparator(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1801
        public SortedMap<K,V> subMap(K fromKey, K toKey)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1802
             { return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1803
        public SortedMap<K,V> headMap(K toKey)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1804
                     { return new UnmodifiableSortedMap<>(sm.headMap(toKey)); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1805
        public SortedMap<K,V> tailMap(K fromKey)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1806
                   { return new UnmodifiableSortedMap<>(sm.tailMap(fromKey)); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1807
        public K firstKey()                           { return sm.firstKey(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1808
        public K lastKey()                             { return sm.lastKey(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1809
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1810
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1811
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1812
     * Returns an unmodifiable view of the specified navigable map.  This method
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1813
     * allows modules to provide users with "read-only" access to internal
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1814
     * navigable maps.  Query operations on the returned navigable map "read
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1815
     * through" to the specified navigable map.  Attempts to modify the returned
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1816
     * navigable map, whether direct, via its collection views, or via its
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1817
     * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1818
     * an {@code UnsupportedOperationException}.<p>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1819
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1820
     * The returned navigable map will be serializable if the specified
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1821
     * navigable map is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1822
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1823
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1824
     * @param <V> the class of the map values
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1825
     * @param m the navigable map for which an unmodifiable view is to be
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1826
     *        returned
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1827
     * @return an unmodifiable view of the specified navigable map
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1828
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1829
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1830
    public static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K, ? extends V> m) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1831
        return new UnmodifiableNavigableMap<>(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1834
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1835
     * @serial include
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1836
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1837
    static class UnmodifiableNavigableMap<K,V>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1838
          extends UnmodifiableSortedMap<K,V>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1839
          implements NavigableMap<K,V>, Serializable {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1840
        private static final long serialVersionUID = -4858195264774772197L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1841
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1842
        /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1843
         * A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1844
         * to preserve singleton property.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1845
         *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1846
         * @param <K> type of keys, if there were any, and of bounds
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1847
         * @param <V> type of values, if there were any
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1848
         */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1849
        private static class EmptyNavigableMap<K,V> extends UnmodifiableNavigableMap<K,V>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1850
            implements Serializable {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1851
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1852
            private static final long serialVersionUID = -2239321462712562324L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1853
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19855
diff changeset
  1854
            EmptyNavigableMap()                       { super(new TreeMap<>()); }
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1855
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1856
            @Override
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1857
            public NavigableSet<K> navigableKeySet()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1858
                                                { return emptyNavigableSet(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1859
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1860
            private Object readResolve()        { return EMPTY_NAVIGABLE_MAP; }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1861
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1862
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1863
        /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1864
         * Singleton for {@link emptyNavigableMap()} which is also immutable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1865
         */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1866
        private static final EmptyNavigableMap<?,?> EMPTY_NAVIGABLE_MAP =
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1867
            new EmptyNavigableMap<>();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1868
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1869
        /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1870
         * The instance we wrap and protect.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1871
         */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1872
        private final NavigableMap<K, ? extends V> nm;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1873
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1874
        UnmodifiableNavigableMap(NavigableMap<K, ? extends V> m)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1875
                                                            {super(m); nm = m;}
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1876
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1877
        public K lowerKey(K key)                   { return nm.lowerKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1878
        public K floorKey(K key)                   { return nm.floorKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1879
        public K ceilingKey(K key)               { return nm.ceilingKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1880
        public K higherKey(K key)                 { return nm.higherKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1881
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1882
        @SuppressWarnings("unchecked")
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1883
        public Entry<K, V> lowerEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1884
            Entry<K,V> lower = (Entry<K, V>) nm.lowerEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1885
            return (null != lower)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1886
                ? new UnmodifiableEntrySet.UnmodifiableEntry<>(lower)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1887
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1888
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1889
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1890
        @SuppressWarnings("unchecked")
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1891
        public Entry<K, V> floorEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1892
            Entry<K,V> floor = (Entry<K, V>) nm.floorEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1893
            return (null != floor)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1894
                ? new UnmodifiableEntrySet.UnmodifiableEntry<>(floor)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1895
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1896
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1897
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1898
        @SuppressWarnings("unchecked")
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1899
        public Entry<K, V> ceilingEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1900
            Entry<K,V> ceiling = (Entry<K, V>) nm.ceilingEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1901
            return (null != ceiling)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1902
                ? new UnmodifiableEntrySet.UnmodifiableEntry<>(ceiling)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1903
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1904
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1905
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1906
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1907
        @SuppressWarnings("unchecked")
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1908
        public Entry<K, V> higherEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1909
            Entry<K,V> higher = (Entry<K, V>) nm.higherEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1910
            return (null != higher)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1911
                ? new UnmodifiableEntrySet.UnmodifiableEntry<>(higher)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1912
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1913
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1914
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1915
        @SuppressWarnings("unchecked")
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1916
        public Entry<K, V> firstEntry() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1917
            Entry<K,V> first = (Entry<K, V>) nm.firstEntry();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1918
            return (null != first)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1919
                ? new UnmodifiableEntrySet.UnmodifiableEntry<>(first)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1920
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1921
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1922
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1923
        @SuppressWarnings("unchecked")
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1924
        public Entry<K, V> lastEntry() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1925
            Entry<K,V> last = (Entry<K, V>) nm.lastEntry();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1926
            return (null != last)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  1927
                ? new UnmodifiableEntrySet.UnmodifiableEntry<>(last)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1928
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1929
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1930
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1931
        public Entry<K, V> pollFirstEntry()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1932
                                 { throw new UnsupportedOperationException(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1933
        public Entry<K, V> pollLastEntry()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1934
                                 { throw new UnsupportedOperationException(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1935
        public NavigableMap<K, V> descendingMap()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1936
                       { return unmodifiableNavigableMap(nm.descendingMap()); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1937
        public NavigableSet<K> navigableKeySet()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1938
                     { return unmodifiableNavigableSet(nm.navigableKeySet()); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1939
        public NavigableSet<K> descendingKeySet()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1940
                    { return unmodifiableNavigableSet(nm.descendingKeySet()); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1941
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1942
        public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1943
            return unmodifiableNavigableMap(
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1944
                nm.subMap(fromKey, fromInclusive, toKey, toInclusive));
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1945
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1946
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1947
        public NavigableMap<K, V> headMap(K toKey, boolean inclusive)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1948
             { return unmodifiableNavigableMap(nm.headMap(toKey, inclusive)); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1949
        public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1950
           { return unmodifiableNavigableMap(nm.tailMap(fromKey, inclusive)); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  1951
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    // Synch Wrappers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * Returns a synchronized (thread-safe) collection backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * collection.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * <strong>all</strong> access to the backing collection is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * through the returned collection.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * It is imperative that the user manually synchronize on the returned
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1962
     * collection when traversing it via {@link Iterator}, {@link Spliterator}
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  1963
     * or {@link Stream}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     *  Collection c = Collections.synchronizedCollection(myCollection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     *     ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  1967
     *  synchronized (c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     *      Iterator i = c.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *         foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     *
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1975
     * <p>The returned collection does <i>not</i> pass the {@code hashCode}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1976
     * and {@code equals} operations through to the backing collection, but
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  1977
     * relies on {@code Object}'s equals and hashCode methods.  This is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * necessary to preserve the contracts of these operations in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * that the backing collection is a set or a list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * The returned collection will be serializable if the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  1984
     * @param  <T> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * @param  c the collection to be "wrapped" in a synchronized collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * @return a synchronized view of the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1989
        return new SynchronizedCollection<>(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  1993
        return new SynchronizedCollection<>(c, mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    static class SynchronizedCollection<E> implements Collection<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        private static final long serialVersionUID = 3053995032091335093L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        final Collection<E> c;  // Backing Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        final Object mutex;     // Object on which to synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        SynchronizedCollection(Collection<E> c) {
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2006
            this.c = Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            mutex = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        }
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2009
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        SynchronizedCollection(Collection<E> c, Object mutex) {
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2011
            this.c = Objects.requireNonNull(c);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2012
            this.mutex = Objects.requireNonNull(mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        public int size() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2016
            synchronized (mutex) {return c.size();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        public boolean isEmpty() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2019
            synchronized (mutex) {return c.isEmpty();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        public boolean contains(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2022
            synchronized (mutex) {return c.contains(o);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        public Object[] toArray() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2025
            synchronized (mutex) {return c.toArray();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        public <T> T[] toArray(T[] a) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2028
            synchronized (mutex) {return c.toArray(a);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            return c.iterator(); // Must be manually synched by user!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        public boolean add(E e) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2036
            synchronized (mutex) {return c.add(e);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        public boolean remove(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2039
            synchronized (mutex) {return c.remove(o);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        public boolean containsAll(Collection<?> coll) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2043
            synchronized (mutex) {return c.containsAll(coll);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        public boolean addAll(Collection<? extends E> coll) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2046
            synchronized (mutex) {return c.addAll(coll);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        public boolean removeAll(Collection<?> coll) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2049
            synchronized (mutex) {return c.removeAll(coll);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        public boolean retainAll(Collection<?> coll) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2052
            synchronized (mutex) {return c.retainAll(coll);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        public void clear() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2055
            synchronized (mutex) {c.clear();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        public String toString() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2058
            synchronized (mutex) {return c.toString();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2060
        // Override default methods in Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2061
        @Override
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2062
        public void forEach(Consumer<? super E> consumer) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2063
            synchronized (mutex) {c.forEach(consumer);}
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2064
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2065
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2066
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2067
            synchronized (mutex) {return c.removeIf(filter);}
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2068
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2069
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2070
        public Spliterator<E> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2071
            return c.spliterator(); // Must be manually synched by user!
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2072
        }
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2073
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2074
        public Stream<E> stream() {
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2075
            return c.stream(); // Must be manually synched by user!
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2076
        }
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2077
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2078
        public Stream<E> parallelStream() {
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2079
            return c.parallelStream(); // Must be manually synched by user!
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  2080
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2081
        private void writeObject(ObjectOutputStream s) throws IOException {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2082
            synchronized (mutex) {s.defaultWriteObject();}
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  2083
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * Returns a synchronized (thread-safe) set backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * set.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * <strong>all</strong> access to the backing set is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * through the returned set.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * It is imperative that the user manually synchronize on the returned
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2093
     * collection when traversing it via {@link Iterator}, {@link Spliterator}
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2094
     * or {@link Stream}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     *  Set s = Collections.synchronizedSet(new HashSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     *      ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2098
     *  synchronized (s) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     *      Iterator i = s.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * <p>The returned set will be serializable if the specified set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2109
     * @param  <T> the class of the objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     * @param  s the set to be "wrapped" in a synchronized set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * @return a synchronized view of the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
    public static <T> Set<T> synchronizedSet(Set<T> s) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2114
        return new SynchronizedSet<>(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2118
        return new SynchronizedSet<>(s, mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    static class SynchronizedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
          extends SynchronizedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
          implements Set<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        private static final long serialVersionUID = 487447009682186044L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        SynchronizedSet(Set<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        SynchronizedSet(Set<E> s, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            super(s, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        public boolean equals(Object o) {
11987
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  2137
            if (this == o)
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  2138
                return true;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2139
            synchronized (mutex) {return c.equals(o);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        public int hashCode() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2142
            synchronized (mutex) {return c.hashCode();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * Returns a synchronized (thread-safe) sorted set backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * sorted set.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * <strong>all</strong> access to the backing sorted set is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * through the returned sorted set (or its views).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * It is imperative that the user manually synchronize on the returned
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2153
     * sorted set when traversing it or any of its {@code subSet},
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2154
     * {@code headSet}, or {@code tailSet} views via {@link Iterator},
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2155
     * {@link Spliterator} or {@link Stream}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     *      ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2159
     *  synchronized (s) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     *      Iterator i = s.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * or:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     *  SortedSet s2 = s.headSet(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     *      ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2170
     *  synchronized (s) {  // Note: s, not s2!!!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     *      Iterator i = s2.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * <p>The returned sorted set will be serializable if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     * sorted set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2181
     * @param  <T> the class of the objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * @param  s the sorted set to be "wrapped" in a synchronized sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * @return a synchronized view of the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
    public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2186
        return new SynchronizedSortedSet<>(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    static class SynchronizedSortedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        extends SynchronizedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        implements SortedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        private static final long serialVersionUID = 8695801310862127406L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2198
        private final SortedSet<E> ss;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        SynchronizedSortedSet(SortedSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            ss = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        SynchronizedSortedSet(SortedSet<E> s, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            super(s, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            ss = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
        public Comparator<? super E> comparator() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2210
            synchronized (mutex) {return ss.comparator();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        public SortedSet<E> subSet(E fromElement, E toElement) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2214
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2215
                return new SynchronizedSortedSet<>(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                    ss.subSet(fromElement, toElement), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        public SortedSet<E> headSet(E toElement) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2220
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2221
                return new SynchronizedSortedSet<>(ss.headSet(toElement), mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        public SortedSet<E> tailSet(E fromElement) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2225
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2226
               return new SynchronizedSortedSet<>(ss.tailSet(fromElement),mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        public E first() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2231
            synchronized (mutex) {return ss.first();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        public E last() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2234
            synchronized (mutex) {return ss.last();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2239
     * Returns a synchronized (thread-safe) navigable set backed by the
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2240
     * specified navigable set.  In order to guarantee serial access, it is
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2241
     * critical that <strong>all</strong> access to the backing navigable set is
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2242
     * accomplished through the returned navigable set (or its views).<p>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2243
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2244
     * It is imperative that the user manually synchronize on the returned
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2245
     * navigable set when traversing it, or any of its {@code subSet},
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2246
     * {@code headSet}, or {@code tailSet} views, via {@link Iterator},
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2247
     * {@link Spliterator} or {@link Stream}:
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2248
     * <pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2249
     *  NavigableSet s = Collections.synchronizedNavigableSet(new TreeSet());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2250
     *      ...
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2251
     *  synchronized (s) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2252
     *      Iterator i = s.iterator(); // Must be in the synchronized block
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2253
     *      while (i.hasNext())
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2254
     *          foo(i.next());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2255
     *  }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2256
     * </pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2257
     * or:
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2258
     * <pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2259
     *  NavigableSet s = Collections.synchronizedNavigableSet(new TreeSet());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2260
     *  NavigableSet s2 = s.headSet(foo, true);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2261
     *      ...
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2262
     *  synchronized (s) {  // Note: s, not s2!!!
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2263
     *      Iterator i = s2.iterator(); // Must be in the synchronized block
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2264
     *      while (i.hasNext())
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2265
     *          foo(i.next());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2266
     *  }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2267
     * </pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2268
     * Failure to follow this advice may result in non-deterministic behavior.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2269
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2270
     * <p>The returned navigable set will be serializable if the specified
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2271
     * navigable set is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2272
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2273
     * @param  <T> the class of the objects in the set
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2274
     * @param  s the navigable set to be "wrapped" in a synchronized navigable
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2275
     * set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2276
     * @return a synchronized view of the specified navigable set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2277
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2278
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2279
    public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2280
        return new SynchronizedNavigableSet<>(s);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2281
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2282
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2283
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2284
     * @serial include
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2285
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2286
    static class SynchronizedNavigableSet<E>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2287
        extends SynchronizedSortedSet<E>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2288
        implements NavigableSet<E>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2289
    {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2290
        private static final long serialVersionUID = -5505529816273629798L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2291
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2292
        private final NavigableSet<E> ns;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2293
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2294
        SynchronizedNavigableSet(NavigableSet<E> s) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2295
            super(s);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2296
            ns = s;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2297
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2298
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2299
        SynchronizedNavigableSet(NavigableSet<E> s, Object mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2300
            super(s, mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2301
            ns = s;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2302
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2303
        public E lower(E e)      { synchronized (mutex) {return ns.lower(e);} }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2304
        public E floor(E e)      { synchronized (mutex) {return ns.floor(e);} }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2305
        public E ceiling(E e)  { synchronized (mutex) {return ns.ceiling(e);} }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2306
        public E higher(E e)    { synchronized (mutex) {return ns.higher(e);} }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2307
        public E pollFirst()  { synchronized (mutex) {return ns.pollFirst();} }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2308
        public E pollLast()    { synchronized (mutex) {return ns.pollLast();} }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2309
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2310
        public NavigableSet<E> descendingSet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2311
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2312
                return new SynchronizedNavigableSet<>(ns.descendingSet(), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2313
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2314
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2315
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2316
        public Iterator<E> descendingIterator()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2317
                 { synchronized (mutex) { return descendingSet().iterator(); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2318
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2319
        public NavigableSet<E> subSet(E fromElement, E toElement) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2320
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2321
                return new SynchronizedNavigableSet<>(ns.subSet(fromElement, true, toElement, false), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2322
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2323
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2324
        public NavigableSet<E> headSet(E toElement) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2325
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2326
                return new SynchronizedNavigableSet<>(ns.headSet(toElement, false), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2327
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2328
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2329
        public NavigableSet<E> tailSet(E fromElement) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2330
            synchronized (mutex) {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2331
                return new SynchronizedNavigableSet<>(ns.tailSet(fromElement, true), mutex);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2332
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2333
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2334
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2335
        public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2336
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2337
                return new SynchronizedNavigableSet<>(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2338
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2339
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2340
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2341
        public NavigableSet<E> headSet(E toElement, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2342
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2343
                return new SynchronizedNavigableSet<>(ns.headSet(toElement, inclusive), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2344
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2345
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2346
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2347
        public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2348
            synchronized (mutex) {
25748
8914cd976d19 8048209: Collections.synchronizedNavigableSet().tailSet(Object,boolean) synchronizes on wrong object
mduigou
parents: 25177
diff changeset
  2349
                return new SynchronizedNavigableSet<>(ns.tailSet(fromElement, inclusive), mutex);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2350
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2351
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2352
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2353
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2354
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * Returns a synchronized (thread-safe) list backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * list.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * <strong>all</strong> access to the backing list is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * through the returned list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * It is imperative that the user manually synchronize on the returned
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2361
     * list when traversing it via {@link Iterator}, {@link Spliterator}
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2362
     * or {@link Stream}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     *  List list = Collections.synchronizedList(new ArrayList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     *      ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2366
     *  synchronized (list) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     *      Iterator i = list.iterator(); // Must be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * <p>The returned list will be serializable if the specified list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2377
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * @param  list the list to be "wrapped" in a synchronized list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * @return a synchronized view of the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
    public static <T> List<T> synchronizedList(List<T> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        return (list instanceof RandomAccess ?
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2383
                new SynchronizedRandomAccessList<>(list) :
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2384
                new SynchronizedList<>(list));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    static <T> List<T> synchronizedList(List<T> list, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        return (list instanceof RandomAccess ?
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2389
                new SynchronizedRandomAccessList<>(list, mutex) :
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2390
                new SynchronizedList<>(list, mutex));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    static class SynchronizedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        extends SynchronizedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        implements List<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        private static final long serialVersionUID = -7754090372962971524L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        final List<E> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        SynchronizedList(List<E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        SynchronizedList(List<E> list, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            super(list, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        public boolean equals(Object o) {
11987
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  2413
            if (this == o)
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  2414
                return true;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2415
            synchronized (mutex) {return list.equals(o);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        public int hashCode() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2418
            synchronized (mutex) {return list.hashCode();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        public E get(int index) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2422
            synchronized (mutex) {return list.get(index);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        public E set(int index, E element) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2425
            synchronized (mutex) {return list.set(index, element);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        public void add(int index, E element) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2428
            synchronized (mutex) {list.add(index, element);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        public E remove(int index) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2431
            synchronized (mutex) {return list.remove(index);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        public int indexOf(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2435
            synchronized (mutex) {return list.indexOf(o);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        public int lastIndexOf(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2438
            synchronized (mutex) {return list.lastIndexOf(o);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        public boolean addAll(int index, Collection<? extends E> c) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2442
            synchronized (mutex) {return list.addAll(index, c);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            return list.listIterator(); // Must be manually synched by user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        public ListIterator<E> listIterator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            return list.listIterator(index); // Must be manually synched by user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
        public List<E> subList(int fromIndex, int toIndex) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2454
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2455
                return new SynchronizedList<>(list.subList(fromIndex, toIndex),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                                            mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2460
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2461
        public void replaceAll(UnaryOperator<E> operator) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2462
            synchronized (mutex) {list.replaceAll(operator);}
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2463
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2464
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2465
        public void sort(Comparator<? super E> c) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2466
            synchronized (mutex) {list.sort(c);}
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2467
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  2468
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
         * SynchronizedRandomAccessList instances are serialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
         * SynchronizedList instances to allow them to be deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
         * in pre-1.4 JREs (which do not have SynchronizedRandomAccessList).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
         * This method inverts the transformation.  As a beneficial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
         * side-effect, it also grafts the RandomAccess marker onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
         * SynchronizedList instances that were serialized in pre-1.4 JREs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
         * Note: Unfortunately, SynchronizedRandomAccessList instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
         * serialized in 1.4.1 and deserialized in 1.4 will become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
         * SynchronizedList instances, as this method was missing in 1.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
            return (list instanceof RandomAccess
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2483
                    ? new SynchronizedRandomAccessList<>(list)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                    : this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
    static class SynchronizedRandomAccessList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        extends SynchronizedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        implements RandomAccess {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        SynchronizedRandomAccessList(List<E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        SynchronizedRandomAccessList(List<E> list, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            super(list, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        public List<E> subList(int fromIndex, int toIndex) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2504
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2505
                return new SynchronizedRandomAccessList<>(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                    list.subList(fromIndex, toIndex), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
        private static final long serialVersionUID = 1530674583602358482L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
         * Allows instances to be deserialized in pre-1.4 JREs (which do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
         * not have SynchronizedRandomAccessList).  SynchronizedList has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
         * a readResolve method that inverts this transformation upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
         * deserialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
        private Object writeReplace() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2519
            return new SynchronizedList<>(list);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * Returns a synchronized (thread-safe) map backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * map.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     * <strong>all</strong> access to the backing map is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * through the returned map.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     * It is imperative that the user manually synchronize on the returned
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2530
     * map when traversing any of its collection views via {@link Iterator},
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2531
     * {@link Spliterator} or {@link Stream}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     *  Map m = Collections.synchronizedMap(new HashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     *  Set s = m.keySet();  // Needn't be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     *      ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2537
     *  synchronized (m) {  // Synchronizing on m, not s!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
     *      Iterator i = s.iterator(); // Must be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
     * <p>The returned map will be serializable if the specified map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2548
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2549
     * @param <V> the class of the map values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
     * @param  m the map to be "wrapped" in a synchronized map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
     * @return a synchronized view of the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
    public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2554
        return new SynchronizedMap<>(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
    private static class SynchronizedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        implements Map<K,V>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        private static final long serialVersionUID = 1978198479659022715L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        private final Map<K,V> m;     // Backing Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        final Object      mutex;        // Object on which to synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
        SynchronizedMap(Map<K,V> m) {
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2568
            this.m = Objects.requireNonNull(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            mutex = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        SynchronizedMap(Map<K,V> m, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
            this.mutex = mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        public int size() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2578
            synchronized (mutex) {return m.size();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        public boolean isEmpty() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2581
            synchronized (mutex) {return m.isEmpty();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        public boolean containsKey(Object key) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2584
            synchronized (mutex) {return m.containsKey(key);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        public boolean containsValue(Object value) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2587
            synchronized (mutex) {return m.containsValue(value);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        public V get(Object key) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2590
            synchronized (mutex) {return m.get(key);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        public V put(K key, V value) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2594
            synchronized (mutex) {return m.put(key, value);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        public V remove(Object key) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2597
            synchronized (mutex) {return m.remove(key);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
        public void putAll(Map<? extends K, ? extends V> map) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2600
            synchronized (mutex) {m.putAll(map);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        public void clear() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2603
            synchronized (mutex) {m.clear();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  2606
        private transient Set<K> keySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  2607
        private transient Set<Map.Entry<K,V>> entrySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  2608
        private transient Collection<V> values;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        public Set<K> keySet() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2611
            synchronized (mutex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                if (keySet==null)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2613
                    keySet = new SynchronizedSet<>(m.keySet(), mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
        public Set<Map.Entry<K,V>> entrySet() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2619
            synchronized (mutex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                if (entrySet==null)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2621
                    entrySet = new SynchronizedSet<>(m.entrySet(), mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        public Collection<V> values() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2627
            synchronized (mutex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                if (values==null)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2629
                    values = new SynchronizedCollection<>(m.values(), mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
                return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        public boolean equals(Object o) {
11987
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  2635
            if (this == o)
a6dde4427e9d 7144488: Infinite recursion for some equals tests in Collections
coffeys
parents: 10896
diff changeset
  2636
                return true;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2637
            synchronized (mutex) {return m.equals(o);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
        public int hashCode() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2640
            synchronized (mutex) {return m.hashCode();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
        public String toString() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2643
            synchronized (mutex) {return m.toString();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
        }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2645
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2646
        // Override default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2647
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2648
        public V getOrDefault(Object k, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2649
            synchronized (mutex) {return m.getOrDefault(k, defaultValue);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2650
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2651
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2652
        public void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2653
            synchronized (mutex) {m.forEach(action);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2654
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2655
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2656
        public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2657
            synchronized (mutex) {m.replaceAll(function);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2658
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2659
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2660
        public V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2661
            synchronized (mutex) {return m.putIfAbsent(key, value);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2662
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2663
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2664
        public boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2665
            synchronized (mutex) {return m.remove(key, value);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2666
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2667
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2668
        public boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2669
            synchronized (mutex) {return m.replace(key, oldValue, newValue);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2670
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2671
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2672
        public V replace(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2673
            synchronized (mutex) {return m.replace(key, value);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2674
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2675
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2676
        public V computeIfAbsent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2677
                Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2678
            synchronized (mutex) {return m.computeIfAbsent(key, mappingFunction);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2679
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2680
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2681
        public V computeIfPresent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2682
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2683
            synchronized (mutex) {return m.computeIfPresent(key, remappingFunction);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2684
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2685
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2686
        public V compute(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2687
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2688
            synchronized (mutex) {return m.compute(key, remappingFunction);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2689
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2690
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2691
        public V merge(K key, V value,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2692
                BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2693
            synchronized (mutex) {return m.merge(key, value, remappingFunction);}
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2694
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  2695
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
        private void writeObject(ObjectOutputStream s) throws IOException {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2697
            synchronized (mutex) {s.defaultWriteObject();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
     * Returns a synchronized (thread-safe) sorted map backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
     * sorted map.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
     * <strong>all</strong> access to the backing sorted map is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
     * through the returned sorted map (or its views).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * It is imperative that the user manually synchronize on the returned
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2708
     * sorted map when traversing any of its collection views, or the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  2709
     * collections views of any of its {@code subMap}, {@code headMap} or
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2710
     * {@code tailMap} views, via {@link Iterator}, {@link Spliterator} or
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2711
     * {@link Stream}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     *  SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     *  Set s = m.keySet();  // Needn't be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     *      ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2717
     *  synchronized (m) {  // Synchronizing on m, not s!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
     *      Iterator i = s.iterator(); // Must be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
     * or:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
     *  SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
     *  SortedMap m2 = m.subMap(foo, bar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
     *  Set s2 = m2.keySet();  // Needn't be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
     *      ...
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2730
     *  synchronized (m) {  // Synchronizing on m, not m2 or s2!
36436
c538d4a57e10 8043329: Wrong variable used in java.util.Collections javadoc code
rriggs
parents: 34714
diff changeset
  2731
     *      Iterator i = s2.iterator(); // Must be in synchronized block
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * <p>The returned sorted map will be serializable if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
     * sorted map is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2741
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2742
     * @param <V> the class of the map values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     * @param  m the sorted map to be "wrapped" in a synchronized sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
     * @return a synchronized view of the specified sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
    public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2747
        return new SynchronizedSortedMap<>(m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
    static class SynchronizedSortedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
        extends SynchronizedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        implements SortedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        private static final long serialVersionUID = -8798146769416483793L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        private final SortedMap<K,V> sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
        SynchronizedSortedMap(SortedMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
            super(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
            sm = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
        SynchronizedSortedMap(SortedMap<K,V> m, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
            super(m, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
            sm = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
        public Comparator<? super K> comparator() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2771
            synchronized (mutex) {return sm.comparator();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
        public SortedMap<K,V> subMap(K fromKey, K toKey) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2775
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2776
                return new SynchronizedSortedMap<>(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                    sm.subMap(fromKey, toKey), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        public SortedMap<K,V> headMap(K toKey) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2781
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2782
                return new SynchronizedSortedMap<>(sm.headMap(toKey), mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
        public SortedMap<K,V> tailMap(K fromKey) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2786
            synchronized (mutex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  2787
               return new SynchronizedSortedMap<>(sm.tailMap(fromKey),mutex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        public K firstKey() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2792
            synchronized (mutex) {return sm.firstKey();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        public K lastKey() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  2795
            synchronized (mutex) {return sm.lastKey();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2799
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2800
     * Returns a synchronized (thread-safe) navigable map backed by the
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2801
     * specified navigable map.  In order to guarantee serial access, it is
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2802
     * critical that <strong>all</strong> access to the backing navigable map is
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2803
     * accomplished through the returned navigable map (or its views).<p>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2804
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2805
     * It is imperative that the user manually synchronize on the returned
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2806
     * navigable map when traversing any of its collection views, or the
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2807
     * collections views of any of its {@code subMap}, {@code headMap} or
33644
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2808
     * {@code tailMap} views, via {@link Iterator}, {@link Spliterator} or
c60dc808ff5a 8141630: Specification of Collections.synchronized* need to state traversal constraints
psandoz
parents: 32994
diff changeset
  2809
     * {@link Stream}:
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2810
     * <pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2811
     *  NavigableMap m = Collections.synchronizedNavigableMap(new TreeMap());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2812
     *      ...
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2813
     *  Set s = m.keySet();  // Needn't be in synchronized block
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2814
     *      ...
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2815
     *  synchronized (m) {  // Synchronizing on m, not s!
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2816
     *      Iterator i = s.iterator(); // Must be in synchronized block
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2817
     *      while (i.hasNext())
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2818
     *          foo(i.next());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2819
     *  }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2820
     * </pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2821
     * or:
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2822
     * <pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2823
     *  NavigableMap m = Collections.synchronizedNavigableMap(new TreeMap());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2824
     *  NavigableMap m2 = m.subMap(foo, true, bar, false);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2825
     *      ...
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2826
     *  Set s2 = m2.keySet();  // Needn't be in synchronized block
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2827
     *      ...
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2828
     *  synchronized (m) {  // Synchronizing on m, not m2 or s2!
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2829
     *      Iterator i = s.iterator(); // Must be in synchronized block
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2830
     *      while (i.hasNext())
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2831
     *          foo(i.next());
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2832
     *  }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2833
     * </pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2834
     * Failure to follow this advice may result in non-deterministic behavior.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2835
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2836
     * <p>The returned navigable map will be serializable if the specified
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2837
     * navigable map is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2838
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2839
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  2840
     * @param <V> the class of the map values
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2841
     * @param  m the navigable map to be "wrapped" in a synchronized navigable
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2842
     *              map
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2843
     * @return a synchronized view of the specified navigable map.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2844
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2845
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2846
    public static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2847
        return new SynchronizedNavigableMap<>(m);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2848
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2849
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2850
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2851
     * A synchronized NavigableMap.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2852
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2853
     * @serial include
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2854
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2855
    static class SynchronizedNavigableMap<K,V>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2856
        extends SynchronizedSortedMap<K,V>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2857
        implements NavigableMap<K,V>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2858
    {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2859
        private static final long serialVersionUID = 699392247599746807L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2860
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2861
        private final NavigableMap<K,V> nm;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2862
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2863
        SynchronizedNavigableMap(NavigableMap<K,V> m) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2864
            super(m);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2865
            nm = m;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2866
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2867
        SynchronizedNavigableMap(NavigableMap<K,V> m, Object mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2868
            super(m, mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2869
            nm = m;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2870
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2871
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2872
        public Entry<K, V> lowerEntry(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2873
                        { synchronized (mutex) { return nm.lowerEntry(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2874
        public K lowerKey(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2875
                          { synchronized (mutex) { return nm.lowerKey(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2876
        public Entry<K, V> floorEntry(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2877
                        { synchronized (mutex) { return nm.floorEntry(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2878
        public K floorKey(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2879
                          { synchronized (mutex) { return nm.floorKey(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2880
        public Entry<K, V> ceilingEntry(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2881
                      { synchronized (mutex) { return nm.ceilingEntry(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2882
        public K ceilingKey(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2883
                        { synchronized (mutex) { return nm.ceilingKey(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2884
        public Entry<K, V> higherEntry(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2885
                       { synchronized (mutex) { return nm.higherEntry(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2886
        public K higherKey(K key)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2887
                         { synchronized (mutex) { return nm.higherKey(key); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2888
        public Entry<K, V> firstEntry()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2889
                           { synchronized (mutex) { return nm.firstEntry(); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2890
        public Entry<K, V> lastEntry()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2891
                            { synchronized (mutex) { return nm.lastEntry(); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2892
        public Entry<K, V> pollFirstEntry()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2893
                       { synchronized (mutex) { return nm.pollFirstEntry(); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2894
        public Entry<K, V> pollLastEntry()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2895
                        { synchronized (mutex) { return nm.pollLastEntry(); } }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2896
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2897
        public NavigableMap<K, V> descendingMap() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2898
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2899
                return
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2900
                    new SynchronizedNavigableMap<>(nm.descendingMap(), mutex);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2901
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2902
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2903
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2904
        public NavigableSet<K> keySet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2905
            return navigableKeySet();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2906
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2907
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2908
        public NavigableSet<K> navigableKeySet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2909
            synchronized (mutex) {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2910
                return new SynchronizedNavigableSet<>(nm.navigableKeySet(), mutex);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2911
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2912
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2913
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2914
        public NavigableSet<K> descendingKeySet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2915
            synchronized (mutex) {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2916
                return new SynchronizedNavigableSet<>(nm.descendingKeySet(), mutex);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2917
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2918
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2919
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2920
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2921
        public SortedMap<K,V> subMap(K fromKey, K toKey) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2922
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2923
                return new SynchronizedNavigableMap<>(
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2924
                    nm.subMap(fromKey, true, toKey, false), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2925
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2926
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2927
        public SortedMap<K,V> headMap(K toKey) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2928
            synchronized (mutex) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2929
                return new SynchronizedNavigableMap<>(nm.headMap(toKey, false), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2930
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2931
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2932
        public SortedMap<K,V> tailMap(K fromKey) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2933
            synchronized (mutex) {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2934
        return new SynchronizedNavigableMap<>(nm.tailMap(fromKey, true),mutex);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2935
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2936
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2937
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2938
        public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2939
            synchronized (mutex) {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2940
                return new SynchronizedNavigableMap<>(
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2941
                    nm.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2942
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2943
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2944
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2945
        public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2946
            synchronized (mutex) {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2947
                return new SynchronizedNavigableMap<>(
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2948
                        nm.headMap(toKey, inclusive), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2949
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2950
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2951
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2952
        public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2953
            synchronized (mutex) {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  2954
                return new SynchronizedNavigableMap<>(
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2955
                    nm.tailMap(fromKey, inclusive), mutex);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2956
            }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2957
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2958
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2959
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
    // Dynamically typesafe collection wrappers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
     * Returns a dynamically typesafe view of the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
     * Any attempt to insert an element of the wrong type will result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
     * immediate {@link ClassCastException}.  Assuming a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     * contains no incorrectly typed elements prior to the time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     * dynamically typesafe view is generated, and that all subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
     * access to the collection takes place through the view, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
     * <i>guaranteed</i> that the collection cannot contain an incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
     * typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
     * <p>The generics mechanism in the language provides compile-time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
     * (static) type checking, but it is possible to defeat this mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
     * with unchecked casts.  Usually this is not a problem, as the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
     * issues warnings on all such unchecked operations.  There are, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
     * times when static type checking alone is not sufficient.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
     * suppose a collection is passed to a third-party library and it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
     * imperative that the library code not corrupt the collection by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
     * inserting an element of the wrong type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
     * <p>Another use of dynamically typesafe views is debugging.  Suppose a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
     * program fails with a {@code ClassCastException}, indicating that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
     * incorrectly typed element was put into a parameterized collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
     * Unfortunately, the exception can occur at any time after the erroneous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
     * element is inserted, so it typically provides little or no information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
     * as to the real source of the problem.  If the problem is reproducible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
     * one can quickly determine its source by temporarily modifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
     * program to wrap the collection with a dynamically typesafe view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
     * For example, this declaration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
     *  <pre> {@code
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2991
     *     Collection<String> c = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
     * may be replaced temporarily by this one:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
     *     Collection<String> c = Collections.checkedCollection(
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  2996
     *         new HashSet<>(), String.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * Running the program again will cause it to fail at the point where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * an incorrectly typed element is inserted into the collection, clearly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     * identifying the source of the problem.  Once the problem is fixed, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
     * modified declaration may be reverted back to the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
     * <p>The returned collection does <i>not</i> pass the hashCode and equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
     * operations through to the backing collection, but relies on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
     * {@code Object}'s {@code equals} and {@code hashCode} methods.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
     * is necessary to preserve the contracts of these operations in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
     * that the backing collection is a set or a list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
     * <p>The returned collection will be serializable if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
     * collection is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
     * type, the returned collection permits insertion of null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
     * whenever the backing collection does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3016
     * @param <E> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
     * @param c the collection for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
     * @param type the type of element that {@code c} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
     * @return a dynamically typesafe view of the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
    public static <E> Collection<E> checkedCollection(Collection<E> c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                                                      Class<E> type) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3025
        return new CheckedCollection<>(c, type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
    static <T> T[] zeroLengthArray(Class<T> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        return (T[]) Array.newInstance(type, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
    static class CheckedCollection<E> implements Collection<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
        private static final long serialVersionUID = 1578914078182001775L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        final Collection<E> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        final Class<E> type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
25171
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3042
        @SuppressWarnings("unchecked")
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3043
        E typeCheck(Object o) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
            if (o != null && !type.isInstance(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                throw new ClassCastException(badElementMsg(o));
25171
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3046
            return (E) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
        private String badElementMsg(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
            return "Attempt to insert " + o.getClass() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
                " element into collection with element type " + type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        CheckedCollection(Collection<E> c, Class<E> type) {
25171
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3055
            this.c = Objects.requireNonNull(c, "c");
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3056
            this.type = Objects.requireNonNull(type, "type");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
        public int size()                 { return c.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
        public boolean isEmpty()          { return c.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
        public boolean contains(Object o) { return c.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
        public Object[] toArray()         { return c.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
        public <T> T[] toArray(T[] a)     { return c.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
        public String toString()          { return c.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
        public boolean remove(Object o)   { return c.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
        public void clear()               {        c.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
        public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
            return c.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
        public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
            return c.removeAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
        public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            return c.retainAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        public Iterator<E> iterator() {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3079
            // JDK-6363904 - unwrapped iterator could be typecast to
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3080
            // ListIterator with unsafe set()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
            final Iterator<E> it = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
            return new Iterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
                public boolean hasNext() { return it.hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
                public E next()          { return it.next(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
                public void remove()     {        it.remove(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 25171
diff changeset
  3088
        public boolean add(E e)          { return c.add(typeCheck(e)); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  3090
        private E[] zeroLengthElementArray; // Lazily initialized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        private E[] zeroLengthElementArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
            return zeroLengthElementArray != null ? zeroLengthElementArray :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                (zeroLengthElementArray = zeroLengthArray(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        Collection<E> checkedCopyOf(Collection<? extends E> coll) {
25171
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3099
            Object[] a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                E[] z = zeroLengthElementArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                a = coll.toArray(z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
                // Defend against coll violating the toArray contract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                if (a.getClass() != z.getClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
                    a = Arrays.copyOf(a, a.length, z.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
            } catch (ArrayStoreException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                // To get better and consistent diagnostics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
                // we call typeCheck explicitly on each element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                // We call clone() to defend against coll retaining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                // reference to the returned array and storing a bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
                // element into it after it has been type checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                a = coll.toArray().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
                for (Object o : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
                    typeCheck(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
            // A slight abuse of the type system, but safe here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
            return (Collection<E>) Arrays.asList(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
        public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
            // Doing things this way insulates us from concurrent changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
            // in the contents of coll and provides all-or-nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
            // semantics (which we wouldn't get if we type-checked each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
            // element as we added it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
            return c.addAll(checkedCopyOf(coll));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3127
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3128
        // Override default methods in Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3129
        @Override
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3130
        public void forEach(Consumer<? super E> action) {c.forEach(action);}
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3131
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3132
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3133
            return c.removeIf(filter);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3134
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3135
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3136
        public Spliterator<E> spliterator() {return c.spliterator();}
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  3137
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  3138
        public Stream<E> stream()           {return c.stream();}
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  3139
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  3140
        public Stream<E> parallelStream()   {return c.parallelStream();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
    /**
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3144
     * Returns a dynamically typesafe view of the specified queue.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3145
     * Any attempt to insert an element of the wrong type will result in
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3146
     * an immediate {@link ClassCastException}.  Assuming a queue contains
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3147
     * no incorrectly typed elements prior to the time a dynamically typesafe
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3148
     * view is generated, and that all subsequent access to the queue
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3149
     * takes place through the view, it is <i>guaranteed</i> that the
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3150
     * queue cannot contain an incorrectly typed element.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3151
     *
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3152
     * <p>A discussion of the use of dynamically typesafe views may be
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3153
     * found in the documentation for the {@link #checkedCollection
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3154
     * checkedCollection} method.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3155
     *
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3156
     * <p>The returned queue will be serializable if the specified queue
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3157
     * is serializable.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3158
     *
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3159
     * <p>Since {@code null} is considered to be a value of any reference
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3160
     * type, the returned queue permits insertion of {@code null} elements
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3161
     * whenever the backing queue does.
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3162
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3163
     * @param <E> the class of the objects in the queue
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3164
     * @param queue the queue for which a dynamically typesafe view is to be
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3165
     *             returned
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3166
     * @param type the type of element that {@code queue} is permitted to hold
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3167
     * @return a dynamically typesafe view of the specified queue
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3168
     * @since 1.8
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3169
     */
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3170
    public static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type) {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3171
        return new CheckedQueue<>(queue, type);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3172
    }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3173
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3174
    /**
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3175
     * @serial include
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3176
     */
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3177
    static class CheckedQueue<E>
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3178
        extends CheckedCollection<E>
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3179
        implements Queue<E>, Serializable
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3180
    {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3181
        private static final long serialVersionUID = 1433151992604707767L;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3182
        final Queue<E> queue;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3183
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3184
        CheckedQueue(Queue<E> queue, Class<E> elementType) {
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3185
            super(queue, elementType);
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3186
            this.queue = queue;
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3187
        }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3188
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3189
        public E element()              {return queue.element();}
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3190
        public boolean equals(Object o) {return o == this || c.equals(o);}
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3191
        public int hashCode()           {return c.hashCode();}
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3192
        public E peek()                 {return queue.peek();}
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3193
        public E poll()                 {return queue.poll();}
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3194
        public E remove()               {return queue.remove();}
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 25171
diff changeset
  3195
        public boolean offer(E e)       {return queue.offer(typeCheck(e));}
10790
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3196
    }
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3197
331fdf9a8824 5029031: Add Collections.checkedQueue()
mduigou
parents: 9503
diff changeset
  3198
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
     * Returns a dynamically typesafe view of the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
     * Any attempt to insert an element of the wrong type will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
     * an immediate {@link ClassCastException}.  Assuming a set contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
     * no incorrectly typed elements prior to the time a dynamically typesafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
     * view is generated, and that all subsequent access to the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
     * takes place through the view, it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
     * set cannot contain an incorrectly typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
     * <p>The returned set will be serializable if the specified set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
     * type, the returned set permits insertion of null elements whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
     * the backing set does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3218
     * @param <E> the class of the objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
     * @param s the set for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
     * @param type the type of element that {@code s} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
     * @return a dynamically typesafe view of the specified set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
    public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3226
        return new CheckedSet<>(s, type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
    static class CheckedSet<E> extends CheckedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                                 implements Set<E>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
        private static final long serialVersionUID = 4694047833775013803L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
        CheckedSet(Set<E> s, Class<E> elementType) { super(s, elementType); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
        public boolean equals(Object o) { return o == this || c.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
        public int hashCode()           { return c.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * Returns a dynamically typesafe view of the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     * Any attempt to insert an element of the wrong type will result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * immediate {@link ClassCastException}.  Assuming a sorted set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
     * contains no incorrectly typed elements prior to the time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
     * dynamically typesafe view is generated, and that all subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
     * access to the sorted set takes place through the view, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
     * <i>guaranteed</i> that the sorted set cannot contain an incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
     * typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
     * <p>The returned sorted set will be serializable if the specified sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     * set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
     * type, the returned sorted set permits insertion of null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
     * whenever the backing sorted set does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3264
     * @param <E> the class of the objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
     * @param s the sorted set for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
     * @param type the type of element that {@code s} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
     * @return a dynamically typesafe view of the specified sorted set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
    public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
                                                    Class<E> type) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3273
        return new CheckedSortedSet<>(s, type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
    static class CheckedSortedSet<E> extends CheckedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        implements SortedSet<E>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
        private static final long serialVersionUID = 1599911165492914959L;
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3283
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
        private final SortedSet<E> ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
        CheckedSortedSet(SortedSet<E> s, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
            super(s, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
            ss = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
        public Comparator<? super E> comparator() { return ss.comparator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
        public E first()                   { return ss.first(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
        public E last()                    { return ss.last(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
        public SortedSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
            return checkedSortedSet(ss.subSet(fromElement,toElement), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
        public SortedSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
            return checkedSortedSet(ss.headSet(toElement), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
        public SortedSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
            return checkedSortedSet(ss.tailSet(fromElement), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3306
/**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3307
     * Returns a dynamically typesafe view of the specified navigable set.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3308
     * Any attempt to insert an element of the wrong type will result in an
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3309
     * immediate {@link ClassCastException}.  Assuming a navigable set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3310
     * contains no incorrectly typed elements prior to the time a
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3311
     * dynamically typesafe view is generated, and that all subsequent
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3312
     * access to the navigable set takes place through the view, it is
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3313
     * <em>guaranteed</em> that the navigable set cannot contain an incorrectly
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3314
     * typed element.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3315
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3316
     * <p>A discussion of the use of dynamically typesafe views may be
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3317
     * found in the documentation for the {@link #checkedCollection
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3318
     * checkedCollection} method.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3319
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3320
     * <p>The returned navigable set will be serializable if the specified
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3321
     * navigable set is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3322
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3323
     * <p>Since {@code null} is considered to be a value of any reference
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3324
     * type, the returned navigable set permits insertion of null elements
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3325
     * whenever the backing sorted set does.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3326
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3327
     * @param <E> the class of the objects in the set
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3328
     * @param s the navigable set for which a dynamically typesafe view is to be
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3329
     *          returned
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3330
     * @param type the type of element that {@code s} is permitted to hold
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3331
     * @return a dynamically typesafe view of the specified navigable set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3332
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3333
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3334
    public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s,
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3335
                                                    Class<E> type) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3336
        return new CheckedNavigableSet<>(s, type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3337
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3338
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3339
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3340
     * @serial include
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3341
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3342
    static class CheckedNavigableSet<E> extends CheckedSortedSet<E>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3343
        implements NavigableSet<E>, Serializable
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3344
    {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3345
        private static final long serialVersionUID = -5429120189805438922L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3346
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3347
        private final NavigableSet<E> ns;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3348
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3349
        CheckedNavigableSet(NavigableSet<E> s, Class<E> type) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3350
            super(s, type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3351
            ns = s;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3352
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3353
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3354
        public E lower(E e)                             { return ns.lower(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3355
        public E floor(E e)                             { return ns.floor(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3356
        public E ceiling(E e)                         { return ns.ceiling(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3357
        public E higher(E e)                           { return ns.higher(e); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3358
        public E pollFirst()                         { return ns.pollFirst(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3359
        public E pollLast()                            {return ns.pollLast(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3360
        public NavigableSet<E> descendingSet()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3361
                      { return checkedNavigableSet(ns.descendingSet(), type); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3362
        public Iterator<E> descendingIterator()
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3363
            {return checkedNavigableSet(ns.descendingSet(), type).iterator(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3364
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3365
        public NavigableSet<E> subSet(E fromElement, E toElement) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3366
            return checkedNavigableSet(ns.subSet(fromElement, true, toElement, false), type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3367
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3368
        public NavigableSet<E> headSet(E toElement) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3369
            return checkedNavigableSet(ns.headSet(toElement, false), type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3370
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3371
        public NavigableSet<E> tailSet(E fromElement) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3372
            return checkedNavigableSet(ns.tailSet(fromElement, true), type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3373
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3374
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3375
        public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3376
            return checkedNavigableSet(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3377
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3378
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3379
        public NavigableSet<E> headSet(E toElement, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3380
            return checkedNavigableSet(ns.headSet(toElement, inclusive), type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3381
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3382
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3383
        public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3384
            return checkedNavigableSet(ns.tailSet(fromElement, inclusive), type);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3385
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3386
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3387
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
     * Returns a dynamically typesafe view of the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
     * Any attempt to insert an element of the wrong type will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
     * an immediate {@link ClassCastException}.  Assuming a list contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
     * no incorrectly typed elements prior to the time a dynamically typesafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
     * view is generated, and that all subsequent access to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
     * takes place through the view, it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
     * list cannot contain an incorrectly typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * <p>The returned list will be serializable if the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     * type, the returned list permits insertion of null elements whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
     * the backing list does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3408
     * @param <E> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
     * @param list the list for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
     *             returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
     * @param type the type of element that {@code list} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
     * @return a dynamically typesafe view of the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
    public static <E> List<E> checkedList(List<E> list, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
        return (list instanceof RandomAccess ?
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3417
                new CheckedRandomAccessList<>(list, type) :
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3418
                new CheckedList<>(list, type));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
    static class CheckedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
        extends CheckedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
        implements List<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
        private static final long serialVersionUID = 65247728283967356L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
        final List<E> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
        CheckedList(List<E> list, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
            super(list, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
        public boolean equals(Object o)  { return o == this || list.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
        public int hashCode()            { return list.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
        public E get(int index)          { return list.get(index); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
        public E remove(int index)       { return list.remove(index); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        public int indexOf(Object o)     { return list.indexOf(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
        public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
        public E set(int index, E element) {
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 25171
diff changeset
  3444
            return list.set(index, typeCheck(element));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
        public void add(int index, E element) {
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 25171
diff changeset
  3448
            list.add(index, typeCheck(element));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
        public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
            return list.addAll(index, checkedCopyOf(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
        public ListIterator<E> listIterator()   { return listIterator(0); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
        public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
            final ListIterator<E> i = list.listIterator(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
            return new ListIterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                public boolean hasNext()     { return i.hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                public E next()              { return i.next(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                public boolean hasPrevious() { return i.hasPrevious(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                public E previous()          { return i.previous(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                public int nextIndex()       { return i.nextIndex(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                public int previousIndex()   { return i.previousIndex(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                public void remove()         {        i.remove(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                public void set(E e) {
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 25171
diff changeset
  3469
                    i.set(typeCheck(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                public void add(E e) {
25177
487a5e71f6dd 8048207: Collections.checkedQueue.offer() calls add on wrapped queue
mduigou
parents: 25171
diff changeset
  3473
                    i.add(typeCheck(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3475
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3476
                @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3477
                public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3478
                    i.forEachRemaining(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  3479
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        public List<E> subList(int fromIndex, int toIndex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3484
            return new CheckedList<>(list.subList(fromIndex, toIndex), type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
        }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3486
25171
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3487
        /**
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3488
         * {@inheritDoc}
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3489
         *
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3490
         * @throws ClassCastException if the class of an element returned by the
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3491
         *         operator prevents it from being added to this collection. The
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3492
         *         exception may be thrown after some elements of the list have
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3493
         *         already been replaced.
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3494
         */
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3495
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3496
        public void replaceAll(UnaryOperator<E> operator) {
25796
c6b97b1f9fee 8053938: Collections.checkedList(empty list).replaceAll((UnaryOperator)null) doesn't throw NPE after JDK-8047795
chegar
parents: 25748
diff changeset
  3497
            Objects.requireNonNull(operator);
25171
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3498
            list.replaceAll(e -> typeCheck(operator.apply(e)));
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3499
        }
5dd99a9e443b 8047795: Collections.checkedList checking bypassed by List.replaceAll
mduigou
parents: 23746
diff changeset
  3500
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3501
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3502
        public void sort(Comparator<? super E> c) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3503
            list.sort(c);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  3504
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
    static class CheckedRandomAccessList<E> extends CheckedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                                            implements RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
        private static final long serialVersionUID = 1638200125423088369L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        CheckedRandomAccessList(List<E> list, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
            super(list, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
        public List<E> subList(int fromIndex, int toIndex) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3520
            return new CheckedRandomAccessList<>(
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3521
                    list.subList(fromIndex, toIndex), type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     * Returns a dynamically typesafe view of the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
     * Any attempt to insert a mapping whose key or value have the wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
     * type will result in an immediate {@link ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
     * Similarly, any attempt to modify the value currently associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
     * a key will result in an immediate {@link ClassCastException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
     * whether the modification is attempted directly through the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
     * itself, or through a {@link Map.Entry} instance obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
     * map's {@link Map#entrySet() entry set} view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
     * <p>Assuming a map contains no incorrectly typed keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
     * prior to the time a dynamically typesafe view is generated, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
     * that all subsequent access to the map takes place through the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
     * (or one of its collection views), it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     * map cannot contain an incorrectly typed key or value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     * <p>The returned map will be serializable if the specified map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     * type, the returned map permits insertion of null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
     * whenever the backing map does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3552
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3553
     * @param <V> the class of the map values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
     * @param m the map for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
     * @param keyType the type of key that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
     * @param valueType the type of value that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
     * @return a dynamically typesafe view of the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
    public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                                              Class<K> keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
                                              Class<V> valueType) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3564
        return new CheckedMap<>(m, keyType, valueType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
    private static class CheckedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
        implements Map<K,V>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
        private static final long serialVersionUID = 5742860141034234728L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
        private final Map<K, V> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
        final Class<K> keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
        final Class<V> valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
        private void typeCheck(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
            if (key != null && !keyType.isInstance(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                throw new ClassCastException(badKeyMsg(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
            if (value != null && !valueType.isInstance(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
                throw new ClassCastException(badValueMsg(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3588
        private BiFunction<? super K, ? super V, ? extends V> typeCheck(
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3589
                BiFunction<? super K, ? super V, ? extends V> func) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3590
            Objects.requireNonNull(func);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3591
            return (k, v) -> {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3592
                V newValue = func.apply(k, v);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3593
                typeCheck(k, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3594
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3595
            };
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3596
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3597
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
        private String badKeyMsg(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
            return "Attempt to insert " + key.getClass() +
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3600
                    " key into map with key type " + keyType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
        private String badValueMsg(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
            return "Attempt to insert " + value.getClass() +
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3605
                    " value into map with value type " + valueType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
        CheckedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) {
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3609
            this.m = Objects.requireNonNull(m);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3610
            this.keyType = Objects.requireNonNull(keyType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3611
            this.valueType = Objects.requireNonNull(valueType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
        public int size()                      { return m.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
        public boolean isEmpty()               { return m.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
        public boolean containsKey(Object key) { return m.containsKey(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
        public boolean containsValue(Object v) { return m.containsValue(v); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
        public V get(Object key)               { return m.get(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
        public V remove(Object key)            { return m.remove(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
        public void clear()                    { m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
        public Set<K> keySet()                 { return m.keySet(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
        public Collection<V> values()          { return m.values(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        public boolean equals(Object o)        { return o == this || m.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
        public int hashCode()                  { return m.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        public String toString()               { return m.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
        public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
            typeCheck(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
            return m.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
        public void putAll(Map<? extends K, ? extends V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
            // Satisfy the following goals:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
            // - good diagnostics in case of type mismatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
            // - all-or-nothing semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
            // - protection from malicious t
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
            // - correct behavior if t is a concurrent map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
            Object[] entries = t.entrySet().toArray();
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3640
            List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
            for (Object o : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
                Object k = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
                Object v = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
                typeCheck(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
                checked.add(
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3647
                        new AbstractMap.SimpleImmutableEntry<>((K)k, (V)v));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
            for (Map.Entry<K,V> e : checked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
                m.put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  3653
        private transient Set<Map.Entry<K,V>> entrySet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
            if (entrySet==null)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3657
                entrySet = new CheckedEntrySet<>(m.entrySet(), valueType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
            return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3661
        // Override default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3662
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3663
        public void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3664
            m.forEach(action);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3665
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3666
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3667
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3668
        public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3669
            m.replaceAll(typeCheck(function));
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3670
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3671
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3672
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3673
        public V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3674
            typeCheck(key, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3675
            return m.putIfAbsent(key, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3676
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3677
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3678
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3679
        public boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3680
            return m.remove(key, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3681
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3682
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3683
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3684
        public boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3685
            typeCheck(key, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3686
            return m.replace(key, oldValue, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3687
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3688
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3689
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3690
        public V replace(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3691
            typeCheck(key, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3692
            return m.replace(key, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3693
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3694
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3695
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3696
        public V computeIfAbsent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3697
                Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3698
            Objects.requireNonNull(mappingFunction);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3699
            return m.computeIfAbsent(key, k -> {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3700
                V value = mappingFunction.apply(k);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3701
                typeCheck(k, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3702
                return value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3703
            });
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3704
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3705
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3706
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3707
        public V computeIfPresent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3708
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3709
            return m.computeIfPresent(key, typeCheck(remappingFunction));
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3710
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3711
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3712
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3713
        public V compute(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3714
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3715
            return m.compute(key, typeCheck(remappingFunction));
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3716
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3717
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3718
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3719
        public V merge(K key, V value,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3720
                BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3721
            Objects.requireNonNull(remappingFunction);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3722
            return m.merge(key, value, (v1, v2) -> {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3723
                V newValue = remappingFunction.apply(v1, v2);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3724
                typeCheck(null, newValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3725
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3726
            });
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3727
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  3728
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
         * We need this class in addition to CheckedSet as Map.Entry permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
         * modification of the backing Map via the setValue operation.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
         * class is subtle: there are many possible attacks that must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
         * thwarted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
         * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
        static class CheckedEntrySet<K,V> implements Set<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
            private final Set<Map.Entry<K,V>> s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
            private final Class<V> valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
            CheckedEntrySet(Set<Map.Entry<K, V>> s, Class<V> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                this.valueType = valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
            public int size()        { return s.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
            public boolean isEmpty() { return s.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
            public String toString() { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
            public int hashCode()    { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
            public void clear()      {        s.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
            public boolean add(Map.Entry<K, V> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
            public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
            public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
                final Iterator<Map.Entry<K, V>> i = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
                final Class<V> valueType = this.valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
                return new Iterator<Map.Entry<K,V>>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
                    public boolean hasNext() { return i.hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
                    public void remove()     { i.remove(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
                    public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
                        return checkedEntry(i.next(), valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
            @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
            public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
                Object[] source = s.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
                 * Ensure that we don't get an ArrayStoreException even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
                 * s.toArray returns an array of something other than Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
                Object[] dest = (CheckedEntry.class.isInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
                    source.getClass().getComponentType()) ? source :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
                                 new Object[source.length]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
                for (int i = 0; i < source.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
                    dest[i] = checkedEntry((Map.Entry<K,V>)source[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
                                           valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
                return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
            @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
            public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
                // We don't pass a to s.toArray, to avoid window of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
                // vulnerability wherein an unscrupulous multithreaded client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                // could get his hands on raw (unwrapped) Entries from s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                T[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
                for (int i=0; i<arr.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
                    arr[i] = (T) checkedEntry((Map.Entry<K,V>)arr[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
                                              valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
                if (arr.length > a.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
                    return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
                System.arraycopy(arr, 0, a, 0, arr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
                if (a.length > arr.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
                    a[arr.length] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
             * This method is overridden to protect the backing set against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
             * an object with a nefarious equals function that senses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
             * that the equality-candidate is Map.Entry and calls its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
             * setValue method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
            public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                return s.contains(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
                    (e instanceof CheckedEntry) ? e : checkedEntry(e, valueType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
             * The bulk collection methods are overridden to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
             * against an unscrupulous collection whose contains(Object o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
             * method senses when o is a Map.Entry, and calls o.setValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
            public boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
                for (Object o : c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                    if (!contains(o)) // Invokes safe contains() above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
            public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
                return s.remove(new AbstractMap.SimpleImmutableEntry
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3840
                                <>((Map.Entry<?,?>)o));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
            public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
                return batchRemove(c, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
            public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
                return batchRemove(c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
            private boolean batchRemove(Collection<?> c, boolean complement) {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19603
diff changeset
  3850
                Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
                boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
                Iterator<Map.Entry<K,V>> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
                    if (c.contains(it.next()) != complement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
                        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
                        modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
            public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
                if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
                if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
                Set<?> that = (Set<?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
                return that.size() == s.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
                    && containsAll(that); // Invokes safe containsAll() above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
            static <K,V,T> CheckedEntry<K,V,T> checkedEntry(Map.Entry<K,V> e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
                                                            Class<T> valueType) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3874
                return new CheckedEntry<>(e, valueType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
             * This "wrapper class" serves two purposes: it prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
             * the client from modifying the backing Map, by short-circuiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
             * the setValue method, and it protects the backing Map against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
             * an ill-behaved Map.Entry that attempts to modify another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
             * Map.Entry when asked to perform an equality check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
            private static class CheckedEntry<K,V,T> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
                private final Map.Entry<K, V> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
                private final Class<T> valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
                CheckedEntry(Map.Entry<K, V> e, Class<T> valueType) {
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3889
                    this.e = Objects.requireNonNull(e);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3890
                    this.valueType = Objects.requireNonNull(valueType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
                public K getKey()        { return e.getKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
                public V getValue()      { return e.getValue(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
                public int hashCode()    { return e.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
                public String toString() { return e.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
                public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
                    if (value != null && !valueType.isInstance(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
                        throw new ClassCastException(badValueMsg(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
                    return e.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
                private String badValueMsg(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
                    return "Attempt to insert " + value.getClass() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
                        " value into map with value type " + valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
                public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
                    if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
                    if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
                    return e.equals(new AbstractMap.SimpleImmutableEntry
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3915
                                    <>((Map.Entry<?,?>)o));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
     * Returns a dynamically typesafe view of the specified sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
     * Any attempt to insert a mapping whose key or value have the wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
     * type will result in an immediate {@link ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
     * Similarly, any attempt to modify the value currently associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
     * a key will result in an immediate {@link ClassCastException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
     * whether the modification is attempted directly through the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
     * itself, or through a {@link Map.Entry} instance obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
     * map's {@link Map#entrySet() entry set} view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
     * <p>Assuming a map contains no incorrectly typed keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
     * prior to the time a dynamically typesafe view is generated, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
     * that all subsequent access to the map takes place through the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
     * (or one of its collection views), it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
     * map cannot contain an incorrectly typed key or value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
     * <p>The returned map will be serializable if the specified map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
     * type, the returned map permits insertion of null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
     * whenever the backing map does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3948
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  3949
     * @param <V> the class of the map values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
     * @param m the map for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
     * @param keyType the type of key that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
     * @param valueType the type of value that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
     * @return a dynamically typesafe view of the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
    public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
                                                        Class<K> keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
                                                        Class<V> valueType) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  3960
        return new CheckedSortedMap<>(m, keyType, valueType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
    static class CheckedSortedMap<K,V> extends CheckedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
        implements SortedMap<K,V>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
        private static final long serialVersionUID = 1599671320688067438L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
        private final SortedMap<K, V> sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
        CheckedSortedMap(SortedMap<K, V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
                         Class<K> keyType, Class<V> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
            super(m, keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
            sm = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
        public Comparator<? super K> comparator() { return sm.comparator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
        public K firstKey()                       { return sm.firstKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
        public K lastKey()                        { return sm.lastKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
        public SortedMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
            return checkedSortedMap(sm.subMap(fromKey, toKey),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
                                    keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
        public SortedMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
            return checkedSortedMap(sm.headMap(toKey), keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
        public SortedMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
            return checkedSortedMap(sm.tailMap(fromKey), keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3995
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3996
     * Returns a dynamically typesafe view of the specified navigable map.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3997
     * Any attempt to insert a mapping whose key or value have the wrong
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3998
     * type will result in an immediate {@link ClassCastException}.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  3999
     * Similarly, any attempt to modify the value currently associated with
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4000
     * a key will result in an immediate {@link ClassCastException},
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4001
     * whether the modification is attempted directly through the map
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4002
     * itself, or through a {@link Map.Entry} instance obtained from the
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4003
     * map's {@link Map#entrySet() entry set} view.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4004
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4005
     * <p>Assuming a map contains no incorrectly typed keys or values
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4006
     * prior to the time a dynamically typesafe view is generated, and
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4007
     * that all subsequent access to the map takes place through the view
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4008
     * (or one of its collection views), it is <em>guaranteed</em> that the
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4009
     * map cannot contain an incorrectly typed key or value.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4010
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4011
     * <p>A discussion of the use of dynamically typesafe views may be
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4012
     * found in the documentation for the {@link #checkedCollection
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4013
     * checkedCollection} method.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4014
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4015
     * <p>The returned map will be serializable if the specified map is
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4016
     * serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4017
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4018
     * <p>Since {@code null} is considered to be a value of any reference
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4019
     * type, the returned map permits insertion of null keys or values
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4020
     * whenever the backing map does.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4021
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4022
     * @param <K> type of map keys
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4023
     * @param <V> type of map values
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4024
     * @param m the map for which a dynamically typesafe view is to be
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4025
     *          returned
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4026
     * @param keyType the type of key that {@code m} is permitted to hold
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4027
     * @param valueType the type of value that {@code m} is permitted to hold
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4028
     * @return a dynamically typesafe view of the specified map
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4029
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4030
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4031
    public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K, V> m,
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4032
                                                        Class<K> keyType,
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4033
                                                        Class<V> valueType) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4034
        return new CheckedNavigableMap<>(m, keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4035
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4036
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4037
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4038
     * @serial include
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4039
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4040
    static class CheckedNavigableMap<K,V> extends CheckedSortedMap<K,V>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4041
        implements NavigableMap<K,V>, Serializable
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4042
    {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4043
        private static final long serialVersionUID = -4852462692372534096L;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4044
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4045
        private final NavigableMap<K, V> nm;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4046
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4047
        CheckedNavigableMap(NavigableMap<K, V> m,
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4048
                         Class<K> keyType, Class<V> valueType) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4049
            super(m, keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4050
            nm = m;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4051
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4052
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4053
        public Comparator<? super K> comparator()   { return nm.comparator(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4054
        public K firstKey()                           { return nm.firstKey(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4055
        public K lastKey()                             { return nm.lastKey(); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4056
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4057
        public Entry<K, V> lowerEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4058
            Entry<K,V> lower = nm.lowerEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4059
            return (null != lower)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4060
                ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(lower, valueType)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4061
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4062
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4063
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4064
        public K lowerKey(K key)                   { return nm.lowerKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4065
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4066
        public Entry<K, V> floorEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4067
            Entry<K,V> floor = nm.floorEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4068
            return (null != floor)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4069
                ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(floor, valueType)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4070
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4071
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4072
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4073
        public K floorKey(K key)                   { return nm.floorKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4074
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4075
        public Entry<K, V> ceilingEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4076
            Entry<K,V> ceiling = nm.ceilingEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4077
            return (null != ceiling)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4078
                ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(ceiling, valueType)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4079
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4080
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4081
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4082
        public K ceilingKey(K key)               { return nm.ceilingKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4083
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4084
        public Entry<K, V> higherEntry(K key) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4085
            Entry<K,V> higher = nm.higherEntry(key);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4086
            return (null != higher)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4087
                ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(higher, valueType)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4088
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4089
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4090
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4091
        public K higherKey(K key)                 { return nm.higherKey(key); }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4092
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4093
        public Entry<K, V> firstEntry() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4094
            Entry<K,V> first = nm.firstEntry();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4095
            return (null != first)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4096
                ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(first, valueType)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4097
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4098
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4099
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4100
        public Entry<K, V> lastEntry() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4101
            Entry<K,V> last = nm.lastEntry();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4102
            return (null != last)
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4103
                ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(last, valueType)
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4104
                : null;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4105
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4106
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4107
        public Entry<K, V> pollFirstEntry() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4108
            Entry<K,V> entry = nm.pollFirstEntry();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4109
            return (null == entry)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4110
                ? null
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4111
                : new CheckedMap.CheckedEntrySet.CheckedEntry<>(entry, valueType);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4112
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4113
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4114
        public Entry<K, V> pollLastEntry() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4115
            Entry<K,V> entry = nm.pollLastEntry();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4116
            return (null == entry)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4117
                ? null
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18829
diff changeset
  4118
                : new CheckedMap.CheckedEntrySet.CheckedEntry<>(entry, valueType);
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4119
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4120
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4121
        public NavigableMap<K, V> descendingMap() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4122
            return checkedNavigableMap(nm.descendingMap(), keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4123
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4124
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4125
        public NavigableSet<K> keySet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4126
            return navigableKeySet();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4127
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4128
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4129
        public NavigableSet<K> navigableKeySet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4130
            return checkedNavigableSet(nm.navigableKeySet(), keyType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4131
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4132
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4133
        public NavigableSet<K> descendingKeySet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4134
            return checkedNavigableSet(nm.descendingKeySet(), keyType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4135
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4136
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4137
        @Override
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4138
        public NavigableMap<K,V> subMap(K fromKey, K toKey) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4139
            return checkedNavigableMap(nm.subMap(fromKey, true, toKey, false),
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4140
                                    keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4141
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4142
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4143
        @Override
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4144
        public NavigableMap<K,V> headMap(K toKey) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4145
            return checkedNavigableMap(nm.headMap(toKey, false), keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4146
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4147
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4148
        @Override
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4149
        public NavigableMap<K,V> tailMap(K fromKey) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4150
            return checkedNavigableMap(nm.tailMap(fromKey, true), keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4151
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4152
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4153
        public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4154
            return checkedNavigableMap(nm.subMap(fromKey, fromInclusive, toKey, toInclusive), keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4155
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4156
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4157
        public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4158
            return checkedNavigableMap(nm.headMap(toKey, inclusive), keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4159
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4160
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4161
        public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4162
            return checkedNavigableMap(nm.tailMap(fromKey, inclusive), keyType, valueType);
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4163
        }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4164
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4165
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
    // Empty collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
     * Returns an iterator that has no elements.  More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4171
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
     * <li>{@link Iterator#hasNext hasNext} always returns {@code
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4173
     * false}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
     * <li>{@link Iterator#next next} always throws {@link
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4175
     * NoSuchElementException}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
     * <li>{@link Iterator#remove remove} always throws {@link
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4177
     * IllegalStateException}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
     * <p>Implementations of this method are permitted, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
     * required, to return the same object from multiple invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
     *
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4183
     * @param <T> type of elements, if there were any, in the iterator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
     * @return an empty iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
    public static <T> Iterator<T> emptyIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
        return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
    private static class EmptyIterator<E> implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
        static final EmptyIterator<Object> EMPTY_ITERATOR
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4194
            = new EmptyIterator<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
        public boolean hasNext() { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
        public E next() { throw new NoSuchElementException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
        public void remove() { throw new IllegalStateException(); }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4199
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4200
        public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4201
            Objects.requireNonNull(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4202
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
     * Returns a list iterator that has no elements.  More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4208
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
     * <li>{@link Iterator#hasNext hasNext} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
     * ListIterator#hasPrevious hasPrevious} always return {@code
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4211
     * false}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
     * <li>{@link Iterator#next next} and {@link ListIterator#previous
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4213
     * previous} always throw {@link NoSuchElementException}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
     * <li>{@link Iterator#remove remove} and {@link ListIterator#set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4215
     * set} always throw {@link IllegalStateException}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
     * <li>{@link ListIterator#add add} always throws {@link
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4217
     * UnsupportedOperationException}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
     * <li>{@link ListIterator#nextIndex nextIndex} always returns
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4219
     * {@code 0}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
     * <li>{@link ListIterator#previousIndex previousIndex} always
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4221
     * returns {@code -1}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
     * <p>Implementations of this method are permitted, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
     * required, to return the same object from multiple invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
     *
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4227
     * @param <T> type of elements, if there were any, in the iterator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
     * @return an empty list iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
    public static <T> ListIterator<T> emptyListIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
        return (ListIterator<T>) EmptyListIterator.EMPTY_ITERATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
    private static class EmptyListIterator<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
        extends EmptyIterator<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
        implements ListIterator<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
        static final EmptyListIterator<Object> EMPTY_ITERATOR
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4241
            = new EmptyListIterator<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
        public boolean hasPrevious() { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
        public E previous() { throw new NoSuchElementException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
        public int nextIndex()     { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
        public int previousIndex() { return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
        public void set(E e) { throw new IllegalStateException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
        public void add(E e) { throw new UnsupportedOperationException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
     * Returns an enumeration that has no elements.  More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4254
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4255
     * <li>{@link Enumeration#hasMoreElements hasMoreElements} always
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4256
     * returns {@code false}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
     * <li> {@link Enumeration#nextElement nextElement} always throws
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4258
     * {@link NoSuchElementException}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4261
     * <p>Implementations of this method are permitted, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4262
     * required, to return the same object from multiple invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4263
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4264
     * @param  <T> the class of the objects in the enumeration
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
     * @return an empty enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
    public static <T> Enumeration<T> emptyEnumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
        return (Enumeration<T>) EmptyEnumeration.EMPTY_ENUMERATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
    private static class EmptyEnumeration<E> implements Enumeration<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
        static final EmptyEnumeration<Object> EMPTY_ENUMERATION
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4275
            = new EmptyEnumeration<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
        public boolean hasMoreElements() { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
        public E nextElement() { throw new NoSuchElementException(); }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 25859
diff changeset
  4279
        public Iterator<E> asIterator() { return emptyIterator(); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
     * The empty set (immutable).  This set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
     * @see #emptySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  4287
    @SuppressWarnings("rawtypes")
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4288
    public static final Set EMPTY_SET = new EmptySet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4291
     * Returns an empty set (immutable).  This set is serializable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
     * Unlike the like-named field, this method is parameterized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
     * <p>This example illustrates the type-safe way to obtain an empty set:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
     *     Set&lt;String&gt; s = Collections.emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
     * </pre>
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4298
     * @implNote Implementations of this method need not create a separate
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4299
     * {@code Set} object for each call.  Using this method is likely to have
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4300
     * comparable cost to using the like-named field.  (Unlike this method, the
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4301
     * field does not provide type safety.)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4302
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4303
     * @param  <T> the class of the objects in the set
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4304
     * @return the empty set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
     * @see #EMPTY_SET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4307
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4309
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4310
    public static final <T> Set<T> emptySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
        return (Set<T>) EMPTY_SET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
    private static class EmptySet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
        extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
        implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
        private static final long serialVersionUID = 1582296315990362920L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
        public Iterator<E> iterator() { return emptyIterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
        public int size() {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
        public boolean isEmpty() {return true;}
39059
a51b9d78b20c 8158253: Collections: Implement a noop clear() for EmptyList, EmptyMap and EmptySet
psandoz
parents: 36436
diff changeset
  4327
        public void clear() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
        public boolean contains(Object obj) {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
        public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
        public Object[] toArray() { return new Object[0]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
            if (a.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4336
                a[0] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4340
        // Override default methods in Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4341
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4342
        public void forEach(Consumer<? super E> action) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4343
            Objects.requireNonNull(action);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4344
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4345
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4346
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4347
            Objects.requireNonNull(filter);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4348
            return false;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4349
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4350
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4351
        public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4352
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
        // Preserves singleton property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
            return EMPTY_SET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
        }
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4357
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4358
        @Override
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4359
        public int hashCode() {
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4360
            return 0;
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4361
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4365
     * Returns an empty sorted set (immutable).  This set is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4366
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4367
     * <p>This example illustrates the type-safe way to obtain an empty
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4368
     * sorted set:
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4369
     * <pre> {@code
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4370
     *     SortedSet<String> s = Collections.emptySortedSet();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4371
     * }</pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4372
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4373
     * @implNote Implementations of this method need not create a separate
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4374
     * {@code SortedSet} object for each call.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4375
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4376
     * @param <E> type of elements, if there were any, in the set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4377
     * @return the empty sorted set
10896
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4378
     * @since 1.8
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4379
     */
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4380
    @SuppressWarnings("unchecked")
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4381
    public static <E> SortedSet<E> emptySortedSet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4382
        return (SortedSet<E>) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET;
10896
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4383
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4384
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4385
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4386
     * Returns an empty navigable set (immutable).  This set is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4387
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4388
     * <p>This example illustrates the type-safe way to obtain an empty
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4389
     * navigable set:
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4390
     * <pre> {@code
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4391
     *     NavigableSet<String> s = Collections.emptyNavigableSet();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4392
     * }</pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4393
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4394
     * @implNote Implementations of this method need not
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4395
     * create a separate {@code NavigableSet} object for each call.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4396
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4397
     * @param <E> type of elements, if there were any, in the set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4398
     * @return the empty navigable set
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4399
     * @since 1.8
10896
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4400
     */
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4401
    @SuppressWarnings("unchecked")
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4402
    public static <E> NavigableSet<E> emptyNavigableSet() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4403
        return (NavigableSet<E>) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET;
10896
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4404
    }
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4405
8b5d56140b70 4533691: Add Collections.emptySortedSet()
mduigou
parents: 10790
diff changeset
  4406
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
     * The empty list (immutable).  This list is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
     * @see #emptyList()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  4411
    @SuppressWarnings("rawtypes")
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4412
    public static final List EMPTY_LIST = new EmptyList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4415
     * Returns an empty list (immutable).  This list is serializable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4417
     * <p>This example illustrates the type-safe way to obtain an empty list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4418
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4419
     *     List&lt;String&gt; s = Collections.emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
     * </pre>
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  4421
     *
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  4422
     * @implNote
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  4423
     * Implementations of this method need not create a separate {@code List}
22285
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  4424
     * object for each call.   Using this method is likely to have comparable
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  4425
     * cost to using the like-named field.  (Unlike this method, the field does
6c67737a5ac0 8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
psandoz
parents: 22078
diff changeset
  4426
     * not provide type safety.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
     *
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4428
     * @param <T> type of elements, if there were any, in the list
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4429
     * @return an empty immutable list
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4430
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
     * @see #EMPTY_LIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
    public static final <T> List<T> emptyList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
        return (List<T>) EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
    private static class EmptyList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
        implements RandomAccess, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
        private static final long serialVersionUID = 8842843931221139166L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
            return emptyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
        public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
            return emptyListIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
        public int size() {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
        public boolean isEmpty() {return true;}
39059
a51b9d78b20c 8158253: Collections: Implement a noop clear() for EmptyList, EmptyMap and EmptySet
psandoz
parents: 36436
diff changeset
  4456
        public void clear() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
        public boolean contains(Object obj) {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
        public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
        public Object[] toArray() { return new Object[0]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
            if (a.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
                a[0] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
            throw new IndexOutOfBoundsException("Index: "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
            return (o instanceof List) && ((List<?>)o).isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
        public int hashCode() { return 1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4479
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4480
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4481
            Objects.requireNonNull(filter);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4482
            return false;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4483
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4484
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4485
        public void replaceAll(UnaryOperator<E> operator) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4486
            Objects.requireNonNull(operator);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4487
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4488
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4489
        public void sort(Comparator<? super E> c) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4490
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4491
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4492
        // Override default methods in Collection
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4493
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4494
        public void forEach(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4495
            Objects.requireNonNull(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4496
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4497
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4498
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4499
        public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4500
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
        // Preserves singleton property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4503
            return EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
     * The empty map (immutable).  This map is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
     * @see #emptyMap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  4513
    @SuppressWarnings("rawtypes")
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4514
    public static final Map EMPTY_MAP = new EmptyMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4517
     * Returns an empty map (immutable).  This map is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4518
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4519
     * <p>This example illustrates the type-safe way to obtain an empty map:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
     *     Map&lt;String, Date&gt; s = Collections.emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
     * </pre>
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4523
     * @implNote Implementations of this method need not create a separate
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4524
     * {@code Map} object for each call.  Using this method is likely to have
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4525
     * comparable cost to using the like-named field.  (Unlike this method, the
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4526
     * field does not provide type safety.)
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4527
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4528
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4529
     * @param <V> the class of the map values
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4530
     * @return an empty map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
     * @see #EMPTY_MAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
    public static final <K,V> Map<K,V> emptyMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
        return (Map<K,V>) EMPTY_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
    /**
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4540
     * Returns an empty sorted map (immutable).  This map is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4541
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4542
     * <p>This example illustrates the type-safe way to obtain an empty map:
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4543
     * <pre> {@code
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4544
     *     SortedMap<String, Date> s = Collections.emptySortedMap();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4545
     * }</pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4546
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4547
     * @implNote Implementations of this method need not create a separate
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4548
     * {@code SortedMap} object for each call.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4549
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4550
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4551
     * @param <V> the class of the map values
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4552
     * @return an empty sorted map
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4553
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4554
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4555
    @SuppressWarnings("unchecked")
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4556
    public static final <K,V> SortedMap<K,V> emptySortedMap() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4557
        return (SortedMap<K,V>) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4558
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4559
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4560
    /**
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4561
     * Returns an empty navigable map (immutable).  This map is serializable.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4562
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4563
     * <p>This example illustrates the type-safe way to obtain an empty map:
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4564
     * <pre> {@code
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4565
     *     NavigableMap<String, Date> s = Collections.emptyNavigableMap();
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4566
     * }</pre>
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4567
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4568
     * @implNote Implementations of this method need not create a separate
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4569
     * {@code NavigableMap} object for each call.
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4570
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4571
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4572
     * @param <V> the class of the map values
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4573
     * @return an empty navigable map
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4574
     * @since 1.8
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4575
     */
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4576
    @SuppressWarnings("unchecked")
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4577
    public static final <K,V> NavigableMap<K,V> emptyNavigableMap() {
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4578
        return (NavigableMap<K,V>) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP;
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4579
    }
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4580
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4581
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4584
    private static class EmptyMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4585
        extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
        implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
        private static final long serialVersionUID = 6428348081105594320L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
        public int size()                          {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
        public boolean isEmpty()                   {return true;}
39059
a51b9d78b20c 8158253: Collections: Implement a noop clear() for EmptyList, EmptyMap and EmptySet
psandoz
parents: 36436
diff changeset
  4592
        public void clear()                        {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
        public boolean containsKey(Object key)     {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
        public boolean containsValue(Object value) {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
        public V get(Object key)                   {return null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
        public Set<K> keySet()                     {return emptySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
        public Collection<V> values()              {return emptySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
        public Set<Map.Entry<K,V>> entrySet()      {return emptySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4600
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
            return (o instanceof Map) && ((Map<?,?>)o).isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4604
        public int hashCode()                      {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4605
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4606
        // Override default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4607
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4608
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4609
        public V getOrDefault(Object k, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4610
            return defaultValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4611
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4612
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4613
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4614
        public void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4615
            Objects.requireNonNull(action);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4616
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4617
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4618
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4619
        public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4620
            Objects.requireNonNull(function);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4621
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4622
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4623
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4624
        public V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4625
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4626
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4627
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4628
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4629
        public boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4630
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4631
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4632
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4633
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4634
        public boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4635
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4636
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4637
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4638
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4639
        public V replace(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4640
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4641
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4642
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4643
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4644
        public V computeIfAbsent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4645
                Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4646
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4647
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4648
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4649
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4650
        public V computeIfPresent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4651
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4652
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4653
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4654
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4655
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4656
        public V compute(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4657
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4658
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4659
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4660
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4661
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4662
        public V merge(K key, V value,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4663
                BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4664
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4665
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4666
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
        // Preserves singleton property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4669
            return EMPTY_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
    // Singleton collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
     * Returns an immutable set containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
     * The returned set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4679
     * @param  <T> the class of the objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
     * @param o the sole object to be stored in the returned set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
     * @return an immutable set containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
    public static <T> Set<T> singleton(T o) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4684
        return new SingletonSet<>(o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
    static <E> Iterator<E> singletonIterator(final E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4688
        return new Iterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4689
            private boolean hasNext = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4690
            public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4691
                return hasNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4693
            public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4694
                if (hasNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4695
                    hasNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
                    return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4697
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4698
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4700
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4701
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4703
            @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4704
            public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4705
                Objects.requireNonNull(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4706
                if (hasNext) {
42435
349ac3a65124 8166446: SingletonIterator.forEachRemaining doesn't advance before calling action
smarks
parents: 39059
diff changeset
  4707
                    hasNext = false;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4708
                    action.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4709
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4710
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4711
        };
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4712
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4713
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4714
    /**
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4715
     * Creates a {@code Spliterator} with only the specified element
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4716
     *
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4717
     * @param <T> Type of elements
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4718
     * @return A singleton {@code Spliterator}
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4719
     */
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4720
    static <T> Spliterator<T> singletonSpliterator(final T element) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4721
        return new Spliterator<T>() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4722
            long est = 1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4723
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4724
            @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4725
            public Spliterator<T> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4726
                return null;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4727
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4728
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4729
            @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4730
            public boolean tryAdvance(Consumer<? super T> consumer) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4731
                Objects.requireNonNull(consumer);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4732
                if (est > 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4733
                    est--;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4734
                    consumer.accept(element);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4735
                    return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4736
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4737
                return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4738
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4739
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4740
            @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4741
            public void forEachRemaining(Consumer<? super T> consumer) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4742
                tryAdvance(consumer);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4743
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4744
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4745
            @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4746
            public long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4747
                return est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4748
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4749
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4750
            @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4751
            public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4752
                int value = (element != null) ? Spliterator.NONNULL : 0;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4753
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4754
                return value | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4755
                       Spliterator.DISTINCT | Spliterator.ORDERED;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4756
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4757
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4761
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
    private static class SingletonSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4764
        extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4765
        implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4766
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4767
        private static final long serialVersionUID = 3193687207550431679L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4768
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  4769
        private final E element;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4771
        SingletonSet(E e) {element = e;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4773
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4774
            return singletonIterator(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4777
        public int size() {return 1;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4779
        public boolean contains(Object o) {return eq(o, element);}
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4780
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4781
        // Override default methods for Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4782
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4783
        public void forEach(Consumer<? super E> action) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4784
            action.accept(element);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4785
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4786
        @Override
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4787
        public Spliterator<E> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4788
            return singletonSpliterator(element);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4789
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4790
        @Override
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4791
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4792
            throw new UnsupportedOperationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4793
        }
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4794
        @Override
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4795
        public int hashCode() {
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4796
            return Objects.hashCode(element);
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4797
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4801
     * Returns an immutable list containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4802
     * The returned list is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4804
     * @param  <T> the class of the objects in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
     * @param o the sole object to be stored in the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
     * @return an immutable list containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4807
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4809
    public static <T> List<T> singletonList(T o) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4810
        return new SingletonList<>(o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4814
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4816
    private static class SingletonList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4817
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4818
        implements RandomAccess, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4820
        private static final long serialVersionUID = 3093736618740652951L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4822
        private final E element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4824
        SingletonList(E obj)                {element = obj;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4826
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4827
            return singletonIterator(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4830
        public int size()                   {return 1;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4832
        public boolean contains(Object obj) {return eq(obj, element);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4834
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4835
            if (index != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4836
              throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4837
            return element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4838
        }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4839
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4840
        // Override default methods for Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4841
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4842
        public void forEach(Consumer<? super E> action) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4843
            action.accept(element);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4844
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4845
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4846
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4847
            throw new UnsupportedOperationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4848
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4849
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4850
        public void replaceAll(UnaryOperator<E> operator) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4851
            throw new UnsupportedOperationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4852
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4853
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4854
        public void sort(Comparator<? super E> c) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  4855
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4856
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4857
        public Spliterator<E> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4858
            return singletonSpliterator(element);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  4859
        }
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4860
        @Override
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4861
        public int hashCode() {
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4862
            return Objects.hashCode(element);
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4863
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4867
     * Returns an immutable map, mapping only the specified key to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
     * specified value.  The returned map is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4869
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4870
     * @param <K> the class of the map keys
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  4871
     * @param <V> the class of the map values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4872
     * @param key the sole key to be stored in the returned map.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  4873
     * @param value the value to which the returned map maps {@code key}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4874
     * @return an immutable map containing only the specified key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4875
     *         mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4876
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4878
    public static <K,V> Map<K,V> singletonMap(K key, V value) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4879
        return new SingletonMap<>(key, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4882
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
    private static class SingletonMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
          extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
          implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
        private static final long serialVersionUID = -6979724477215052911L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
        private final K k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
        private final V v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
        SingletonMap(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
            k = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
            v = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4898
        public int size()                                           {return 1;}
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4899
        public boolean isEmpty()                                {return false;}
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4900
        public boolean containsKey(Object key)             {return eq(key, k);}
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4901
        public boolean containsValue(Object value)       {return eq(value, v);}
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  4902
        public V get(Object key)              {return (eq(key, k) ? v : null);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4903
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  4904
        private transient Set<K> keySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  4905
        private transient Set<Map.Entry<K,V>> entrySet;
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22285
diff changeset
  4906
        private transient Collection<V> values;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4908
        public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4909
            if (keySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4910
                keySet = singleton(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4911
            return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
            if (entrySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
                entrySet = Collections.<Map.Entry<K,V>>singleton(
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  4917
                    new SimpleImmutableEntry<>(k, v));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4918
            return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
        public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
            if (values==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
                values = singleton(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
            return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4927
        // Override default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4928
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4929
        public V getOrDefault(Object key, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4930
            return eq(key, k) ? v : defaultValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4931
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4932
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4933
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4934
        public void forEach(BiConsumer<? super K, ? super V> action) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4935
            action.accept(k, v);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4936
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4937
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4938
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4939
        public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4940
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4941
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4942
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4943
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4944
        public V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4945
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4946
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4947
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4948
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4949
        public boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4950
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4951
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4952
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4953
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4954
        public boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4955
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4956
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4957
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4958
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4959
        public V replace(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4960
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4961
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4962
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4963
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4964
        public V computeIfAbsent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4965
                Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4966
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4967
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4968
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4969
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4970
        public V computeIfPresent(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4971
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4972
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4973
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4974
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4975
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4976
        public V compute(K key,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4977
                BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4978
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4979
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4980
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4981
        @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4982
        public V merge(K key, V value,
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4983
                BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4984
            throw new UnsupportedOperationException();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16064
diff changeset
  4985
        }
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4986
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4987
        @Override
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4988
        public int hashCode() {
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4989
            return Objects.hashCode(k) ^ Objects.hashCode(v);
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 42435
diff changeset
  4990
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
    // Miscellaneous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4995
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  4996
     * Returns an immutable list consisting of {@code n} copies of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4997
     * specified object.  The newly allocated data object is tiny (it contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4998
     * a single reference to the data object).  This method is useful in
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  4999
     * combination with the {@code List.addAll} method to grow lists.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
     * The returned list is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5001
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5002
     * @param  <T> the class of the object to copy and of the objects
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5003
     *         in the returned list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
     * @param  n the number of elements in the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
     * @param  o the element to appear repeatedly in the returned list.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5006
     * @return an immutable list consisting of {@code n} copies of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
     *         specified object.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5781
diff changeset
  5008
     * @throws IllegalArgumentException if {@code n < 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
     * @see    List#addAll(Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
     * @see    List#addAll(int, Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
    public static <T> List<T> nCopies(int n, T o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
        if (n < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
            throw new IllegalArgumentException("List length = " + n);
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  5015
        return new CopiesList<>(n, o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5019
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5021
    private static class CopiesList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5022
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
        implements RandomAccess, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5024
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5025
        private static final long serialVersionUID = 2739099268398711800L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5027
        final int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5028
        final E element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
        CopiesList(int n, E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5031
            assert n >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5032
            this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
            element = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5036
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5037
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5040
        public boolean contains(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5041
            return n != 0 && eq(obj, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5044
        public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5045
            return contains(o) ? 0 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5048
        public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5049
            return contains(o) ? n - 1 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5052
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5053
            if (index < 0 || index >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5054
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5055
                                                    ", Size: "+n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5056
            return element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5057
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5059
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5060
            final Object[] a = new Object[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5061
            if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5062
                Arrays.fill(a, 0, n, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5063
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5065
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  5066
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5067
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5068
            final int n = this.n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5069
            if (a.length < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5070
                a = (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5071
                    .newInstance(a.getClass().getComponentType(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5072
                if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5073
                    Arrays.fill(a, 0, n, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5074
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5075
                Arrays.fill(a, 0, n, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5076
                if (a.length > n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5077
                    a[n] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5078
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5079
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5082
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5083
            if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5084
                throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5085
            if (toIndex > n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5086
                throw new IndexOutOfBoundsException("toIndex = " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5087
            if (fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5088
                throw new IllegalArgumentException("fromIndex(" + fromIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5089
                                                   ") > toIndex(" + toIndex + ")");
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  5090
            return new CopiesList<>(toIndex - fromIndex, element);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5091
        }
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5092
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5093
        // Override default methods in Collection
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5094
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5095
        public Stream<E> stream() {
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5096
            return IntStream.range(0, n).mapToObj(i -> element);
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5097
        }
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5098
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5099
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5100
        public Stream<E> parallelStream() {
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5101
            return IntStream.range(0, n).parallel().mapToObj(i -> element);
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5102
        }
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5103
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5104
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5105
        public Spliterator<E> spliterator() {
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5106
            return stream().spliterator();
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5107
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5110
    /**
9004
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5111
     * Returns a comparator that imposes the reverse of the <em>natural
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5112
     * ordering</em> on a collection of objects that implement the
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5113
     * {@code Comparable} interface.  (The natural ordering is the ordering
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5114
     * imposed by the objects' own {@code compareTo} method.)  This enables a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5115
     * simple idiom for sorting (or maintaining) collections (or arrays) of
9004
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5116
     * objects that implement the {@code Comparable} interface in
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5117
     * reverse-natural-order.  For example, suppose {@code a} is an array of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5118
     * strings. Then: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5119
     *          Arrays.sort(a, Collections.reverseOrder());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5120
     * </pre> sorts the array in reverse-lexicographic (alphabetical) order.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5122
     * The returned comparator is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5123
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5124
     * @param  <T> the class of the objects compared by the comparator
9004
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5125
     * @return A comparator that imposes the reverse of the <i>natural
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5126
     *         ordering</i> on a collection of objects that implement
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5127
     *         the {@code Comparable} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5128
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5129
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  5130
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5131
    public static <T> Comparator<T> reverseOrder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5132
        return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5136
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5138
    private static class ReverseComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5139
        implements Comparator<Comparable<Object>>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5141
        private static final long serialVersionUID = 7207038068494060240L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5143
        static final ReverseComparator REVERSE_ORDER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5144
            = new ReverseComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5146
        public int compare(Comparable<Object> c1, Comparable<Object> c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5147
            return c2.compareTo(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5149
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 13795
diff changeset
  5150
        private Object readResolve() { return Collections.reverseOrder(); }
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5151
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5152
        @Override
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5153
        public Comparator<Comparable<Object>> reversed() {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5154
            return Comparator.naturalOrder();
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5155
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5159
     * Returns a comparator that imposes the reverse ordering of the specified
9004
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5160
     * comparator.  If the specified comparator is {@code null}, this method is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5161
     * equivalent to {@link #reverseOrder()} (in other words, it returns a
9004
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5162
     * comparator that imposes the reverse of the <em>natural ordering</em> on
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5163
     * a collection of objects that implement the Comparable interface).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5165
     * <p>The returned comparator is serializable (assuming the specified
9004
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5166
     * comparator is also serializable or {@code null}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5167
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5168
     * @param <T> the class of the objects compared by the comparator
9004
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5169
     * @param cmp a comparator who's ordering is to be reversed by the returned
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5170
     * comparator or {@code null}
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5171
     * @return A comparator that imposes the reverse ordering of the
284f923de1bf 7030442: Add missing @param tag for Collections.reverseOrder()
mduigou
parents: 8151
diff changeset
  5172
     *         specified comparator.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5173
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5175
    public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5176
        if (cmp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5177
            return reverseOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5179
        if (cmp instanceof ReverseComparator2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5180
            return ((ReverseComparator2<T>)cmp).cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5181
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  5182
        return new ReverseComparator2<>(cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5186
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5188
    private static class ReverseComparator2<T> implements Comparator<T>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5189
        Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5191
        private static final long serialVersionUID = 4374092139857L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5193
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5194
         * The comparator specified in the static factory.  This will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5195
         * be null, as the static factory returns a ReverseComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5196
         * instance if its argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5197
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5198
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5199
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5200
        final Comparator<T> cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5202
        ReverseComparator2(Comparator<T> cmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5203
            assert cmp != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5204
            this.cmp = cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5207
        public int compare(T t1, T t2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5208
            return cmp.compare(t2, t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5211
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5212
            return (o == this) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5213
                (o instanceof ReverseComparator2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5214
                 cmp.equals(((ReverseComparator2)o).cmp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5217
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5218
            return cmp.hashCode() ^ Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5219
        }
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5220
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5221
        @Override
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5222
        public Comparator<T> reversed() {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5223
            return cmp;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18156
diff changeset
  5224
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5228
     * Returns an enumeration over the specified collection.  This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5229
     * interoperability with legacy APIs that require an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5230
     * as input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5231
     *
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 25859
diff changeset
  5232
     * <p>The iterator returned from a call to {@link Enumeration#asIterator()}
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 25859
diff changeset
  5233
     * does not support removal of elements from the specified collection.  This
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 25859
diff changeset
  5234
     * is necessary to avoid unintentionally increasing the capabilities of the
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 25859
diff changeset
  5235
     * returned enumeration.
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 25859
diff changeset
  5236
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5237
     * @param  <T> the class of the objects in the collection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5238
     * @param c the collection for which an enumeration is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5239
     * @return an enumeration over the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5240
     * @see Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5242
    public static <T> Enumeration<T> enumeration(final Collection<T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5243
        return new Enumeration<T>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5244
            private final Iterator<T> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5246
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5247
                return i.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5250
            public T nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5251
                return i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5253
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5257
     * Returns an array list containing the elements returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5258
     * specified enumeration in the order they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5259
     * enumeration.  This method provides interoperability between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5260
     * legacy APIs that return enumerations and new APIs that require
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5261
     * collections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5262
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5263
     * @param <T> the class of the objects returned by the enumeration
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5264
     * @param e enumeration providing elements for the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5265
     *          array list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5266
     * @return an array list containing the elements returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5267
     *         by the specified enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5268
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5269
     * @see Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5270
     * @see ArrayList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5272
    public static <T> ArrayList<T> list(Enumeration<T> e) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  5273
        ArrayList<T> l = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5274
        while (e.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5275
            l.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5276
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5280
     * Returns true if the specified arguments are equal, or both null.
18818
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  5281
     *
a9ceff754226 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
mduigou
parents: 18794
diff changeset
  5282
     * NB: Do not replace with Object.equals until JDK-8015417 is resolved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5284
    static boolean eq(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5285
        return o1==null ? o2==null : o1.equals(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5289
     * Returns the number of elements in the specified collection equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5290
     * specified object.  More formally, returns the number of elements
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5291
     * {@code e} in the collection such that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5292
     * {@code Objects.equals(o, e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5294
     * @param c the collection in which to determine the frequency
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5295
     *     of {@code o}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5296
     * @param o the object whose frequency is to be determined
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5297
     * @return the number of elements in {@code c} equal to {@code o}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5298
     * @throws NullPointerException if {@code c} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5299
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5301
    public static int frequency(Collection<?> c, Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5302
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5303
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5304
            for (Object e : c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5305
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5306
                    result++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5307
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5308
            for (Object e : c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5309
                if (o.equals(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5310
                    result++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5312
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5315
    /**
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5316
     * Returns {@code true} if the two specified collections have no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5317
     * elements in common.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5319
     * <p>Care must be exercised if this method is used on collections that
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5320
     * do not comply with the general contract for {@code Collection}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5321
     * Implementations may elect to iterate over either collection and test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5322
     * for containment in the other collection (or to perform any equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5323
     * computation).  If either collection uses a nonstandard equality test
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5324
     * (as does a {@link SortedSet} whose ordering is not <em>compatible with
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5325
     * equals</em>, or the key set of an {@link IdentityHashMap}), both
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5326
     * collections must use the same nonstandard equality test, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5327
     * result of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5328
     *
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5329
     * <p>Care must also be exercised when using collections that have
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5330
     * restrictions on the elements that they may contain. Collection
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5331
     * implementations are allowed to throw exceptions for any operation
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5332
     * involving elements they deem ineligible. For absolute safety the
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5333
     * specified collections should contain only elements which are
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5334
     * eligible elements for both collections.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5335
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5336
     * <p>Note that it is permissible to pass the same collection in both
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5337
     * parameters, in which case the method will return {@code true} if and
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5338
     * only if the collection is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5340
     * @param c1 a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5341
     * @param c2 a collection
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5342
     * @return {@code true} if the two specified collections have no
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5343
     * elements in common.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5344
     * @throws NullPointerException if either collection is {@code null}.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5345
     * @throws NullPointerException if one collection contains a {@code null}
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5346
     * element and {@code null} is not an eligible element for the other collection.
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9050
diff changeset
  5347
     * (<a href="Collection.html#optional-restrictions">optional</a>)
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5348
     * @throws ClassCastException if one collection contains an element that is
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9050
diff changeset
  5349
     * of a type which is ineligible for the other collection.
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9050
diff changeset
  5350
     * (<a href="Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5351
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5353
    public static boolean disjoint(Collection<?> c1, Collection<?> c2) {
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5354
        // The collection to be used for contains(). Preference is given to
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5355
        // the collection who's contains() has lower O() complexity.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5356
        Collection<?> contains = c2;
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5357
        // The collection to be iterated. If the collections' contains() impl
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5358
        // are of different O() complexity, the collection with slower
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5359
        // contains() will be used for iteration. For collections who's
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5360
        // contains() are of the same complexity then best performance is
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5361
        // achieved by iterating the smaller collection.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5362
        Collection<?> iterate = c1;
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5363
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5364
        // Performance optimization cases. The heuristics:
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5365
        //   1. Generally iterate over c1.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5366
        //   2. If c1 is a Set then iterate over c2.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5367
        //   3. If either collection is empty then result is always true.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5368
        //   4. Iterate over the smaller Collection.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5369
        if (c1 instanceof Set) {
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5370
            // Use c1 for contains as a Set's contains() is expected to perform
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5371
            // better than O(N/2)
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5372
            iterate = c2;
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5373
            contains = c1;
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5374
        } else if (!(c2 instanceof Set)) {
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5375
            // Both are mere Collections. Iterate over smaller collection.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5376
            // Example: If c1 contains 3 elements and c2 contains 50 elements and
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5377
            // assuming contains() requires ceiling(N/2) comparisons then
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5378
            // checking for all c1 elements in c2 would require 75 comparisons
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5379
            // (3 * ceiling(50/2)) vs. checking all c2 elements in c1 requiring
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5380
            // 100 comparisons (50 * ceiling(3/2)).
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5381
            int c1size = c1.size();
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5382
            int c2size = c2.size();
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5383
            if (c1size == 0 || c2size == 0) {
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5384
                // At least one collection is empty. Nothing will match.
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5385
                return true;
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5386
            }
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5387
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5388
            if (c1size > c2size) {
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5389
                iterate = c2;
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5390
                contains = c1;
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5391
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5393
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5394
        for (Object e : iterate) {
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5395
            if (contains.contains(e)) {
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5396
               // Found a common element. Collections are not disjoint.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5397
                return false;
7987
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5398
            }
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5399
        }
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5400
04c9822b55c9 6728865: Provide a better heuristics for Collections.disjoint method
mduigou
parents: 7976
diff changeset
  5401
        // No common elements were found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5402
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5406
     * Adds all of the specified elements to the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5407
     * Elements to be added may be specified individually or as an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5408
     * The behavior of this convenience method is identical to that of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5409
     * {@code c.addAll(Arrays.asList(elements))}, but this method is likely
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5410
     * to run significantly faster under most implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5412
     * <p>When elements are specified individually, this method provides a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5413
     * convenient way to add a few elements to an existing collection:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5414
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5415
     *     Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5416
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5417
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5418
     * @param  <T> the class of the elements to add and of the collection
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5419
     * @param c the collection into which {@code elements} are to be inserted
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5420
     * @param elements the elements to insert into {@code c}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5421
     * @return {@code true} if the collection changed as a result of the call
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5422
     * @throws UnsupportedOperationException if {@code c} does not support
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5423
     *         the {@code add} operation
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5424
     * @throws NullPointerException if {@code elements} contains one or more
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5425
     *         null values and {@code c} does not permit null elements, or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5426
     *         if {@code c} or {@code elements} are {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5427
     * @throws IllegalArgumentException if some property of a value in
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5428
     *         {@code elements} prevents it from being added to {@code c}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5429
     * @see Collection#addAll(Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5430
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5431
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 7987
diff changeset
  5432
    @SafeVarargs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5433
    public static <T> boolean addAll(Collection<? super T> c, T... elements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5434
        boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5435
        for (T element : elements)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5436
            result |= c.add(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5437
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5441
     * Returns a set backed by the specified map.  The resulting set displays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5442
     * the same ordering, concurrency, and performance characteristics as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5443
     * backing map.  In essence, this factory method provides a {@link Set}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5444
     * implementation corresponding to any {@link Map} implementation.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5445
     * is no need to use this method on a {@link Map} implementation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5446
     * already has a corresponding {@link Set} implementation (such as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5447
     * HashMap} or {@link TreeMap}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5449
     * <p>Each method invocation on the set returned by this method results in
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5450
     * exactly one method invocation on the backing map or its {@code keySet}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5451
     * view, with one exception.  The {@code addAll} method is implemented
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5452
     * as a sequence of {@code put} invocations on the backing map.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5454
     * <p>The specified map must be empty at the time this method is invoked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5455
     * and should not be accessed directly after this method returns.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5456
     * conditions are ensured if the map is created empty, passed directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5457
     * to this method, and no reference to the map is retained, as illustrated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5458
     * in the following code fragment:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5459
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5460
     *    Set&lt;Object&gt; weakHashSet = Collections.newSetFromMap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5461
     *        new WeakHashMap&lt;Object, Boolean&gt;());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5462
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5463
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5464
     * @param <E> the class of the map keys and of the objects in the
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5465
     *        returned set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5466
     * @param map the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5467
     * @return the set backed by the map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5468
     * @throws IllegalArgumentException if {@code map} is not empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5469
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5471
    public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
  5472
        return new SetFromMap<>(map);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5476
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5478
    private static class SetFromMap<E> extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5479
        implements Set<E>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5480
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5481
        private final Map<E, Boolean> m;  // The backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5482
        private transient Set<E> s;       // Its keySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5484
        SetFromMap(Map<E, Boolean> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5485
            if (!map.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5486
                throw new IllegalArgumentException("Map is non-empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5487
            m = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5488
            s = map.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5491
        public void clear()               {        m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5492
        public int size()                 { return m.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5493
        public boolean isEmpty()          { return m.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5494
        public boolean contains(Object o) { return m.containsKey(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5495
        public boolean remove(Object o)   { return m.remove(o) != null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5496
        public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5497
        public Iterator<E> iterator()     { return s.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5498
        public Object[] toArray()         { return s.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5499
        public <T> T[] toArray(T[] a)     { return s.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5500
        public String toString()          { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5501
        public int hashCode()             { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5502
        public boolean equals(Object o)   { return o == this || s.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5503
        public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5504
        public boolean removeAll(Collection<?> c)   {return s.removeAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5505
        public boolean retainAll(Collection<?> c)   {return s.retainAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5506
        // addAll is the only inherited implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5507
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  5508
        // Override default methods in Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5509
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5510
        public void forEach(Consumer<? super E> action) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5511
            s.forEach(action);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5512
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5513
        @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5514
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5515
            return s.removeIf(filter);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5516
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5517
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  5518
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  5519
        public Spliterator<E> spliterator() {return s.spliterator();}
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5520
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5521
        public Stream<E> stream()           {return s.stream();}
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5522
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5523
        public Stream<E> parallelStream()   {return s.parallelStream();}
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  5524
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5525
        private static final long serialVersionUID = 2454657854757543876L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5527
        private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5528
            throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5529
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5530
            stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5531
            s = m.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5536
     * Returns a view of a {@link Deque} as a Last-in-first-out (Lifo)
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5537
     * {@link Queue}. Method {@code add} is mapped to {@code push},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5538
     * {@code remove} is mapped to {@code pop} and so on. This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5539
     * view can be useful when you would like to use a method
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31469
diff changeset
  5540
     * requiring a {@code Queue} but you need Lifo ordering.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5542
     * <p>Each method invocation on the queue returned by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5543
     * results in exactly one method invocation on the backing deque, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5544
     * one exception.  The {@link Queue#addAll addAll} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5545
     * implemented as a sequence of {@link Deque#addFirst addFirst}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5546
     * invocations on the backing deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5547
     *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
  5548
     * @param  <T> the class of the objects in the deque
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5549
     * @param deque the deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5550
     * @return the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5551
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5553
    public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
34714
2637c9a952c2 8145006: Collections.asLifoQueue(null) doesn't throw NPE as specified
psandoz
parents: 33644
diff changeset
  5554
        return new AsLIFOQueue<>(Objects.requireNonNull(deque));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5558
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5560
    static class AsLIFOQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5561
        implements Queue<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5562
        private static final long serialVersionUID = 1802017725587941708L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5563
        private final Deque<E> q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5564
        AsLIFOQueue(Deque<E> q)           { this.q = q; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5565
        public boolean add(E e)           { q.addFirst(e); return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5566
        public boolean offer(E e)         { return q.offerFirst(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5567
        public E poll()                   { return q.pollFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5568
        public E remove()                 { return q.removeFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5569
        public E peek()                   { return q.peekFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5570
        public E element()                { return q.getFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5571
        public void clear()               {        q.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5572
        public int size()                 { return q.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5573
        public boolean isEmpty()          { return q.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5574
        public boolean contains(Object o) { return q.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5575
        public boolean remove(Object o)   { return q.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5576
        public Iterator<E> iterator()     { return q.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5577
        public Object[] toArray()         { return q.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5578
        public <T> T[] toArray(T[] a)     { return q.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5579
        public String toString()          { return q.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5580
        public boolean containsAll(Collection<?> c) {return q.containsAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5581
        public boolean removeAll(Collection<?> c)   {return q.removeAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5582
        public boolean retainAll(Collection<?> c)   {return q.retainAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5583
        // We use inherited addAll; forwarding addAll would be wrong
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5584
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  5585
        // Override default methods in Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5586
        @Override
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  5587
        public void forEach(Consumer<? super E> action) {q.forEach(action);}
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  5588
        @Override
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5589
        public boolean removeIf(Predicate<? super E> filter) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5590
            return q.removeIf(filter);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 16867
diff changeset
  5591
        }
19603
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5592
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5593
        public Spliterator<E> spliterator() {return q.spliterator();}
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5594
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5595
        public Stream<E> stream()           {return q.stream();}
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5596
        @Override
636cd05ce8e5 8023275: Wrapping collections should override default methods
henryjen
parents: 19445
diff changeset
  5597
        public Stream<E> parallelStream()   {return q.parallelStream();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5599
}