jdk/src/share/classes/java/util/Collections.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3420 bba8035eebfa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class consists exclusively of static methods that operate on or return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * collections.  It contains polymorphic algorithms that operate on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * collections, "wrappers", which return a new collection backed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * specified collection, and a few other odds and ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>The methods of this class all throw a <tt>NullPointerException</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * if the collections or class objects provided to them are null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>The documentation for the polymorphic algorithms contained in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * generally includes a brief description of the <i>implementation</i>.  Such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * descriptions should be regarded as <i>implementation notes</i>, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * parts of the <i>specification</i>.  Implementors should feel free to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * substitute other algorithms, so long as the specification itself is adhered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * to.  (For example, the algorithm used by <tt>sort</tt> does not have to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * a mergesort, but it does have to be <i>stable</i>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>The "destructive" algorithms contained in this class, that is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * algorithms that modify the collection on which they operate, are specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * to throw <tt>UnsupportedOperationException</tt> if the collection does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * support the appropriate mutation primitive(s), such as the <tt>set</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * method.  These algorithms may, but are not required to, throw this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * exception if an invocation would have no effect on the collection.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * example, invoking the <tt>sort</tt> method on an unmodifiable list that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * already sorted may or may not throw <tt>UnsupportedOperationException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see     Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @see     List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public class Collections {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Suppresses default constructor, ensuring non-instantiability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Collections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Tuning parameters for algorithms - Many of the List algorithms have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * two implementations, one of which is appropriate for RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * lists, the other for "sequential."  Often, the random access variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * yields better performance on small sequential access lists.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * tuning parameters below determine the cutoff point for what constitutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * a "small" sequential access list for each algorithm.  The values below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * were empirically determined to work well for LinkedList. Hopefully
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * they should be reasonable for other sequential access List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * implementations.  Those doing performance work on this code would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * do well to validate the values of these parameters from time to time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * (The first word of each tuning parameter name is the algorithm to which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * it applies.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final int BINARYSEARCH_THRESHOLD   = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final int REVERSE_THRESHOLD        =   18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final int SHUFFLE_THRESHOLD        =    5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final int FILL_THRESHOLD           =   25;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final int ROTATE_THRESHOLD         =  100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final int COPY_THRESHOLD           =   10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final int REPLACEALL_THRESHOLD     =   11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final int INDEXOFSUBLIST_THRESHOLD =   35;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Sorts the specified list into ascending order, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <i>natural ordering</i> of its elements.  All elements in the list must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * implement the <tt>Comparable</tt> interface.  Furthermore, all elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * in the list must be <i>mutually comparable</i> (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * for any elements <tt>e1</tt> and <tt>e2</tt> in the list).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This sort is guaranteed to be <i>stable</i>:  equal elements will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * not be reordered as a result of the sort.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The specified list must be modifiable, but need not be resizable.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The sorting algorithm is a modified mergesort (in which the merge is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * omitted if the highest element in the low sublist is less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * lowest element in the high sublist).  This algorithm offers guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * n log(n) performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * This implementation dumps the specified list into an array, sorts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * the array, and iterates over the list resetting each element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * from the corresponding position in the array.  This avoids the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * n<sup>2</sup> log(n) performance that would result from attempting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * to sort a linked list in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param  list the list to be sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *         <i>mutually comparable</i> (for example, strings and integers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws UnsupportedOperationException if the specified list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *         list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static <T extends Comparable<? super T>> void sort(List<T> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Object[] a = list.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Arrays.sort(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ListIterator<T> i = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (int j=0; j<a.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            i.set((T)a[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Sorts the specified list according to the order induced by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * specified comparator.  All elements in the list must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * comparable</i> using the specified comparator (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <tt>c.compare(e1, e2)</tt> must not throw a <tt>ClassCastException</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * for any elements <tt>e1</tt> and <tt>e2</tt> in the list).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * This sort is guaranteed to be <i>stable</i>:  equal elements will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * not be reordered as a result of the sort.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * The sorting algorithm is a modified mergesort (in which the merge is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * omitted if the highest element in the low sublist is less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * lowest element in the high sublist).  This algorithm offers guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * n log(n) performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * The specified list must be modifiable, but need not be resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * This implementation dumps the specified list into an array, sorts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * the array, and iterates over the list resetting each element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * from the corresponding position in the array.  This avoids the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * n<sup>2</sup> log(n) performance that would result from attempting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * to sort a linked list in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param  list the list to be sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param  c the comparator to determine the order of the list.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *        <tt>null</tt> value indicates that the elements' <i>natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *        ordering</i> should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *         <i>mutually comparable</i> using the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @throws UnsupportedOperationException if the specified list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *         list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public static <T> void sort(List<T> list, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Object[] a = list.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Arrays.sort(a, (Comparator)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ListIterator i = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (int j=0; j<a.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            i.set(a[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Searches the specified list for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * search algorithm.  The list must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * according to the {@linkplain Comparable natural ordering} of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * elements (as by the {@link #sort(List)} method) prior to making this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * call.  If it is not sorted, the results are undefined.  If the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * contains multiple elements equal to the specified object, there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * guarantee which one will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>This method runs in log(n) time for a "random access" list (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * provides near-constant-time positional access).  If the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * does not implement the {@link RandomAccess} interface and is large,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * this method will do an iterator-based binary search that performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * O(n) link traversals and O(log n) element comparisons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param  list the list to be searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param  key the key to be searched for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return the index of the search key, if it is contained in the list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *         key would be inserted into the list: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *         element greater than the key, or <tt>list.size()</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *         elements in the list are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         <i>mutually comparable</i> (for example, strings and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *         integers), or the search key is not mutually comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         with the elements of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public static <T>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    int binarySearch(List<? extends Comparable<? super T>> list, T key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return Collections.indexedBinarySearch(list, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return Collections.iteratorBinarySearch(list, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private static <T>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int high = list.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            Comparable<? super T> midVal = list.get(mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            int cmp = midVal.compareTo(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private static <T>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int high = list.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        ListIterator<? extends Comparable<? super T>> i = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            Comparable<? super T> midVal = get(i, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int cmp = midVal.compareTo(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Gets the ith element from the given list by repositioning the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * list listIterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static <T> T get(ListIterator<? extends T> i, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        T obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int pos = i.nextIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (pos <= index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                obj = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            } while (pos++ < index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                obj = i.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } while (--pos > index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Searches the specified list for the specified object using the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * search algorithm.  The list must be sorted into ascending order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * according to the specified comparator (as by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * {@link #sort(List, Comparator) sort(List, Comparator)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * method), prior to making this call.  If it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * not sorted, the results are undefined.  If the list contains multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * elements equal to the specified object, there is no guarantee which one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * will be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p>This method runs in log(n) time for a "random access" list (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * provides near-constant-time positional access).  If the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * does not implement the {@link RandomAccess} interface and is large,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * this method will do an iterator-based binary search that performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * O(n) link traversals and O(log n) element comparisons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param  list the list to be searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param  key the key to be searched for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param  c the comparator by which the list is ordered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *         A <tt>null</tt> value indicates that the elements'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *         {@linkplain Comparable natural ordering} should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return the index of the search key, if it is contained in the list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *         <i>insertion point</i> is defined as the point at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *         key would be inserted into the list: the index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *         element greater than the key, or <tt>list.size()</tt> if all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *         elements in the list are less than the specified key.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *         that this guarantees that the return value will be &gt;= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *         and only if the key is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @throws ClassCastException if the list contains elements that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *         <i>mutually comparable</i> using the specified comparator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *         or the search key is not mutually comparable with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         elements of the list using this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return binarySearch((List) list, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return Collections.indexedBinarySearch(list, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return Collections.iteratorBinarySearch(list, key, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private static <T> int indexedBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int high = l.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            T midVal = l.get(mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private static <T> int iteratorBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int high = l.size()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        ListIterator<? extends T> i = l.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        while (low <= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            int mid = (low + high) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            T midVal = get(i, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            int cmp = c.compare(midVal, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (cmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                low = mid + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            else if (cmp > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                high = mid - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                return mid; // key found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return -(low + 1);  // key not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private interface SelfComparable extends Comparable<SelfComparable> {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Reverses the order of the elements in the specified list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * This method runs in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param  list the list whose elements are to be reversed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws UnsupportedOperationException if the specified list or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *         its list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public static void reverse(List<?> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                swap(list, i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            ListIterator fwd = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            ListIterator rev = list.listIterator(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            for (int i=0, mid=list.size()>>1; i<mid; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                Object tmp = fwd.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                fwd.set(rev.previous());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                rev.set(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Randomly permutes the specified list using a default source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * randomness.  All permutations occur with approximately equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * likelihood.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * The hedge "approximately" is used in the foregoing description because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * default source of randomness is only approximately an unbiased source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * of independently chosen bits. If it were a perfect source of randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * chosen bits, then the algorithm would choose permutations with perfect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * uniformity.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * This implementation traverses the list backwards, from the last element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * up to the second, repeatedly swapping a randomly selected element into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * the "current position".  Elements are randomly selected from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * portion of the list that runs from the first element to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * position, inclusive.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * This method runs in linear time.  If the specified list does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * implement the {@link RandomAccess} interface and is large, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * implementation dumps the specified list into an array before shuffling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * it, and dumps the shuffled array back into the list.  This avoids the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * quadratic behavior that would result from shuffling a "sequential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * access" list in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param  list the list to be shuffled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @throws UnsupportedOperationException if the specified list or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *         its list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public static void shuffle(List<?> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            r = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        shuffle(list, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private static Random r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Randomly permute the specified list using the specified source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * randomness.  All permutations occur with equal likelihood
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * assuming that the source of randomness is fair.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * This implementation traverses the list backwards, from the last element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * up to the second, repeatedly swapping a randomly selected element into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * the "current position".  Elements are randomly selected from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * portion of the list that runs from the first element to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * position, inclusive.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * This method runs in linear time.  If the specified list does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * implement the {@link RandomAccess} interface and is large, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * implementation dumps the specified list into an array before shuffling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * it, and dumps the shuffled array back into the list.  This avoids the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * quadratic behavior that would result from shuffling a "sequential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * access" list in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param  list the list to be shuffled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param  rnd the source of randomness to use to shuffle the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @throws UnsupportedOperationException if the specified list or its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *         list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public static void shuffle(List<?> list, Random rnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            for (int i=size; i>1; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                swap(list, i-1, rnd.nextInt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            Object arr[] = list.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            // Shuffle array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            for (int i=size; i>1; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                swap(arr, i-1, rnd.nextInt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            // Dump array back into list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            ListIterator it = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            for (int i=0; i<arr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                it.set(arr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
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
     * Swaps the elements at the specified positions in the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * (If the specified positions are equal, invoking this method leaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * the list unchanged.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param list The list in which to swap elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param i the index of one element to be swapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param j the index of the other element to be swapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *         is out of range (i &lt; 0 || i &gt;= list.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *         || j &lt; 0 || j &gt;= list.size()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public static void swap(List<?> list, int i, int j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        final List l = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        l.set(i, l.set(j, l.get(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Swaps the two specified elements in the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private static void swap(Object[] arr, int i, int j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        Object tmp = arr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        arr[i] = arr[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        arr[j] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Replaces all of the elements of the specified list with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * element. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * This method runs in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param  list the list to be filled with the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param  obj The element with which to fill the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws UnsupportedOperationException if the specified list or its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *         list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public static <T> void fill(List<? super T> list, T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            for (int i=0; i<size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                list.set(i, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            ListIterator<? super T> itr = list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                itr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                itr.set(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Copies all of the elements from one list into another.  After the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * operation, the index of each copied element in the destination list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * will be identical to its index in the source list.  The destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * list must be at least as long as the source list.  If it is longer, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * remaining elements in the destination list are unaffected. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * This method runs in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param  dest The destination list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param  src The source list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @throws IndexOutOfBoundsException if the destination list is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *         to contain the entire source List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @throws UnsupportedOperationException if the destination list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *         list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public static <T> void copy(List<? super T> dest, List<? extends T> src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        int srcSize = src.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (srcSize > dest.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            throw new IndexOutOfBoundsException("Source does not fit in dest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (srcSize < COPY_THRESHOLD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            (src instanceof RandomAccess && dest instanceof RandomAccess)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            for (int i=0; i<srcSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                dest.set(i, src.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            ListIterator<? super T> di=dest.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            ListIterator<? extends T> si=src.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            for (int i=0; i<srcSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                di.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                di.set(si.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Returns the minimum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <i>natural ordering</i> of its elements.  All elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * collection must implement the <tt>Comparable</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Furthermore, all elements in the collection must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * <tt>e2</tt> in the collection).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param  coll the collection whose minimum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @return the minimum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *         to the <i>natural ordering</i> of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *         not <i>mutually comparable</i> (for example, strings and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *         integers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (next.compareTo(candidate) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Returns the minimum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * order induced by the specified comparator.  All elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * collection must be <i>mutually comparable</i> by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <tt>e2</tt> in the collection).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param  coll the collection whose minimum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param  comp the comparator with which to determine the minimum element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *         A <tt>null</tt> value indicates that the elements' <i>natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *         ordering</i> should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @return the minimum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *         to the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *         not <i>mutually comparable</i> using the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (comp==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return (T)min((Collection<SelfComparable>) (Collection) coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (comp.compare(next, candidate) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Returns the maximum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <i>natural ordering</i> of its elements.  All elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * collection must implement the <tt>Comparable</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Furthermore, all elements in the collection must be <i>mutually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <tt>e2</tt> in the collection).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param  coll the collection whose maximum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @return the maximum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *         to the <i>natural ordering</i> of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *         not <i>mutually comparable</i> (for example, strings and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *         integers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (next.compareTo(candidate) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Returns the maximum element of the given collection, according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * order induced by the specified comparator.  All elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * collection must be <i>mutually comparable</i> by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <tt>e2</tt> in the collection).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * This method iterates over the entire collection, hence it requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * time proportional to the size of the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @param  coll the collection whose maximum element is to be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @param  comp the comparator with which to determine the maximum element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *         A <tt>null</tt> value indicates that the elements' <i>natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *        ordering</i> should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @return the maximum element of the given collection, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *         to the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @throws ClassCastException if the collection contains elements that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *         not <i>mutually comparable</i> using the specified comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @throws NoSuchElementException if the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (comp==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            return (T)max((Collection<SelfComparable>) (Collection) coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        Iterator<? extends T> i = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        T candidate = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            T next = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (comp.compare(next, candidate) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                candidate = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Rotates the elements in the specified list by the specified distance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * After calling this method, the element at index <tt>i</tt> will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * the element previously at index <tt>(i - distance)</tt> mod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * <tt>list.size()</tt>, for all values of <tt>i</tt> between <tt>0</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * and <tt>list.size()-1</tt>, inclusive.  (This method has no effect on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * the size of the list.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * <p>For example, suppose <tt>list</tt> comprises<tt> [t, a, n, k, s]</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * After invoking <tt>Collections.rotate(list, 1)</tt> (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <tt>Collections.rotate(list, -4)</tt>), <tt>list</tt> will comprise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <tt>[s, t, a, n, k]</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <p>Note that this method can usefully be applied to sublists to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * move one or more elements within a list while preserving the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * order of the remaining elements.  For example, the following idiom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * moves the element at index <tt>j</tt> forward to position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <tt>k</tt> (which must be greater than or equal to <tt>j</tt>):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *     Collections.rotate(list.subList(j, k+1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * To make this concrete, suppose <tt>list</tt> comprises
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * <tt>[a, b, c, d, e]</tt>.  To move the element at index <tt>1</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * (<tt>b</tt>) forward two positions, perform the following invocation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *     Collections.rotate(l.subList(1, 4), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * The resulting list is <tt>[a, c, d, b, e]</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <p>To move more than one element forward, increase the absolute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * of the rotation distance.  To move elements backward, use a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * shift distance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <p>If the specified list is small or implements the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * RandomAccess} interface, this implementation exchanges the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * element into the location it should go, and then repeatedly exchanges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * the displaced element into the location it should go until a displaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * element is swapped into the first element.  If necessary, the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * is repeated on the second and successive elements, until the rotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * is complete.  If the specified list is large and doesn't implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * <tt>RandomAccess</tt> interface, this implementation breaks the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * list into two sublist views around index <tt>-distance mod size</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Then the {@link #reverse(List)} method is invoked on each sublist view,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * and finally it is invoked on the entire list.  For a more complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * description of both algorithms, see Section 2.3 of Jon Bentley's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * <i>Programming Pearls</i> (Addison-Wesley, 1986).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @param list the list to be rotated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @param distance the distance to rotate the list.  There are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *        constraints on this value; it may be zero, negative, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *        greater than <tt>list.size()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @throws UnsupportedOperationException if the specified list or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *         its list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public static void rotate(List<?> list, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            rotate1(list, distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            rotate2(list, distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    private static <T> void rotate1(List<T> list, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        distance = distance % size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (distance < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            distance += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (distance == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        for (int cycleStart = 0, nMoved = 0; nMoved != size; cycleStart++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            T displaced = list.get(cycleStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            int i = cycleStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                i += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                if (i >= size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    i -= size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                displaced = list.set(i, displaced);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                nMoved ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            } while(i != cycleStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private static void rotate2(List<?> list, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        int mid =  -distance % size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (mid < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            mid += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (mid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        reverse(list.subList(0, mid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        reverse(list.subList(mid, size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        reverse(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Replaces all occurrences of one specified value in a list with another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * More formally, replaces with <tt>newVal</tt> each element <tt>e</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * in <tt>list</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * <tt>(oldVal==null ? e==null : oldVal.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * (This method has no effect on the size of the list.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @param list the list in which replacement is to occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param oldVal the old value to be replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @param newVal the new value with which <tt>oldVal</tt> is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *        replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @return <tt>true</tt> if <tt>list</tt> contained one or more elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *         <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *         <tt>(oldVal==null ?  e==null : oldVal.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @throws UnsupportedOperationException if the specified list or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *         its list-iterator does not support the <tt>set</tt> operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (oldVal==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    if (list.get(i)==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        list.set(i, newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    if (oldVal.equals(list.get(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        list.set(i, newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            ListIterator<T> itr=list.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (oldVal==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    if (itr.next()==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        itr.set(newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                for (int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    if (oldVal.equals(itr.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        itr.set(newVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                        result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * Returns the starting position of the first occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * target list within the specified source list, or -1 if there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * such occurrence.  More formally, returns the lowest index <tt>i</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * such that <tt>source.subList(i, i+target.size()).equals(target)</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * or -1 if there is no such index.  (Returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * <tt>target.size() > source.size()</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * <p>This implementation uses the "brute force" technique of scanning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * over the source list, looking for a match with the target at each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * location in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @param source the list in which to search for the first occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *        of <tt>target</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @param target the list to search for as a subList of <tt>source</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @return the starting position of the first occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *         target list within the specified source list, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *         is no such occurrence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    public static int indexOfSubList(List<?> source, List<?> target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        int sourceSize = source.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        int targetSize = target.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        int maxCandidate = sourceSize - targetSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            (source instanceof RandomAccess&&target instanceof RandomAccess)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            for (int candidate = 0; candidate <= maxCandidate; candidate++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                for (int i=0, j=candidate; i<targetSize; i++, j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    if (!eq(target.get(i), source.get(j)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                        continue nextCand;  // Element mismatch, try next cand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                return candidate;  // All elements of candidate matched target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        } else {  // Iterator version of above algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            ListIterator<?> si = source.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            for (int candidate = 0; candidate <= maxCandidate; candidate++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                ListIterator<?> ti = target.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                for (int i=0; i<targetSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    if (!eq(ti.next(), si.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        // Back up source iterator to next candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                        for (int j=0; j<i; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                            si.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                        continue nextCand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        return -1;  // No candidate matched the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Returns the starting position of the last occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * target list within the specified source list, or -1 if there is no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * occurrence.  More formally, returns the highest index <tt>i</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * such that <tt>source.subList(i, i+target.size()).equals(target)</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * or -1 if there is no such index.  (Returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * <tt>target.size() > source.size()</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * <p>This implementation uses the "brute force" technique of iterating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * over the source list, looking for a match with the target at each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * location in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @param source the list in which to search for the last occurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *        of <tt>target</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @param target the list to search for as a subList of <tt>source</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @return the starting position of the last occurrence of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *         target list within the specified source list, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *         is no such occurrence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    public static int lastIndexOfSubList(List<?> source, List<?> target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        int sourceSize = source.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        int targetSize = target.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        int maxCandidate = sourceSize - targetSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            source instanceof RandomAccess) {   // Index access version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            for (int candidate = maxCandidate; candidate >= 0; candidate--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                for (int i=0, j=candidate; i<targetSize; i++, j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    if (!eq(target.get(i), source.get(j)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                        continue nextCand;  // Element mismatch, try next cand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                return candidate;  // All elements of candidate matched target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        } else {  // Iterator version of above algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            if (maxCandidate < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            ListIterator<?> si = source.listIterator(maxCandidate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        nextCand:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            for (int candidate = maxCandidate; candidate >= 0; candidate--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                ListIterator<?> ti = target.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                for (int i=0; i<targetSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    if (!eq(ti.next(), si.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                        if (candidate != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                            // Back up source iterator to next candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                            for (int j=0; j<=i+1; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                                si.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                        continue nextCand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return -1;  // No candidate matched the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    // Unmodifiable Wrappers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * Returns an unmodifiable view of the specified collection.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * collections.  Query operations on the returned collection "read through"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * to the specified collection, and attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * collection, whether direct or via its iterator, result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <tt>UnsupportedOperationException</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * The returned collection does <i>not</i> pass the hashCode and equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * operations through to the backing collection, but relies on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * <tt>Object</tt>'s <tt>equals</tt> and <tt>hashCode</tt> methods.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * is necessary to preserve the contracts of these operations in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * that the backing collection is a set or a list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * The returned collection will be serializable if the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @param  c the collection for which an unmodifiable view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *         returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @return an unmodifiable view of the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        return new UnmodifiableCollection<T>(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        private static final long serialVersionUID = 1820017752578914078L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        final Collection<? extends E> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        UnmodifiableCollection(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        public int size()                   {return c.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        public boolean isEmpty()            {return c.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        public boolean contains(Object o)   {return c.contains(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        public Object[] toArray()           {return c.toArray();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        public <T> T[] toArray(T[] a)       {return c.toArray(a);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        public String toString()            {return c.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            return new Iterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                private final Iterator<? extends E> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                public E next()          {return i.next();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            return c.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * Returns an unmodifiable view of the specified set.  This method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * modules to provide users with "read-only" access to internal sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * Query operations on the returned set "read through" to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * set, and attempts to modify the returned set, whether direct or via its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * iterator, result in an <tt>UnsupportedOperationException</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * The returned set will be serializable if the specified set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @param  s the set for which an unmodifiable view is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @return an unmodifiable view of the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        return new UnmodifiableSet<T>(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                                 implements Set<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        private static final long serialVersionUID = -9215047833775013803L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        UnmodifiableSet(Set<? extends E> s)     {super(s);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        public boolean equals(Object o) {return o == this || c.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        public int hashCode()           {return c.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * Returns an unmodifiable view of the specified sorted set.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * sorted sets.  Query operations on the returned sorted set "read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * through" to the specified sorted set.  Attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * sorted set, whether direct, via its iterator, or via its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * <tt>subSet</tt>, <tt>headSet</tt>, or <tt>tailSet</tt> views, result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * an <tt>UnsupportedOperationException</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * The returned sorted set will be serializable if the specified sorted set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @param s the sorted set for which an unmodifiable view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *        returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * @return an unmodifiable view of the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        return new UnmodifiableSortedSet<T>(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    static class UnmodifiableSortedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                             extends UnmodifiableSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                             implements SortedSet<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        private static final long serialVersionUID = -4929149591599911165L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        private final SortedSet<E> ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        public Comparator<? super E> comparator() {return ss.comparator();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        public SortedSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            return new UnmodifiableSortedSet<E>(ss.subSet(fromElement,toElement));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        public SortedSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            return new UnmodifiableSortedSet<E>(ss.headSet(toElement));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        public SortedSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            return new UnmodifiableSortedSet<E>(ss.tailSet(fromElement));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        public E first()                   {return ss.first();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        public E last()                    {return ss.last();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Returns an unmodifiable view of the specified list.  This method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * lists.  Query operations on the returned list "read through" to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * specified list, and attempts to modify the returned list, whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * direct or via its iterator, result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * <tt>UnsupportedOperationException</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * The returned list will be serializable if the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * is serializable. Similarly, the returned list will implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * {@link RandomAccess} if the specified list does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @param  list the list for which an unmodifiable view is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @return an unmodifiable view of the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    public static <T> List<T> unmodifiableList(List<? extends T> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        return (list instanceof RandomAccess ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                new UnmodifiableRandomAccessList<T>(list) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                new UnmodifiableList<T>(list));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    static class UnmodifiableList<E> extends UnmodifiableCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                                  implements List<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        private static final long serialVersionUID = -283967356065247728L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        final List<? extends E> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        UnmodifiableList(List<? extends E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        public boolean equals(Object o) {return o == this || list.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        public int hashCode()           {return list.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        public E get(int index) {return list.get(index);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        public int indexOf(Object o)            {return list.indexOf(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        public int lastIndexOf(Object o)        {return list.lastIndexOf(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        public ListIterator<E> listIterator()   {return listIterator(0);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            return new ListIterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                private final ListIterator<? extends E> i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                    = list.listIterator(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                public boolean hasNext()     {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                public E next()              {return i.next();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                public boolean hasPrevious() {return i.hasPrevious();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                public E previous()          {return i.previous();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                public int nextIndex()       {return i.nextIndex();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                public int previousIndex()   {return i.previousIndex();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            return new UnmodifiableList<E>(list.subList(fromIndex, toIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
         * UnmodifiableRandomAccessList instances are serialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
         * UnmodifiableList instances to allow them to be deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
         * in pre-1.4 JREs (which do not have UnmodifiableRandomAccessList).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
         * This method inverts the transformation.  As a beneficial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
         * side-effect, it also grafts the RandomAccess marker onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
         * UnmodifiableList instances that were serialized in pre-1.4 JREs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         * Note: Unfortunately, UnmodifiableRandomAccessList instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         * serialized in 1.4.1 and deserialized in 1.4 will become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
         * UnmodifiableList instances, as this method was missing in 1.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return (list instanceof RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    ? new UnmodifiableRandomAccessList<E>(list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    : this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                                              implements RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        UnmodifiableRandomAccessList(List<? extends E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            return new UnmodifiableRandomAccessList<E>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                list.subList(fromIndex, toIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        private static final long serialVersionUID = -2542308836966382001L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
         * Allows instances to be deserialized in pre-1.4 JREs (which do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         * not have UnmodifiableRandomAccessList).  UnmodifiableList has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         * a readResolve method that inverts this transformation upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
         * deserialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        private Object writeReplace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            return new UnmodifiableList<E>(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * Returns an unmodifiable view of the specified map.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * maps.  Query operations on the returned map "read through"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * to the specified map, and attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * map, whether direct or via its collection views, result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * <tt>UnsupportedOperationException</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * The returned map will be serializable if the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * @param  m the map for which an unmodifiable view is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @return an unmodifiable view of the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        return new UnmodifiableMap<K,V>(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        private static final long serialVersionUID = -1034234728574286014L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        private final Map<? extends K, ? extends V> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        UnmodifiableMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            if (m==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        public int size()                        {return m.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        public boolean isEmpty()                 {return m.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        public boolean containsKey(Object key)   {return m.containsKey(key);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        public boolean containsValue(Object val) {return m.containsValue(val);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        public V get(Object key)                 {return m.get(key);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        private transient Set<K> keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        private transient Collection<V> values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            if (keySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                keySet = unmodifiableSet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            if (entrySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                entrySet = new UnmodifiableEntrySet<K,V>(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            if (values==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                values = unmodifiableCollection(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        public boolean equals(Object o) {return o == this || m.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        public int hashCode()           {return m.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        public String toString()        {return m.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
         * We need this class in addition to UnmodifiableSet as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         * Map.Entries themselves permit modification of the backing Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         * via their setValue operation.  This class is subtle: there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
         * many possible attacks that must be thwarted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
         * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        static class UnmodifiableEntrySet<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            extends UnmodifiableSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            private static final long serialVersionUID = 7854390611657943733L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                super((Set)s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                return new Iterator<Map.Entry<K,V>>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                    public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                        return i.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        return new UnmodifiableEntry<K,V>(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                for (int i=0; i<a.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    a[i] = new UnmodifiableEntry<K,V>((Map.Entry<K,V>)a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                // We don't pass a to c.toArray, to avoid window of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                // vulnerability wherein an unscrupulous multithreaded client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                // could get his hands on raw (unwrapped) Entries from c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                for (int i=0; i<arr.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    arr[i] = new UnmodifiableEntry<K,V>((Map.Entry<K,V>)arr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                if (arr.length > a.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    return (T[])arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                System.arraycopy(arr, 0, a, 0, arr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                if (a.length > arr.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    a[arr.length] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
             * This method is overridden to protect the backing set against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
             * an object with a nefarious equals function that senses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
             * that the equality-candidate is Map.Entry and calls its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
             * setValue method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                return c.contains(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    new UnmodifiableEntry<Object,Object>((Map.Entry<?,?>) o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
             * The next two methods are overridden to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
             * an unscrupulous List whose contains(Object o) method senses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
             * when o is a Map.Entry, and calls o.setValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                Iterator<?> e = coll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                while (e.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                    if (!contains(e.next())) // Invokes safe contains() above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                Set s = (Set) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                if (s.size() != c.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                return containsAll(s); // Invokes safe containsAll() above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
             * This "wrapper class" serves two purposes: it prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
             * the client from modifying the backing Map, by short-circuiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
             * the setValue method, and it protects the backing Map against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
             * an ill-behaved Map.Entry that attempts to modify another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
             * Map Entry when asked to perform an equality check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            private static class UnmodifiableEntry<K,V> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                private Map.Entry<? extends K, ? extends V> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                UnmodifiableEntry(Map.Entry<? extends K, ? extends V> e) {this.e = e;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                public K getKey()         {return e.getKey();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                public V getValue()  {return e.getValue();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                public int hashCode()     {return e.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                    if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    Map.Entry t = (Map.Entry)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                    return eq(e.getKey(),   t.getKey()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                           eq(e.getValue(), t.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                public String toString()  {return e.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * Returns an unmodifiable view of the specified sorted map.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * allows modules to provide users with "read-only" access to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * sorted maps.  Query operations on the returned sorted map "read through"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * to the specified sorted map.  Attempts to modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * sorted map, whether direct, via its collection views, or via its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * <tt>subMap</tt>, <tt>headMap</tt>, or <tt>tailMap</tt> views, result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * an <tt>UnsupportedOperationException</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * The returned sorted map will be serializable if the specified sorted map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * @param m the sorted map for which an unmodifiable view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     *        returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * @return an unmodifiable view of the specified sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        return new UnmodifiableSortedMap<K,V>(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    static class UnmodifiableSortedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
          extends UnmodifiableMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
          implements SortedMap<K,V>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        private static final long serialVersionUID = -8806743815996713206L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        private final SortedMap<K, ? extends V> sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        public Comparator<? super K> comparator() {return sm.comparator();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        public SortedMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            return new UnmodifiableSortedMap<K,V>(sm.subMap(fromKey, toKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        public SortedMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            return new UnmodifiableSortedMap<K,V>(sm.headMap(toKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        public SortedMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            return new UnmodifiableSortedMap<K,V>(sm.tailMap(fromKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        public K firstKey()           {return sm.firstKey();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        public K lastKey()            {return sm.lastKey();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    // Synch Wrappers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * Returns a synchronized (thread-safe) collection backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * collection.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * <strong>all</strong> access to the backing collection is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * through the returned collection.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * It is imperative that the user manually synchronize on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * collection when iterating over it:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *  Collection c = Collections.synchronizedCollection(myCollection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     *  synchronized(c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     *      Iterator i = c.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     *         foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * <p>The returned collection does <i>not</i> pass the <tt>hashCode</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * and <tt>equals</tt> operations through to the backing collection, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * relies on <tt>Object</tt>'s equals and hashCode methods.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * necessary to preserve the contracts of these operations in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * that the backing collection is a set or a list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * The returned collection will be serializable if the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * @param  c the collection to be "wrapped" in a synchronized collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * @return a synchronized view of the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        return new SynchronizedCollection<T>(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        return new SynchronizedCollection<T>(c, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    static class SynchronizedCollection<E> implements Collection<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        private static final long serialVersionUID = 3053995032091335093L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        final Collection<E> c;  // Backing Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        final Object mutex;     // Object on which to synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        SynchronizedCollection(Collection<E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            if (c==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            mutex = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        SynchronizedCollection(Collection<E> c, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            this.mutex = mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            synchronized(mutex) {return c.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            synchronized(mutex) {return c.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            synchronized(mutex) {return c.contains(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            synchronized(mutex) {return c.toArray();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            synchronized(mutex) {return c.toArray(a);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            return c.iterator(); // Must be manually synched by user!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            synchronized(mutex) {return c.add(e);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            synchronized(mutex) {return c.remove(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            synchronized(mutex) {return c.containsAll(coll);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            synchronized(mutex) {return c.addAll(coll);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            synchronized(mutex) {return c.removeAll(coll);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            synchronized(mutex) {return c.retainAll(coll);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            synchronized(mutex) {c.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            synchronized(mutex) {return c.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            synchronized(mutex) {s.defaultWriteObject();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * Returns a synchronized (thread-safe) set backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * set.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     * <strong>all</strong> access to the backing set is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * through the returned set.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * It is imperative that the user manually synchronize on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * set when iterating over it:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     *  Set s = Collections.synchronizedSet(new HashSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     *  synchronized(s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     *      Iterator i = s.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * <p>The returned set will be serializable if the specified set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * @param  s the set to be "wrapped" in a synchronized set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * @return a synchronized view of the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    public static <T> Set<T> synchronizedSet(Set<T> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        return new SynchronizedSet<T>(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        return new SynchronizedSet<T>(s, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    static class SynchronizedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
          extends SynchronizedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
          implements Set<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        private static final long serialVersionUID = 487447009682186044L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        SynchronizedSet(Set<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        SynchronizedSet(Set<E> s, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            super(s, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            synchronized(mutex) {return c.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            synchronized(mutex) {return c.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * Returns a synchronized (thread-safe) sorted set backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * sorted set.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     * <strong>all</strong> access to the backing sorted set is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * through the returned sorted set (or its views).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * It is imperative that the user manually synchronize on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * sorted set when iterating over it or any of its <tt>subSet</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * <tt>headSet</tt>, or <tt>tailSet</tt> views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     *  synchronized(s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     *      Iterator i = s.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * or:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     *  SortedSet s2 = s.headSet(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     *  synchronized(s) {  // Note: s, not s2!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     *      Iterator i = s2.iterator(); // Must be in the synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * <p>The returned sorted set will be serializable if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * sorted set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * @param  s the sorted set to be "wrapped" in a synchronized sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * @return a synchronized view of the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        return new SynchronizedSortedSet<T>(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    static class SynchronizedSortedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        extends SynchronizedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        implements SortedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        private static final long serialVersionUID = 8695801310862127406L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        final private SortedSet<E> ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        SynchronizedSortedSet(SortedSet<E> s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            ss = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        SynchronizedSortedSet(SortedSet<E> s, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            super(s, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            ss = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        public Comparator<? super E> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            synchronized(mutex) {return ss.comparator();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        public SortedSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                return new SynchronizedSortedSet<E>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                    ss.subSet(fromElement, toElement), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        public SortedSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                return new SynchronizedSortedSet<E>(ss.headSet(toElement), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        public SortedSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
               return new SynchronizedSortedSet<E>(ss.tailSet(fromElement),mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        public E first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            synchronized(mutex) {return ss.first();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        public E last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            synchronized(mutex) {return ss.last();}
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * Returns a synchronized (thread-safe) list backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * list.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * <strong>all</strong> access to the backing list is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * through the returned list.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * It is imperative that the user manually synchronize on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * list when iterating over it:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     *  List list = Collections.synchronizedList(new ArrayList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     *  synchronized(list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     *      Iterator i = list.iterator(); // Must be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * <p>The returned list will be serializable if the specified list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * @param  list the list to be "wrapped" in a synchronized list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * @return a synchronized view of the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    public static <T> List<T> synchronizedList(List<T> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        return (list instanceof RandomAccess ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                new SynchronizedRandomAccessList<T>(list) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                new SynchronizedList<T>(list));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    static <T> List<T> synchronizedList(List<T> list, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        return (list instanceof RandomAccess ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                new SynchronizedRandomAccessList<T>(list, mutex) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                new SynchronizedList<T>(list, mutex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    static class SynchronizedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        extends SynchronizedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        implements List<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        private static final long serialVersionUID = -7754090372962971524L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        final List<E> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        SynchronizedList(List<E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        SynchronizedList(List<E> list, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            super(list, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            synchronized(mutex) {return list.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            synchronized(mutex) {return list.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            synchronized(mutex) {return list.get(index);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            synchronized(mutex) {return list.set(index, element);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            synchronized(mutex) {list.add(index, element);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            synchronized(mutex) {return list.remove(index);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            synchronized(mutex) {return list.indexOf(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            synchronized(mutex) {return list.lastIndexOf(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            synchronized(mutex) {return list.addAll(index, c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            return list.listIterator(); // Must be manually synched by user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        public ListIterator<E> listIterator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            return list.listIterator(index); // Must be manually synched by user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                return new SynchronizedList<E>(list.subList(fromIndex, toIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                                            mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
         * SynchronizedRandomAccessList instances are serialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         * SynchronizedList instances to allow them to be deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
         * in pre-1.4 JREs (which do not have SynchronizedRandomAccessList).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
         * This method inverts the transformation.  As a beneficial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
         * side-effect, it also grafts the RandomAccess marker onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
         * SynchronizedList instances that were serialized in pre-1.4 JREs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
         * Note: Unfortunately, SynchronizedRandomAccessList instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
         * serialized in 1.4.1 and deserialized in 1.4 will become
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
         * SynchronizedList instances, as this method was missing in 1.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            return (list instanceof RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                    ? new SynchronizedRandomAccessList<E>(list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    : this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    static class SynchronizedRandomAccessList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        extends SynchronizedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        implements RandomAccess {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        SynchronizedRandomAccessList(List<E> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            super(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        SynchronizedRandomAccessList(List<E> list, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            super(list, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                return new SynchronizedRandomAccessList<E>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                    list.subList(fromIndex, toIndex), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        private static final long serialVersionUID = 1530674583602358482L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
         * Allows instances to be deserialized in pre-1.4 JREs (which do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
         * not have SynchronizedRandomAccessList).  SynchronizedList has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
         * a readResolve method that inverts this transformation upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
         * deserialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        private Object writeReplace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            return new SynchronizedList<E>(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * Returns a synchronized (thread-safe) map backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * map.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * <strong>all</strong> access to the backing map is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * through the returned map.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * It is imperative that the user manually synchronize on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * map when iterating over any of its collection views:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *  Map m = Collections.synchronizedMap(new HashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     *  Set s = m.keySet();  // Needn't be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     *  synchronized(m) {  // Synchronizing on m, not s!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     *      Iterator i = s.iterator(); // Must be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * <p>The returned map will be serializable if the specified map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * @param  m the map to be "wrapped" in a synchronized map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * @return a synchronized view of the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        return new SynchronizedMap<K,V>(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    private static class SynchronizedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        implements Map<K,V>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        private static final long serialVersionUID = 1978198479659022715L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        private final Map<K,V> m;     // Backing Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        final Object      mutex;        // Object on which to synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        SynchronizedMap(Map<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            if (m==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            mutex = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        SynchronizedMap(Map<K,V> m, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            this.mutex = mutex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            synchronized(mutex) {return m.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            synchronized(mutex) {return m.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            synchronized(mutex) {return m.containsKey(key);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            synchronized(mutex) {return m.containsValue(value);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            synchronized(mutex) {return m.get(key);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            synchronized(mutex) {return m.put(key, value);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            synchronized(mutex) {return m.remove(key);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        public void putAll(Map<? extends K, ? extends V> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            synchronized(mutex) {m.putAll(map);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            synchronized(mutex) {m.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        private transient Set<K> keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        private transient Collection<V> values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                if (keySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                    keySet = new SynchronizedSet<K>(m.keySet(), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                if (entrySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    entrySet = new SynchronizedSet<Map.Entry<K,V>>(m.entrySet(), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                if (values==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                    values = new SynchronizedCollection<V>(m.values(), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            synchronized(mutex) {return m.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            synchronized(mutex) {return m.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            synchronized(mutex) {return m.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            synchronized(mutex) {s.defaultWriteObject();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * Returns a synchronized (thread-safe) sorted map backed by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * sorted map.  In order to guarantee serial access, it is critical that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * <strong>all</strong> access to the backing sorted map is accomplished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * through the returned sorted map (or its views).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * It is imperative that the user manually synchronize on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * sorted map when iterating over any of its collection views, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * collections views of any of its <tt>subMap</tt>, <tt>headMap</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * <tt>tailMap</tt> views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     *  SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *  Set s = m.keySet();  // Needn't be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     *  synchronized(m) {  // Synchronizing on m, not s!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     *      Iterator i = s.iterator(); // Must be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * or:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     *  SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     *  SortedMap m2 = m.subMap(foo, bar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     *  Set s2 = m2.keySet();  // Needn't be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     *      ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     *  synchronized(m) {  // Synchronizing on m, not m2 or s2!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     *      Iterator i = s.iterator(); // Must be in synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     *      while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     *          foo(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * Failure to follow this advice may result in non-deterministic behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     * <p>The returned sorted map will be serializable if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * sorted map is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * @param  m the sorted map to be "wrapped" in a synchronized sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * @return a synchronized view of the specified sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        return new SynchronizedSortedMap<K,V>(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    static class SynchronizedSortedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        extends SynchronizedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        implements SortedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        private static final long serialVersionUID = -8798146769416483793L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        private final SortedMap<K,V> sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        SynchronizedSortedMap(SortedMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            super(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            sm = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        SynchronizedSortedMap(SortedMap<K,V> m, Object mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            super(m, mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            sm = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        public Comparator<? super K> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            synchronized(mutex) {return sm.comparator();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        public SortedMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                return new SynchronizedSortedMap<K,V>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                    sm.subMap(fromKey, toKey), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        public SortedMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                return new SynchronizedSortedMap<K,V>(sm.headMap(toKey), mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        public SortedMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            synchronized(mutex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
               return new SynchronizedSortedMap<K,V>(sm.tailMap(fromKey),mutex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            }
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 K firstKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            synchronized(mutex) {return sm.firstKey();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        public K lastKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            synchronized(mutex) {return sm.lastKey();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    // Dynamically typesafe collection wrappers
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 dynamically typesafe view of the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * Any attempt to insert an element of the wrong type will result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * immediate {@link ClassCastException}.  Assuming a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * contains no incorrectly typed elements prior to the time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * dynamically typesafe view is generated, and that all subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * access to the collection takes place through the view, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * <i>guaranteed</i> that the collection cannot contain an incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * <p>The generics mechanism in the language provides compile-time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * (static) type checking, but it is possible to defeat this mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * with unchecked casts.  Usually this is not a problem, as the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * issues warnings on all such unchecked operations.  There are, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * times when static type checking alone is not sufficient.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * suppose a collection is passed to a third-party library and it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * imperative that the library code not corrupt the collection by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * inserting an element of the wrong type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * <p>Another use of dynamically typesafe views is debugging.  Suppose a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * program fails with a {@code ClassCastException}, indicating that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * incorrectly typed element was put into a parameterized collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * Unfortunately, the exception can occur at any time after the erroneous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * element is inserted, so it typically provides little or no information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * as to the real source of the problem.  If the problem is reproducible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * one can quickly determine its source by temporarily modifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * program to wrap the collection with a dynamically typesafe view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * For example, this declaration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     *     Collection<String> c = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * may be replaced temporarily by this one:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     *     Collection<String> c = Collections.checkedCollection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     *         new HashSet<String>(), String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * Running the program again will cause it to fail at the point where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * an incorrectly typed element is inserted into the collection, clearly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * identifying the source of the problem.  Once the problem is fixed, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * modified declaration may be reverted back to the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * <p>The returned collection does <i>not</i> pass the hashCode and equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * operations through to the backing collection, but relies on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * {@code Object}'s {@code equals} and {@code hashCode} methods.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * is necessary to preserve the contracts of these operations in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * that the backing collection is a set or a list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * <p>The returned collection will be serializable if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * collection is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * type, the returned collection permits insertion of null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     * whenever the backing collection does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * @param c the collection for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * @param type the type of element that {@code c} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * @return a dynamically typesafe view of the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    public static <E> Collection<E> checkedCollection(Collection<E> c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                                                      Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        return new CheckedCollection<E>(c, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
    static <T> T[] zeroLengthArray(Class<T> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        return (T[]) Array.newInstance(type, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    static class CheckedCollection<E> implements Collection<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        private static final long serialVersionUID = 1578914078182001775L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        final Collection<E> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        final Class<E> type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        void typeCheck(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            if (o != null && !type.isInstance(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                throw new ClassCastException(badElementMsg(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        private String badElementMsg(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            return "Attempt to insert " + o.getClass() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                " element into collection with element type " + type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        CheckedCollection(Collection<E> c, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            if (c==null || type == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        public int size()                 { return c.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
        public boolean isEmpty()          { return c.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        public boolean contains(Object o) { return c.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        public Object[] toArray()         { return c.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
        public <T> T[] toArray(T[] a)     { return c.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        public String toString()          { return c.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
        public boolean remove(Object o)   { return c.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        public void clear()               {        c.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        public boolean containsAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            return c.containsAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        public boolean removeAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            return c.removeAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        public boolean retainAll(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            return c.retainAll(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            final Iterator<E> it = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            return new Iterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                public boolean hasNext() { return it.hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                public E next()          { return it.next(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                public void remove()     {        it.remove(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            typeCheck(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            return c.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        private E[] zeroLengthElementArray = null; // Lazily initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        private E[] zeroLengthElementArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            return zeroLengthElementArray != null ? zeroLengthElementArray :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                (zeroLengthElementArray = zeroLengthArray(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        Collection<E> checkedCopyOf(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            Object[] a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                E[] z = zeroLengthElementArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                a = coll.toArray(z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                // Defend against coll violating the toArray contract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                if (a.getClass() != z.getClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                    a = Arrays.copyOf(a, a.length, z.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            } catch (ArrayStoreException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                // To get better and consistent diagnostics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                // we call typeCheck explicitly on each element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                // We call clone() to defend against coll retaining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                // reference to the returned array and storing a bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                // element into it after it has been type checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                a = coll.toArray().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                for (Object o : a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                    typeCheck(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            // A slight abuse of the type system, but safe here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            return (Collection<E>) Arrays.asList(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        public boolean addAll(Collection<? extends E> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            // Doing things this way insulates us from concurrent changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            // in the contents of coll and provides all-or-nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            // semantics (which we wouldn't get if we type-checked each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
            // element as we added it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            return c.addAll(checkedCopyOf(coll));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * Returns a dynamically typesafe view of the specified set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * Any attempt to insert an element of the wrong type will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * an immediate {@link ClassCastException}.  Assuming a set contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * no incorrectly typed elements prior to the time a dynamically typesafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * view is generated, and that all subsequent access to the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * takes place through the view, it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     * set cannot contain an incorrectly typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * <p>The returned set will be serializable if the specified set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * type, the returned set permits insertion of null elements whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * the backing set does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @param s the set for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     * @param type the type of element that {@code s} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     * @return a dynamically typesafe view of the specified set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
    public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        return new CheckedSet<E>(s, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
    static class CheckedSet<E> extends CheckedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                                 implements Set<E>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        private static final long serialVersionUID = 4694047833775013803L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        CheckedSet(Set<E> s, Class<E> elementType) { super(s, elementType); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        public boolean equals(Object o) { return o == this || c.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        public int hashCode()           { return c.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * Returns a dynamically typesafe view of the specified sorted set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * Any attempt to insert an element of the wrong type will result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * immediate {@link ClassCastException}.  Assuming a sorted set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * contains no incorrectly typed elements prior to the time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * dynamically typesafe view is generated, and that all subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * access to the sorted set takes place through the view, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * <i>guaranteed</i> that the sorted set cannot contain an incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     * <p>The returned sorted set will be serializable if the specified sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * type, the returned sorted set permits insertion of null elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * whenever the backing sorted set does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * @param s the sorted set for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     * @param type the type of element that {@code s} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * @return a dynamically typesafe view of the specified sorted set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                                                    Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        return new CheckedSortedSet<E>(s, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    static class CheckedSortedSet<E> extends CheckedSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        implements SortedSet<E>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        private static final long serialVersionUID = 1599911165492914959L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        private final SortedSet<E> ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        CheckedSortedSet(SortedSet<E> s, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            super(s, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            ss = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        public Comparator<? super E> comparator() { return ss.comparator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        public E first()                   { return ss.first(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        public E last()                    { return ss.last(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        public SortedSet<E> subSet(E fromElement, E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            return checkedSortedSet(ss.subSet(fromElement,toElement), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        public SortedSet<E> headSet(E toElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            return checkedSortedSet(ss.headSet(toElement), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        public SortedSet<E> tailSet(E fromElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            return checkedSortedSet(ss.tailSet(fromElement), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     * Returns a dynamically typesafe view of the specified list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     * Any attempt to insert an element of the wrong type will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * an immediate {@link ClassCastException}.  Assuming a list contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * no incorrectly typed elements prior to the time a dynamically typesafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * view is generated, and that all subsequent access to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     * takes place through the view, it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * list cannot contain an incorrectly typed element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * <p>The returned list will be serializable if the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * type, the returned list permits insertion of null elements whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * the backing list does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * @param list the list for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     *             returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     * @param type the type of element that {@code list} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
     * @return a dynamically typesafe view of the specified list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
    public static <E> List<E> checkedList(List<E> list, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        return (list instanceof RandomAccess ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                new CheckedRandomAccessList<E>(list, type) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                new CheckedList<E>(list, type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    static class CheckedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        extends CheckedCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
        implements List<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        private static final long serialVersionUID = 65247728283967356L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        final List<E> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        CheckedList(List<E> list, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            super(list, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        public boolean equals(Object o)  { return o == this || list.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
        public int hashCode()            { return list.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        public E get(int index)          { return list.get(index); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
        public E remove(int index)       { return list.remove(index); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        public int indexOf(Object o)     { return list.indexOf(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
        public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            typeCheck(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            return list.set(index, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
            typeCheck(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            list.add(index, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            return list.addAll(index, checkedCopyOf(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        public ListIterator<E> listIterator()   { return listIterator(0); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        public ListIterator<E> listIterator(final int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            final ListIterator<E> i = list.listIterator(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            return new ListIterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                public boolean hasNext()     { return i.hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                public E next()              { return i.next(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                public boolean hasPrevious() { return i.hasPrevious(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                public E previous()          { return i.previous(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                public int nextIndex()       { return i.nextIndex(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                public int previousIndex()   { return i.previousIndex(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                public void remove()         {        i.remove(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                    typeCheck(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                    i.set(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                    typeCheck(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                    i.add(e);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
            return new CheckedList<E>(list.subList(fromIndex, toIndex), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    static class CheckedRandomAccessList<E> extends CheckedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                                            implements RandomAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        private static final long serialVersionUID = 1638200125423088369L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        CheckedRandomAccessList(List<E> list, Class<E> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
            super(list, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        public List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            return new CheckedRandomAccessList<E>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                list.subList(fromIndex, toIndex), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * Returns a dynamically typesafe view of the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     * Any attempt to insert a mapping whose key or value have the wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
     * type will result in an immediate {@link ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
     * Similarly, any attempt to modify the value currently associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
     * a key will result in an immediate {@link ClassCastException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
     * whether the modification is attempted directly through the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     * itself, or through a {@link Map.Entry} instance obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     * map's {@link Map#entrySet() entry set} view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
     * <p>Assuming a map contains no incorrectly typed keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
     * prior to the time a dynamically typesafe view is generated, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
     * that all subsequent access to the map takes place through the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
     * (or one of its collection views), it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
     * map cannot contain an incorrectly typed key or value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
     * <p>The returned map will be serializable if the specified map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     * type, the returned map permits insertion of null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     * whenever the backing map does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     * @param m the map for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     * @param keyType the type of key that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * @param valueType the type of value that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     * @return a dynamically typesafe view of the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
    public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                                              Class<K> keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                                              Class<V> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        return new CheckedMap<K,V>(m, keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
    private static class CheckedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        implements Map<K,V>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        private static final long serialVersionUID = 5742860141034234728L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        private final Map<K, V> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
        final Class<K> keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        final Class<V> valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        private void typeCheck(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            if (key != null && !keyType.isInstance(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                throw new ClassCastException(badKeyMsg(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
            if (value != null && !valueType.isInstance(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                throw new ClassCastException(badValueMsg(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        private String badKeyMsg(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
            return "Attempt to insert " + key.getClass() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                " key into map with key type " + keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        private String badValueMsg(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            return "Attempt to insert " + value.getClass() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                " value into map with value type " + valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
        CheckedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
            if (m == null || keyType == null || valueType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            this.m = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            this.keyType = keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            this.valueType = valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        public int size()                      { return m.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        public boolean isEmpty()               { return m.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        public boolean containsKey(Object key) { return m.containsKey(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        public boolean containsValue(Object v) { return m.containsValue(v); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        public V get(Object key)               { return m.get(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
        public V remove(Object key)            { return m.remove(key); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        public void clear()                    { m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        public Set<K> keySet()                 { return m.keySet(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        public Collection<V> values()          { return m.values(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
        public boolean equals(Object o)        { return o == this || m.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        public int hashCode()                  { return m.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        public String toString()               { return m.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
            typeCheck(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            return m.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        public void putAll(Map<? extends K, ? extends V> t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            // Satisfy the following goals:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            // - good diagnostics in case of type mismatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            // - all-or-nothing semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            // - protection from malicious t
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            // - correct behavior if t is a concurrent map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
            Object[] entries = t.entrySet().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            List<Map.Entry<K,V>> checked =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                new ArrayList<Map.Entry<K,V>>(entries.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            for (Object o : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                Object k = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                Object v = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                typeCheck(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                checked.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                    new AbstractMap.SimpleImmutableEntry<K,V>((K) k, (V) v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
            for (Map.Entry<K,V> e : checked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                m.put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            if (entrySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                entrySet = new CheckedEntrySet<K,V>(m.entrySet(), valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
            return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
         * We need this class in addition to CheckedSet as Map.Entry permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
         * modification of the backing Map via the setValue operation.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
         * class is subtle: there are many possible attacks that must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
         * thwarted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
         * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        static class CheckedEntrySet<K,V> implements Set<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
            private final Set<Map.Entry<K,V>> s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
            private final Class<V> valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            CheckedEntrySet(Set<Map.Entry<K, V>> s, Class<V> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                this.valueType = valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            public int size()        { return s.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            public boolean isEmpty() { return s.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
            public String toString() { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
            public int hashCode()    { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
            public void clear()      {        s.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            public boolean add(Map.Entry<K, V> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
            public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                final Iterator<Map.Entry<K, V>> i = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                final Class<V> valueType = this.valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                return new Iterator<Map.Entry<K,V>>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                    public boolean hasNext() { return i.hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                    public void remove()     { i.remove(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                    public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                        return checkedEntry(i.next(), valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
            @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
            public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                Object[] source = s.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
                 * Ensure that we don't get an ArrayStoreException even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
                 * s.toArray returns an array of something other than Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                Object[] dest = (CheckedEntry.class.isInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                    source.getClass().getComponentType()) ? source :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                                 new Object[source.length]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                for (int i = 0; i < source.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                    dest[i] = checkedEntry((Map.Entry<K,V>)source[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                                           valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                // We don't pass a to s.toArray, to avoid window of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                // vulnerability wherein an unscrupulous multithreaded client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                // could get his hands on raw (unwrapped) Entries from s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                T[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                for (int i=0; i<arr.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                    arr[i] = (T) checkedEntry((Map.Entry<K,V>)arr[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                                              valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                if (arr.length > a.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                    return arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                System.arraycopy(arr, 0, a, 0, arr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                if (a.length > arr.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                    a[arr.length] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
             * This method is overridden to protect the backing set against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
             * an object with a nefarious equals function that senses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
             * that the equality-candidate is Map.Entry and calls its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
             * setValue method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
            public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                return s.contains(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                    (e instanceof CheckedEntry) ? e : checkedEntry(e, valueType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
             * The bulk collection methods are overridden to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
             * against an unscrupulous collection whose contains(Object o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
             * method senses when o is a Map.Entry, and calls o.setValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
            public boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                for (Object o : c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                    if (!contains(o)) // Invokes safe contains() above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                return s.remove(new AbstractMap.SimpleImmutableEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                                <Object, Object>((Map.Entry<?,?>)o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
            public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                return batchRemove(c, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
            public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                return batchRemove(c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
            private boolean batchRemove(Collection<?> c, boolean complement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
                boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                Iterator<Map.Entry<K,V>> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                    if (c.contains(it.next()) != complement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                        modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
            public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                Set<?> that = (Set<?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                return that.size() == s.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                    && containsAll(that); // Invokes safe containsAll() above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
            static <K,V,T> CheckedEntry<K,V,T> checkedEntry(Map.Entry<K,V> e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                                                            Class<T> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                return new CheckedEntry<K,V,T>(e, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
             * This "wrapper class" serves two purposes: it prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
             * the client from modifying the backing Map, by short-circuiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
             * the setValue method, and it protects the backing Map against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
             * an ill-behaved Map.Entry that attempts to modify another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
             * Map.Entry when asked to perform an equality check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            private static class CheckedEntry<K,V,T> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                private final Map.Entry<K, V> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                private final Class<T> valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                CheckedEntry(Map.Entry<K, V> e, Class<T> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                    this.e = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                    this.valueType = valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                public K getKey()        { return e.getKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                public V getValue()      { return e.getValue(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                public int hashCode()    { return e.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                public String toString() { return e.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                public V setValue(V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
                    if (value != null && !valueType.isInstance(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
                        throw new ClassCastException(badValueMsg(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                    return e.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                private String badValueMsg(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                    return "Attempt to insert " + value.getClass() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                        " value into map with value type " + valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                    if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
                    if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
                    return e.equals(new AbstractMap.SimpleImmutableEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
                                    <Object, Object>((Map.Entry<?,?>)o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * Returns a dynamically typesafe view of the specified sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * Any attempt to insert a mapping whose key or value have the wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * type will result in an immediate {@link ClassCastException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * Similarly, any attempt to modify the value currently associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     * a key will result in an immediate {@link ClassCastException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * whether the modification is attempted directly through the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     * itself, or through a {@link Map.Entry} instance obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * map's {@link Map#entrySet() entry set} view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
     * <p>Assuming a map contains no incorrectly typed keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
     * prior to the time a dynamically typesafe view is generated, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
     * that all subsequent access to the map takes place through the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
     * (or one of its collection views), it is <i>guaranteed</i> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
     * map cannot contain an incorrectly typed key or value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
     * <p>A discussion of the use of dynamically typesafe views may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
     * found in the documentation for the {@link #checkedCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
     * checkedCollection} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
     * <p>The returned map will be serializable if the specified map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
     * <p>Since {@code null} is considered to be a value of any reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * type, the returned map permits insertion of null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     * whenever the backing map does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     * @param m the map for which a dynamically typesafe view is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     *          returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     * @param keyType the type of key that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
     * @param valueType the type of value that {@code m} is permitted to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * @return a dynamically typesafe view of the specified map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
    public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
                                                        Class<K> keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                                                        Class<V> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
        return new CheckedSortedMap<K,V>(m, keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
    static class CheckedSortedMap<K,V> extends CheckedMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
        implements SortedMap<K,V>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
        private static final long serialVersionUID = 1599671320688067438L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        private final SortedMap<K, V> sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        CheckedSortedMap(SortedMap<K, V> m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                         Class<K> keyType, Class<V> valueType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
            super(m, keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            sm = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
        public Comparator<? super K> comparator() { return sm.comparator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
        public K firstKey()                       { return sm.firstKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
        public K lastKey()                        { return sm.lastKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
        public SortedMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
            return checkedSortedMap(sm.subMap(fromKey, toKey),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                                    keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
        public SortedMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
            return checkedSortedMap(sm.headMap(toKey), keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
        public SortedMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
            return checkedSortedMap(sm.tailMap(fromKey), keyType, valueType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
    // Empty collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     * Returns an iterator that has no elements.  More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     * <ul compact>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     * <li>{@link Iterator#hasNext hasNext} always returns {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     * false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
     * <li>{@link Iterator#next next} always throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
     * NoSuchElementException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
     * <li>{@link Iterator#remove remove} always throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
     * IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
     * <p>Implementations of this method are permitted, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
     * required, to return the same object from multiple invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
     * @return an empty iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    public static <T> Iterator<T> emptyIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
        return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
    private static class EmptyIterator<E> implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
        static final EmptyIterator<Object> EMPTY_ITERATOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
            = new EmptyIterator<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
        public boolean hasNext() { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        public E next() { throw new NoSuchElementException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
        public void remove() { throw new IllegalStateException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
    }
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 list iterator that has no elements.  More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
     * <ul compact>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     * <li>{@link Iterator#hasNext hasNext} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
     * ListIterator#hasPrevious hasPrevious} always return {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
     * false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
     * <li>{@link Iterator#next next} and {@link ListIterator#previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
     * previous} always throw {@link NoSuchElementException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
     * <li>{@link Iterator#remove remove} and {@link ListIterator#set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
     * set} always throw {@link IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
     * <li>{@link ListIterator#add add} always throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
     * UnsupportedOperationException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
     * <li>{@link ListIterator#nextIndex nextIndex} always returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
     * {@code 0} .
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
     * <li>{@link ListIterator#previousIndex previousIndex} always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
     * returns {@code -1}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
     * <p>Implementations of this method are permitted, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
     * required, to return the same object from multiple invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
     * @return an empty list iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
    public static <T> ListIterator<T> emptyListIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        return (ListIterator<T>) EmptyListIterator.EMPTY_ITERATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
    private static class EmptyListIterator<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
        extends EmptyIterator<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        implements ListIterator<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        static final EmptyListIterator<Object> EMPTY_ITERATOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
            = new EmptyListIterator<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
        public boolean hasPrevious() { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        public E previous() { throw new NoSuchElementException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        public int nextIndex()     { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
        public int previousIndex() { return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        public void set(E e) { throw new IllegalStateException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        public void add(E e) { throw new UnsupportedOperationException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     * Returns an enumeration that has no elements.  More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
     * <ul compact>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
     * <li>{@link Enumeration#hasMoreElements hasMoreElements} always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
     * returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
     * <li> {@link Enumeration#nextElement nextElement} always throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
     * {@link NoSuchElementException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
     * <p>Implementations of this method are permitted, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
     * required, to return the same object from multiple invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
     * @return an empty enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
    public static <T> Enumeration<T> emptyEnumeration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
        return (Enumeration<T>) EmptyEnumeration.EMPTY_ENUMERATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
    private static class EmptyEnumeration<E> implements Enumeration<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        static final EmptyEnumeration<Object> EMPTY_ENUMERATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
            = new EmptyEnumeration<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
        public boolean hasMoreElements() { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
        public E nextElement() { throw new NoSuchElementException(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
     * The empty set (immutable).  This set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
     * @see #emptySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
    public static final Set EMPTY_SET = new EmptySet<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
     * Returns the empty set (immutable).  This set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
     * Unlike the like-named field, this method is parameterized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
     * <p>This example illustrates the type-safe way to obtain an empty set:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
     *     Set&lt;String&gt; s = Collections.emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     * Implementation note:  Implementations of this method need not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
     * create a separate <tt>Set</tt> object for each call.   Using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
     * method is likely to have comparable cost to using the like-named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
     * field.  (Unlike this method, the field does not provide type safety.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
     * @see #EMPTY_SET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
    public static final <T> Set<T> emptySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
        return (Set<T>) EMPTY_SET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
    private static class EmptySet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
        extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
        implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
        private static final long serialVersionUID = 1582296315990362920L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        public Iterator<E> iterator() { return emptyIterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        public int size() {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        public boolean isEmpty() {return true;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        public boolean contains(Object obj) {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
        public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        public Object[] toArray() { return new Object[0]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
            if (a.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                a[0] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        // Preserves singleton property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
            return EMPTY_SET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
     * The empty list (immutable).  This list is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
     * @see #emptyList()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
    public static final List EMPTY_LIST = new EmptyList<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
     * Returns the empty list (immutable).  This list is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
     * <p>This example illustrates the type-safe way to obtain an empty list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
     *     List&lt;String&gt; s = Collections.emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
     * Implementation note:  Implementations of this method need not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
     * create a separate <tt>List</tt> object for each call.   Using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
     * method is likely to have comparable cost to using the like-named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
     * field.  (Unlike this method, the field does not provide type safety.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
     * @see #EMPTY_LIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
    public static final <T> List<T> emptyList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
        return (List<T>) EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
    private static class EmptyList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        implements RandomAccess, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
        private static final long serialVersionUID = 8842843931221139166L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
            return emptyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
        public ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
            return emptyListIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        public int size() {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
        public boolean isEmpty() {return true;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
        public boolean contains(Object obj) {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
        public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
        public Object[] toArray() { return new Object[0]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
            if (a.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
                a[0] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
            throw new IndexOutOfBoundsException("Index: "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            return (o instanceof List) && ((List<?>)o).isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
        public int hashCode() { return 1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
        // Preserves singleton property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            return EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
     * The empty map (immutable).  This map is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
     * @see #emptyMap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
    public static final Map EMPTY_MAP = new EmptyMap<Object,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
     * Returns the empty map (immutable).  This map is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
     * <p>This example illustrates the type-safe way to obtain an empty set:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
     *     Map&lt;String, Date&gt; s = Collections.emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     * Implementation note:  Implementations of this method need not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     * create a separate <tt>Map</tt> object for each call.   Using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
     * method is likely to have comparable cost to using the like-named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
     * field.  (Unlike this method, the field does not provide type safety.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
     * @see #EMPTY_MAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
    public static final <K,V> Map<K,V> emptyMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
        return (Map<K,V>) EMPTY_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
    private static class EmptyMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
        implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
        private static final long serialVersionUID = 6428348081105594320L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
        public int size()                          {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
        public boolean isEmpty()                   {return true;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
        public boolean containsKey(Object key)     {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
        public boolean containsValue(Object value) {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
        public V get(Object key)                   {return null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
        public Set<K> keySet()                     {return emptySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        public Collection<V> values()              {return emptySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
        public Set<Map.Entry<K,V>> entrySet()      {return emptySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            return (o instanceof Map) && ((Map<?,?>)o).isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
        public int hashCode()                      {return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
        // Preserves singleton property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
        private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            return EMPTY_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
    // Singleton collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
     * Returns an immutable set containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
     * The returned set is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * @param o the sole object to be stored in the returned set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     * @return an immutable set containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
    public static <T> Set<T> singleton(T o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
        return new SingletonSet<T>(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
    static <E> Iterator<E> singletonIterator(final E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
        return new Iterator<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            private boolean hasNext = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
            public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
                return hasNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
                if (hasNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
                    hasNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
                    return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
    private static class SingletonSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
        extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
        implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
        private static final long serialVersionUID = 3193687207550431679L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
        final private E element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
        SingletonSet(E e) {element = e;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
            return singletonIterator(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
        public int size() {return 1;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        public boolean contains(Object o) {return eq(o, element);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
     * Returns an immutable list containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
     * The returned list is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
     * @param o the sole object to be stored in the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
     * @return an immutable list containing only the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
    public static <T> List<T> singletonList(T o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
        return new SingletonList<T>(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
    private static class SingletonList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        implements RandomAccess, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
        private static final long serialVersionUID = 3093736618740652951L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
        private final E element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
        SingletonList(E obj)                {element = obj;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
        public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
            return singletonIterator(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
        public int size()                   {return 1;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
        public boolean contains(Object obj) {return eq(obj, element);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
            if (index != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
              throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
            return element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
     * Returns an immutable map, mapping only the specified key to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
     * specified value.  The returned map is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
     * @param key the sole key to be stored in the returned map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
     * @param value the value to which the returned map maps <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
     * @return an immutable map containing only the specified key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
     *         mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
    public static <K,V> Map<K,V> singletonMap(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
        return new SingletonMap<K,V>(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
    private static class SingletonMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
          extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
          implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
        private static final long serialVersionUID = -6979724477215052911L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        private final K k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
        private final V v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
        SingletonMap(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
            k = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
            v = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
        public int size()                          {return 1;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
        public boolean isEmpty()                   {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        public boolean containsKey(Object key)     {return eq(key, k);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
        public boolean containsValue(Object value) {return eq(value, v);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        public V get(Object key)                   {return (eq(key, k) ? v : null);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        private transient Set<K> keySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
        private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
        private transient Collection<V> values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
            if (keySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                keySet = singleton(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
            return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
        public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
            if (entrySet==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
                entrySet = Collections.<Map.Entry<K,V>>singleton(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                    new SimpleImmutableEntry<K,V>(k, v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
            return entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
            if (values==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
                values = singleton(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
            return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
    // Miscellaneous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     * Returns an immutable list consisting of <tt>n</tt> copies of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * specified object.  The newly allocated data object is tiny (it contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * a single reference to the data object).  This method is useful in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     * combination with the <tt>List.addAll</tt> method to grow lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     * The returned list is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
     * @param  n the number of elements in the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
     * @param  o the element to appear repeatedly in the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
     * @return an immutable list consisting of <tt>n</tt> copies of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
     *         specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
     * @throws IllegalArgumentException if n &lt; 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
     * @see    List#addAll(Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
     * @see    List#addAll(int, Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
    public static <T> List<T> nCopies(int n, T o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
        if (n < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
            throw new IllegalArgumentException("List length = " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
        return new CopiesList<T>(n, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
    private static class CopiesList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
        extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
        implements RandomAccess, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
        private static final long serialVersionUID = 2739099268398711800L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
        final int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
        final E element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
        CopiesList(int n, E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
            assert n >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
            this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
            element = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
        public boolean contains(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
            return n != 0 && eq(obj, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
            return contains(o) ? 0 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
        public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
            return contains(o) ? n - 1 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
        public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
            if (index < 0 || index >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                                                    ", Size: "+n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
            return element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
        public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
            final Object[] a = new Object[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
            if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                Arrays.fill(a, 0, n, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
        public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
            final int n = this.n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
            if (a.length < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                a = (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                    .newInstance(a.getClass().getComponentType(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                if (element != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                    Arrays.fill(a, 0, n, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                Arrays.fill(a, 0, n, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                if (a.length > n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                    a[n] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            return a;
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
            if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
                throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
            if (toIndex > n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
                throw new IndexOutOfBoundsException("toIndex = " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
            if (fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
                throw new IllegalArgumentException("fromIndex(" + fromIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
                                                   ") > toIndex(" + toIndex + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
            return new CopiesList<E>(toIndex - fromIndex, element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
     * Returns a comparator that imposes the reverse of the <i>natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
     * ordering</i> on a collection of objects that implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
     * <tt>Comparable</tt> interface.  (The natural ordering is the ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
     * imposed by the objects' own <tt>compareTo</tt> method.)  This enables a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
     * simple idiom for sorting (or maintaining) collections (or arrays) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
     * objects that implement the <tt>Comparable</tt> interface in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
     * reverse-natural-order.  For example, suppose a is an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
     * strings. Then: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
     *          Arrays.sort(a, Collections.reverseOrder());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
     * </pre> sorts the array in reverse-lexicographic (alphabetical) order.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
     * The returned comparator is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     * @return a comparator that imposes the reverse of the <i>natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
     *         ordering</i> on a collection of objects that implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
     *         the <tt>Comparable</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
     * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
    public static <T> Comparator<T> reverseOrder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
    private static class ReverseComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
        implements Comparator<Comparable<Object>>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
        private static final long serialVersionUID = 7207038068494060240L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
        static final ReverseComparator REVERSE_ORDER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
            = new ReverseComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        public int compare(Comparable<Object> c1, Comparable<Object> c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
            return c2.compareTo(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
        private Object readResolve() { return reverseOrder(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
     * Returns a comparator that imposes the reverse ordering of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
     * comparator.  If the specified comparator is null, this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     * equivalent to {@link #reverseOrder()} (in other words, it returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     * comparator that imposes the reverse of the <i>natural ordering</i> on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     * collection of objects that implement the Comparable interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     * <p>The returned comparator is serializable (assuming the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     * comparator is also serializable or null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * @return a comparator that imposes the reverse ordering of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
     *         specified comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
    public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        if (cmp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
            return reverseOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        if (cmp instanceof ReverseComparator2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            return ((ReverseComparator2<T>)cmp).cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
        return new ReverseComparator2<T>(cmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
    private static class ReverseComparator2<T> implements Comparator<T>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
        Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        private static final long serialVersionUID = 4374092139857L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
         * The comparator specified in the static factory.  This will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
         * be null, as the static factory returns a ReverseComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
         * instance if its argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
        final Comparator<T> cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
        ReverseComparator2(Comparator<T> cmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
            assert cmp != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
            this.cmp = cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
        public int compare(T t1, T t2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
            return cmp.compare(t2, t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
            return (o == this) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
                (o instanceof ReverseComparator2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
                 cmp.equals(((ReverseComparator2)o).cmp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
            return cmp.hashCode() ^ Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
     * Returns an enumeration over the specified collection.  This provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
     * interoperability with legacy APIs that require an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
     * as input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
     * @param c the collection for which an enumeration is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
     * @return an enumeration over the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
     * @see Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
    public static <T> Enumeration<T> enumeration(final Collection<T> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
        return new Enumeration<T>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
            private final Iterator<T> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                return i.hasNext();
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 T nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
                return i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
     * Returns an array list containing the elements returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
     * specified enumeration in the order they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
     * enumeration.  This method provides interoperability between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
     * legacy APIs that return enumerations and new APIs that require
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
     * collections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
     * @param e enumeration providing elements for the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
     *          array list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
     * @return an array list containing the elements returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
     *         by the specified enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
     * @see Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
     * @see ArrayList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
    public static <T> ArrayList<T> list(Enumeration<T> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
        ArrayList<T> l = new ArrayList<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
        while (e.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
            l.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
     * Returns true if the specified arguments are equal, or both null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
    static boolean eq(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
        return o1==null ? o2==null : o1.equals(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
     * Returns the number of elements in the specified collection equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
     * specified object.  More formally, returns the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
     * <tt>e</tt> in the collection such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
     * <tt>(o == null ? e == null : o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
     * @param c the collection in which to determine the frequency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
     *     of <tt>o</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
     * @param o the object whose frequency is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
     * @throws NullPointerException if <tt>c</tt> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
    public static int frequency(Collection<?> c, Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
            for (Object e : c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
                    result++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
            for (Object e : c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
                if (o.equals(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
                    result++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     * Returns <tt>true</tt> if the two specified collections have no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     * elements in common.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
     * <p>Care must be exercised if this method is used on collections that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
     * do not comply with the general contract for <tt>Collection</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
     * Implementations may elect to iterate over either collection and test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
     * for containment in the other collection (or to perform any equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
     * computation).  If either collection uses a nonstandard equality test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
     * (as does a {@link SortedSet} whose ordering is not <i>compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
     * equals</i>, or the key set of an {@link IdentityHashMap}), both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
     * collections must use the same nonstandard equality test, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
     * result of this method is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
     * <p>Note that it is permissible to pass the same collection in both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
     * parameters, in which case the method will return true if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
     * the collection is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     * @param c1 a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
     * @param c2 a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
     * @throws NullPointerException if either collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
    public static boolean disjoint(Collection<?> c1, Collection<?> c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
         * We're going to iterate through c1 and test for inclusion in c2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
         * If c1 is a Set and c2 isn't, swap the collections.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
         * place the shorter collection in c1.  Hopefully this heuristic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
         * will minimize the cost of the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
        if ((c1 instanceof Set) && !(c2 instanceof Set) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
            (c1.size() > c2.size())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
            Collection<?> tmp = c1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
            c1 = c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
            c2 = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
        for (Object e : c1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
            if (c2.contains(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
     * Adds all of the specified elements to the specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
     * Elements to be added may be specified individually or as an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
     * The behavior of this convenience method is identical to that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
     * <tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
     * to run significantly faster under most implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
     * <p>When elements are specified individually, this method provides a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
     * convenient way to add a few elements to an existing collection:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     *     Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
     * @param c the collection into which <tt>elements</tt> are to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
     * @param elements the elements to insert into <tt>c</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
     * @return <tt>true</tt> if the collection changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
     * @throws UnsupportedOperationException if <tt>c</tt> does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
     *         the <tt>add</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
     * @throws NullPointerException if <tt>elements</tt> contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
     *         null values and <tt>c</tt> does not permit null elements, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
     *         if <tt>c</tt> or <tt>elements</tt> are <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
     * @throws IllegalArgumentException if some property of a value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
     *         <tt>elements</tt> prevents it from being added to <tt>c</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
     * @see Collection#addAll(Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
    public static <T> boolean addAll(Collection<? super T> c, T... elements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
        boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
        for (T element : elements)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
            result |= c.add(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
     * Returns a set backed by the specified map.  The resulting set displays
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
     * the same ordering, concurrency, and performance characteristics as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
     * backing map.  In essence, this factory method provides a {@link Set}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
     * implementation corresponding to any {@link Map} implementation.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
     * is no need to use this method on a {@link Map} implementation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
     * already has a corresponding {@link Set} implementation (such as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
     * HashMap} or {@link TreeMap}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
     * <p>Each method invocation on the set returned by this method results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
     * exactly one method invocation on the backing map or its <tt>keySet</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
     * view, with one exception.  The <tt>addAll</tt> method is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
     * as a sequence of <tt>put</tt> invocations on the backing map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
     * <p>The specified map must be empty at the time this method is invoked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
     * and should not be accessed directly after this method returns.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
     * conditions are ensured if the map is created empty, passed directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
     * to this method, and no reference to the map is retained, as illustrated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
     * in the following code fragment:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
     *    Set&lt;Object&gt; weakHashSet = Collections.newSetFromMap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
     *        new WeakHashMap&lt;Object, Boolean&gt;());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
     * @param map the backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
     * @return the set backed by the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
     * @throws IllegalArgumentException if <tt>map</tt> is not empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
    public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
        return new SetFromMap<E>(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
    private static class SetFromMap<E> extends AbstractSet<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
        implements Set<E>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
        private final Map<E, Boolean> m;  // The backing map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
        private transient Set<E> s;       // Its keySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
        SetFromMap(Map<E, Boolean> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
            if (!map.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                throw new IllegalArgumentException("Map is non-empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
            m = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
            s = map.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
        public void clear()               {        m.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
        public int size()                 { return m.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
        public boolean isEmpty()          { return m.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
        public boolean contains(Object o) { return m.containsKey(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
        public boolean remove(Object o)   { return m.remove(o) != null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
        public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
        public Iterator<E> iterator()     { return s.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
        public Object[] toArray()         { return s.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
        public <T> T[] toArray(T[] a)     { return s.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
        public String toString()          { return s.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
        public int hashCode()             { return s.hashCode(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
        public boolean equals(Object o)   { return o == this || s.equals(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
        public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
        public boolean removeAll(Collection<?> c)   {return s.removeAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
        public boolean retainAll(Collection<?> c)   {return s.retainAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
        // addAll is the only inherited implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
        private static final long serialVersionUID = 2454657854757543876L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
        private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
            throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
            stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
            s = m.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
     * Returns a view of a {@link Deque} as a Last-in-first-out (Lifo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
     * {@link Queue}. Method <tt>add</tt> is mapped to <tt>push</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
     * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
     * view can be useful when you would like to use a method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
     * requiring a <tt>Queue</tt> but you need Lifo ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
     * <p>Each method invocation on the queue returned by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
     * results in exactly one method invocation on the backing deque, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
     * one exception.  The {@link Queue#addAll addAll} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
     * implemented as a sequence of {@link Deque#addFirst addFirst}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
     * invocations on the backing deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
     * @param deque the deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
     * @return the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
    public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
        return new AsLIFOQueue<T>(deque);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
    static class AsLIFOQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
        implements Queue<E>, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
        private static final long serialVersionUID = 1802017725587941708L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
        private final Deque<E> q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
        AsLIFOQueue(Deque<E> q)           { this.q = q; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
        public boolean add(E e)           { q.addFirst(e); return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
        public boolean offer(E e)         { return q.offerFirst(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
        public E poll()                   { return q.pollFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
        public E remove()                 { return q.removeFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
        public E peek()                   { return q.peekFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
        public E element()                { return q.getFirst(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
        public void clear()               {        q.clear(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
        public int size()                 { return q.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
        public boolean isEmpty()          { return q.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
        public boolean contains(Object o) { return q.contains(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
        public boolean remove(Object o)   { return q.remove(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
        public Iterator<E> iterator()     { return q.iterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
        public Object[] toArray()         { return q.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
        public <T> T[] toArray(T[] a)     { return q.toArray(a); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
        public String toString()          { return q.toString(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
        public boolean containsAll(Collection<?> c) {return q.containsAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
        public boolean removeAll(Collection<?> c)   {return q.removeAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
        public boolean retainAll(Collection<?> c)   {return q.retainAll(c);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
        // We use inherited addAll; forwarding addAll would be wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
}