emul/compact/src/main/java/java/util/Arrays.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 22:32:27 +0100
branchjdk7-b147
changeset 557 5be31d9fa455
child 564 353102c8cf9f
permissions -rw-r--r--
Basic classes needed to support ServiceLoader
jaroslav@557
     1
/*
jaroslav@557
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@557
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@557
     4
 *
jaroslav@557
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@557
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@557
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@557
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@557
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@557
    10
 *
jaroslav@557
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@557
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@557
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@557
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@557
    15
 * accompanied this code).
jaroslav@557
    16
 *
jaroslav@557
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@557
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@557
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@557
    20
 *
jaroslav@557
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@557
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@557
    23
 * questions.
jaroslav@557
    24
 */
jaroslav@557
    25
jaroslav@557
    26
package java.util;
jaroslav@557
    27
jaroslav@557
    28
import java.lang.reflect.*;
jaroslav@557
    29
jaroslav@557
    30
/**
jaroslav@557
    31
 * This class contains various methods for manipulating arrays (such as
jaroslav@557
    32
 * sorting and searching). This class also contains a static factory
jaroslav@557
    33
 * that allows arrays to be viewed as lists.
jaroslav@557
    34
 *
jaroslav@557
    35
 * <p>The methods in this class all throw a {@code NullPointerException},
jaroslav@557
    36
 * if the specified array reference is null, except where noted.
jaroslav@557
    37
 *
jaroslav@557
    38
 * <p>The documentation for the methods contained in this class includes
jaroslav@557
    39
 * briefs description of the <i>implementations</i>. Such descriptions should
jaroslav@557
    40
 * be regarded as <i>implementation notes</i>, rather than parts of the
jaroslav@557
    41
 * <i>specification</i>. Implementors should feel free to substitute other
jaroslav@557
    42
 * algorithms, so long as the specification itself is adhered to. (For
jaroslav@557
    43
 * example, the algorithm used by {@code sort(Object[])} does not have to be
jaroslav@557
    44
 * a MergeSort, but it does have to be <i>stable</i>.)
jaroslav@557
    45
 *
jaroslav@557
    46
 * <p>This class is a member of the
jaroslav@557
    47
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@557
    48
 * Java Collections Framework</a>.
jaroslav@557
    49
 *
jaroslav@557
    50
 * @author Josh Bloch
jaroslav@557
    51
 * @author Neal Gafter
jaroslav@557
    52
 * @author John Rose
jaroslav@557
    53
 * @since  1.2
jaroslav@557
    54
 */
jaroslav@557
    55
public class Arrays {
jaroslav@557
    56
jaroslav@557
    57
    // Suppresses default constructor, ensuring non-instantiability.
jaroslav@557
    58
    private Arrays() {}
jaroslav@557
    59
jaroslav@557
    60
    /*
jaroslav@557
    61
     * Sorting of primitive type arrays.
jaroslav@557
    62
     */
jaroslav@557
    63
jaroslav@557
    64
    /**
jaroslav@557
    65
     * Sorts the specified array into ascending numerical order.
jaroslav@557
    66
     *
jaroslav@557
    67
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
    68
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
    69
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
    70
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
    71
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
    72
     *
jaroslav@557
    73
     * @param a the array to be sorted
jaroslav@557
    74
     */
jaroslav@557
    75
    public static void sort(int[] a) {
jaroslav@557
    76
        DualPivotQuicksort.sort(a);
jaroslav@557
    77
    }
jaroslav@557
    78
jaroslav@557
    79
    /**
jaroslav@557
    80
     * Sorts the specified range of the array into ascending order. The range
jaroslav@557
    81
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
jaroslav@557
    82
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
jaroslav@557
    83
     * the range to be sorted is empty.
jaroslav@557
    84
     *
jaroslav@557
    85
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
    86
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
    87
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
    88
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
    89
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
    90
     *
jaroslav@557
    91
     * @param a the array to be sorted
jaroslav@557
    92
     * @param fromIndex the index of the first element, inclusive, to be sorted
jaroslav@557
    93
     * @param toIndex the index of the last element, exclusive, to be sorted
jaroslav@557
    94
     *
jaroslav@557
    95
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
jaroslav@557
    96
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
    97
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
jaroslav@557
    98
     */
jaroslav@557
    99
    public static void sort(int[] a, int fromIndex, int toIndex) {
jaroslav@557
   100
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   101
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
jaroslav@557
   102
    }
jaroslav@557
   103
jaroslav@557
   104
    /**
jaroslav@557
   105
     * Sorts the specified array into ascending numerical order.
jaroslav@557
   106
     *
jaroslav@557
   107
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   108
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   109
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   110
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   111
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   112
     *
jaroslav@557
   113
     * @param a the array to be sorted
jaroslav@557
   114
     */
jaroslav@557
   115
    public static void sort(long[] a) {
jaroslav@557
   116
        DualPivotQuicksort.sort(a);
jaroslav@557
   117
    }
jaroslav@557
   118
jaroslav@557
   119
    /**
jaroslav@557
   120
     * Sorts the specified range of the array into ascending order. The range
jaroslav@557
   121
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
jaroslav@557
   122
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
jaroslav@557
   123
     * the range to be sorted is empty.
jaroslav@557
   124
     *
jaroslav@557
   125
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   126
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   127
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   128
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   129
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   130
     *
jaroslav@557
   131
     * @param a the array to be sorted
jaroslav@557
   132
     * @param fromIndex the index of the first element, inclusive, to be sorted
jaroslav@557
   133
     * @param toIndex the index of the last element, exclusive, to be sorted
jaroslav@557
   134
     *
jaroslav@557
   135
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
jaroslav@557
   136
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   137
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
jaroslav@557
   138
     */
jaroslav@557
   139
    public static void sort(long[] a, int fromIndex, int toIndex) {
jaroslav@557
   140
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   141
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
jaroslav@557
   142
    }
jaroslav@557
   143
jaroslav@557
   144
    /**
jaroslav@557
   145
     * Sorts the specified array into ascending numerical order.
jaroslav@557
   146
     *
jaroslav@557
   147
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   148
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   149
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   150
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   151
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   152
     *
jaroslav@557
   153
     * @param a the array to be sorted
jaroslav@557
   154
     */
jaroslav@557
   155
    public static void sort(short[] a) {
jaroslav@557
   156
        DualPivotQuicksort.sort(a);
jaroslav@557
   157
    }
jaroslav@557
   158
jaroslav@557
   159
    /**
jaroslav@557
   160
     * Sorts the specified range of the array into ascending order. The range
jaroslav@557
   161
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
jaroslav@557
   162
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
jaroslav@557
   163
     * the range to be sorted is empty.
jaroslav@557
   164
     *
jaroslav@557
   165
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   166
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   167
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   168
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   169
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   170
     *
jaroslav@557
   171
     * @param a the array to be sorted
jaroslav@557
   172
     * @param fromIndex the index of the first element, inclusive, to be sorted
jaroslav@557
   173
     * @param toIndex the index of the last element, exclusive, to be sorted
jaroslav@557
   174
     *
jaroslav@557
   175
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
jaroslav@557
   176
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   177
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
jaroslav@557
   178
     */
jaroslav@557
   179
    public static void sort(short[] a, int fromIndex, int toIndex) {
jaroslav@557
   180
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   181
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
jaroslav@557
   182
    }
jaroslav@557
   183
jaroslav@557
   184
    /**
jaroslav@557
   185
     * Sorts the specified array into ascending numerical order.
jaroslav@557
   186
     *
jaroslav@557
   187
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   188
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   189
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   190
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   191
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   192
     *
jaroslav@557
   193
     * @param a the array to be sorted
jaroslav@557
   194
     */
jaroslav@557
   195
    public static void sort(char[] a) {
jaroslav@557
   196
        DualPivotQuicksort.sort(a);
jaroslav@557
   197
    }
jaroslav@557
   198
jaroslav@557
   199
    /**
jaroslav@557
   200
     * Sorts the specified range of the array into ascending order. The range
jaroslav@557
   201
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
jaroslav@557
   202
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
jaroslav@557
   203
     * the range to be sorted is empty.
jaroslav@557
   204
     *
jaroslav@557
   205
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   206
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   207
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   208
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   209
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   210
     *
jaroslav@557
   211
     * @param a the array to be sorted
jaroslav@557
   212
     * @param fromIndex the index of the first element, inclusive, to be sorted
jaroslav@557
   213
     * @param toIndex the index of the last element, exclusive, to be sorted
jaroslav@557
   214
     *
jaroslav@557
   215
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
jaroslav@557
   216
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   217
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
jaroslav@557
   218
     */
jaroslav@557
   219
    public static void sort(char[] a, int fromIndex, int toIndex) {
jaroslav@557
   220
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   221
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
jaroslav@557
   222
    }
jaroslav@557
   223
jaroslav@557
   224
    /**
jaroslav@557
   225
     * Sorts the specified array into ascending numerical order.
jaroslav@557
   226
     *
jaroslav@557
   227
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   228
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   229
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   230
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   231
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   232
     *
jaroslav@557
   233
     * @param a the array to be sorted
jaroslav@557
   234
     */
jaroslav@557
   235
    public static void sort(byte[] a) {
jaroslav@557
   236
        DualPivotQuicksort.sort(a);
jaroslav@557
   237
    }
jaroslav@557
   238
jaroslav@557
   239
    /**
jaroslav@557
   240
     * Sorts the specified range of the array into ascending order. The range
jaroslav@557
   241
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
jaroslav@557
   242
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
jaroslav@557
   243
     * the range to be sorted is empty.
jaroslav@557
   244
     *
jaroslav@557
   245
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   246
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   247
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   248
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   249
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   250
     *
jaroslav@557
   251
     * @param a the array to be sorted
jaroslav@557
   252
     * @param fromIndex the index of the first element, inclusive, to be sorted
jaroslav@557
   253
     * @param toIndex the index of the last element, exclusive, to be sorted
jaroslav@557
   254
     *
jaroslav@557
   255
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
jaroslav@557
   256
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   257
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
jaroslav@557
   258
     */
jaroslav@557
   259
    public static void sort(byte[] a, int fromIndex, int toIndex) {
jaroslav@557
   260
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   261
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
jaroslav@557
   262
    }
jaroslav@557
   263
jaroslav@557
   264
    /**
jaroslav@557
   265
     * Sorts the specified array into ascending numerical order.
jaroslav@557
   266
     *
jaroslav@557
   267
     * <p>The {@code <} relation does not provide a total order on all float
jaroslav@557
   268
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
jaroslav@557
   269
     * value compares neither less than, greater than, nor equal to any value,
jaroslav@557
   270
     * even itself. This method uses the total order imposed by the method
jaroslav@557
   271
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
jaroslav@557
   272
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
jaroslav@557
   273
     * other value and all {@code Float.NaN} values are considered equal.
jaroslav@557
   274
     *
jaroslav@557
   275
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   276
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   277
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   278
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   279
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   280
     *
jaroslav@557
   281
     * @param a the array to be sorted
jaroslav@557
   282
     */
jaroslav@557
   283
    public static void sort(float[] a) {
jaroslav@557
   284
        DualPivotQuicksort.sort(a);
jaroslav@557
   285
    }
jaroslav@557
   286
jaroslav@557
   287
    /**
jaroslav@557
   288
     * Sorts the specified range of the array into ascending order. The range
jaroslav@557
   289
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
jaroslav@557
   290
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
jaroslav@557
   291
     * the range to be sorted is empty.
jaroslav@557
   292
     *
jaroslav@557
   293
     * <p>The {@code <} relation does not provide a total order on all float
jaroslav@557
   294
     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
jaroslav@557
   295
     * value compares neither less than, greater than, nor equal to any value,
jaroslav@557
   296
     * even itself. This method uses the total order imposed by the method
jaroslav@557
   297
     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
jaroslav@557
   298
     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
jaroslav@557
   299
     * other value and all {@code Float.NaN} values are considered equal.
jaroslav@557
   300
     *
jaroslav@557
   301
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   302
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   303
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   304
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   305
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   306
     *
jaroslav@557
   307
     * @param a the array to be sorted
jaroslav@557
   308
     * @param fromIndex the index of the first element, inclusive, to be sorted
jaroslav@557
   309
     * @param toIndex the index of the last element, exclusive, to be sorted
jaroslav@557
   310
     *
jaroslav@557
   311
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
jaroslav@557
   312
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   313
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
jaroslav@557
   314
     */
jaroslav@557
   315
    public static void sort(float[] a, int fromIndex, int toIndex) {
jaroslav@557
   316
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   317
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
jaroslav@557
   318
    }
jaroslav@557
   319
jaroslav@557
   320
    /**
jaroslav@557
   321
     * Sorts the specified array into ascending numerical order.
jaroslav@557
   322
     *
jaroslav@557
   323
     * <p>The {@code <} relation does not provide a total order on all double
jaroslav@557
   324
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
jaroslav@557
   325
     * value compares neither less than, greater than, nor equal to any value,
jaroslav@557
   326
     * even itself. This method uses the total order imposed by the method
jaroslav@557
   327
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
jaroslav@557
   328
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
jaroslav@557
   329
     * other value and all {@code Double.NaN} values are considered equal.
jaroslav@557
   330
     *
jaroslav@557
   331
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   332
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   333
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   334
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   335
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   336
     *
jaroslav@557
   337
     * @param a the array to be sorted
jaroslav@557
   338
     */
jaroslav@557
   339
    public static void sort(double[] a) {
jaroslav@557
   340
        DualPivotQuicksort.sort(a);
jaroslav@557
   341
    }
jaroslav@557
   342
jaroslav@557
   343
    /**
jaroslav@557
   344
     * Sorts the specified range of the array into ascending order. The range
jaroslav@557
   345
     * to be sorted extends from the index {@code fromIndex}, inclusive, to
jaroslav@557
   346
     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
jaroslav@557
   347
     * the range to be sorted is empty.
jaroslav@557
   348
     *
jaroslav@557
   349
     * <p>The {@code <} relation does not provide a total order on all double
jaroslav@557
   350
     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
jaroslav@557
   351
     * value compares neither less than, greater than, nor equal to any value,
jaroslav@557
   352
     * even itself. This method uses the total order imposed by the method
jaroslav@557
   353
     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
jaroslav@557
   354
     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
jaroslav@557
   355
     * other value and all {@code Double.NaN} values are considered equal.
jaroslav@557
   356
     *
jaroslav@557
   357
     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
jaroslav@557
   358
     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
jaroslav@557
   359
     * offers O(n log(n)) performance on many data sets that cause other
jaroslav@557
   360
     * quicksorts to degrade to quadratic performance, and is typically
jaroslav@557
   361
     * faster than traditional (one-pivot) Quicksort implementations.
jaroslav@557
   362
     *
jaroslav@557
   363
     * @param a the array to be sorted
jaroslav@557
   364
     * @param fromIndex the index of the first element, inclusive, to be sorted
jaroslav@557
   365
     * @param toIndex the index of the last element, exclusive, to be sorted
jaroslav@557
   366
     *
jaroslav@557
   367
     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
jaroslav@557
   368
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   369
     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
jaroslav@557
   370
     */
jaroslav@557
   371
    public static void sort(double[] a, int fromIndex, int toIndex) {
jaroslav@557
   372
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   373
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
jaroslav@557
   374
    }
jaroslav@557
   375
jaroslav@557
   376
    /*
jaroslav@557
   377
     * Sorting of complex type arrays.
jaroslav@557
   378
     */
jaroslav@557
   379
jaroslav@557
   380
    /**
jaroslav@557
   381
     * Old merge sort implementation can be selected (for
jaroslav@557
   382
     * compatibility with broken comparators) using a system property.
jaroslav@557
   383
     * Cannot be a static boolean in the enclosing class due to
jaroslav@557
   384
     * circular dependencies. To be removed in a future release.
jaroslav@557
   385
     */
jaroslav@557
   386
    static final class LegacyMergeSort {
jaroslav@557
   387
        private static final boolean userRequested =
jaroslav@557
   388
            java.security.AccessController.doPrivileged(
jaroslav@557
   389
                new sun.security.action.GetBooleanAction(
jaroslav@557
   390
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
jaroslav@557
   391
    }
jaroslav@557
   392
jaroslav@557
   393
    /*
jaroslav@557
   394
     * If this platform has an optimizing VM, check whether ComparableTimSort
jaroslav@557
   395
     * offers any performance benefit over TimSort in conjunction with a
jaroslav@557
   396
     * comparator that returns:
jaroslav@557
   397
     *    {@code ((Comparable)first).compareTo(Second)}.
jaroslav@557
   398
     * If not, you are better off deleting ComparableTimSort to
jaroslav@557
   399
     * eliminate the code duplication.  In other words, the commented
jaroslav@557
   400
     * out code below is the preferable implementation for sorting
jaroslav@557
   401
     * arrays of Comparables if it offers sufficient performance.
jaroslav@557
   402
     */
jaroslav@557
   403
jaroslav@557
   404
//    /**
jaroslav@557
   405
//     * A comparator that implements the natural ordering of a group of
jaroslav@557
   406
//     * mutually comparable elements.  Using this comparator saves us
jaroslav@557
   407
//     * from duplicating most of the code in this file (one version for
jaroslav@557
   408
//     * Comparables, one for explicit Comparators).
jaroslav@557
   409
//     */
jaroslav@557
   410
//    private static final Comparator<Object> NATURAL_ORDER =
jaroslav@557
   411
//            new Comparator<Object>() {
jaroslav@557
   412
//        @SuppressWarnings("unchecked")
jaroslav@557
   413
//        public int compare(Object first, Object second) {
jaroslav@557
   414
//            return ((Comparable<Object>)first).compareTo(second);
jaroslav@557
   415
//        }
jaroslav@557
   416
//    };
jaroslav@557
   417
//
jaroslav@557
   418
//    public static void sort(Object[] a) {
jaroslav@557
   419
//        sort(a, 0, a.length, NATURAL_ORDER);
jaroslav@557
   420
//    }
jaroslav@557
   421
//
jaroslav@557
   422
//    public static void sort(Object[] a, int fromIndex, int toIndex) {
jaroslav@557
   423
//        sort(a, fromIndex, toIndex, NATURAL_ORDER);
jaroslav@557
   424
//    }
jaroslav@557
   425
jaroslav@557
   426
    /**
jaroslav@557
   427
     * Sorts the specified array of objects into ascending order, according
jaroslav@557
   428
     * to the {@linkplain Comparable natural ordering} of its elements.
jaroslav@557
   429
     * All elements in the array must implement the {@link Comparable}
jaroslav@557
   430
     * interface.  Furthermore, all elements in the array must be
jaroslav@557
   431
     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
jaroslav@557
   432
     * not throw a {@code ClassCastException} for any elements {@code e1}
jaroslav@557
   433
     * and {@code e2} in the array).
jaroslav@557
   434
     *
jaroslav@557
   435
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
jaroslav@557
   436
     * not be reordered as a result of the sort.
jaroslav@557
   437
     *
jaroslav@557
   438
     * <p>Implementation note: This implementation is a stable, adaptive,
jaroslav@557
   439
     * iterative mergesort that requires far fewer than n lg(n) comparisons
jaroslav@557
   440
     * when the input array is partially sorted, while offering the
jaroslav@557
   441
     * performance of a traditional mergesort when the input array is
jaroslav@557
   442
     * randomly ordered.  If the input array is nearly sorted, the
jaroslav@557
   443
     * implementation requires approximately n comparisons.  Temporary
jaroslav@557
   444
     * storage requirements vary from a small constant for nearly sorted
jaroslav@557
   445
     * input arrays to n/2 object references for randomly ordered input
jaroslav@557
   446
     * arrays.
jaroslav@557
   447
     *
jaroslav@557
   448
     * <p>The implementation takes equal advantage of ascending and
jaroslav@557
   449
     * descending order in its input array, and can take advantage of
jaroslav@557
   450
     * ascending and descending order in different parts of the the same
jaroslav@557
   451
     * input array.  It is well-suited to merging two or more sorted arrays:
jaroslav@557
   452
     * simply concatenate the arrays and sort the resulting array.
jaroslav@557
   453
     *
jaroslav@557
   454
     * <p>The implementation was adapted from Tim Peters's list sort for Python
jaroslav@557
   455
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
jaroslav@557
   456
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
jaroslav@557
   457
     * Sorting and Information Theoretic Complexity", in Proceedings of the
jaroslav@557
   458
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
jaroslav@557
   459
     * January 1993.
jaroslav@557
   460
     *
jaroslav@557
   461
     * @param a the array to be sorted
jaroslav@557
   462
     * @throws ClassCastException if the array contains elements that are not
jaroslav@557
   463
     *         <i>mutually comparable</i> (for example, strings and integers)
jaroslav@557
   464
     * @throws IllegalArgumentException (optional) if the natural
jaroslav@557
   465
     *         ordering of the array elements is found to violate the
jaroslav@557
   466
     *         {@link Comparable} contract
jaroslav@557
   467
     */
jaroslav@557
   468
    public static void sort(Object[] a) {
jaroslav@557
   469
        if (LegacyMergeSort.userRequested)
jaroslav@557
   470
            legacyMergeSort(a);
jaroslav@557
   471
        else
jaroslav@557
   472
            ComparableTimSort.sort(a);
jaroslav@557
   473
    }
jaroslav@557
   474
jaroslav@557
   475
    /** To be removed in a future release. */
jaroslav@557
   476
    private static void legacyMergeSort(Object[] a) {
jaroslav@557
   477
        Object[] aux = a.clone();
jaroslav@557
   478
        mergeSort(aux, a, 0, a.length, 0);
jaroslav@557
   479
    }
jaroslav@557
   480
jaroslav@557
   481
    /**
jaroslav@557
   482
     * Sorts the specified range of the specified array of objects into
jaroslav@557
   483
     * ascending order, according to the
jaroslav@557
   484
     * {@linkplain Comparable natural ordering} of its
jaroslav@557
   485
     * elements.  The range to be sorted extends from index
jaroslav@557
   486
     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
jaroslav@557
   487
     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
jaroslav@557
   488
     * elements in this range must implement the {@link Comparable}
jaroslav@557
   489
     * interface.  Furthermore, all elements in this range must be <i>mutually
jaroslav@557
   490
     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
jaroslav@557
   491
     * {@code ClassCastException} for any elements {@code e1} and
jaroslav@557
   492
     * {@code e2} in the array).
jaroslav@557
   493
     *
jaroslav@557
   494
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
jaroslav@557
   495
     * not be reordered as a result of the sort.
jaroslav@557
   496
     *
jaroslav@557
   497
     * <p>Implementation note: This implementation is a stable, adaptive,
jaroslav@557
   498
     * iterative mergesort that requires far fewer than n lg(n) comparisons
jaroslav@557
   499
     * when the input array is partially sorted, while offering the
jaroslav@557
   500
     * performance of a traditional mergesort when the input array is
jaroslav@557
   501
     * randomly ordered.  If the input array is nearly sorted, the
jaroslav@557
   502
     * implementation requires approximately n comparisons.  Temporary
jaroslav@557
   503
     * storage requirements vary from a small constant for nearly sorted
jaroslav@557
   504
     * input arrays to n/2 object references for randomly ordered input
jaroslav@557
   505
     * arrays.
jaroslav@557
   506
     *
jaroslav@557
   507
     * <p>The implementation takes equal advantage of ascending and
jaroslav@557
   508
     * descending order in its input array, and can take advantage of
jaroslav@557
   509
     * ascending and descending order in different parts of the the same
jaroslav@557
   510
     * input array.  It is well-suited to merging two or more sorted arrays:
jaroslav@557
   511
     * simply concatenate the arrays and sort the resulting array.
jaroslav@557
   512
     *
jaroslav@557
   513
     * <p>The implementation was adapted from Tim Peters's list sort for Python
jaroslav@557
   514
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
jaroslav@557
   515
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
jaroslav@557
   516
     * Sorting and Information Theoretic Complexity", in Proceedings of the
jaroslav@557
   517
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
jaroslav@557
   518
     * January 1993.
jaroslav@557
   519
     *
jaroslav@557
   520
     * @param a the array to be sorted
jaroslav@557
   521
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
   522
     *        sorted
jaroslav@557
   523
     * @param toIndex the index of the last element (exclusive) to be sorted
jaroslav@557
   524
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
jaroslav@557
   525
     *         (optional) if the natural ordering of the array elements is
jaroslav@557
   526
     *         found to violate the {@link Comparable} contract
jaroslav@557
   527
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
jaroslav@557
   528
     *         {@code toIndex > a.length}
jaroslav@557
   529
     * @throws ClassCastException if the array contains elements that are
jaroslav@557
   530
     *         not <i>mutually comparable</i> (for example, strings and
jaroslav@557
   531
     *         integers).
jaroslav@557
   532
     */
jaroslav@557
   533
    public static void sort(Object[] a, int fromIndex, int toIndex) {
jaroslav@557
   534
        if (LegacyMergeSort.userRequested)
jaroslav@557
   535
            legacyMergeSort(a, fromIndex, toIndex);
jaroslav@557
   536
        else
jaroslav@557
   537
            ComparableTimSort.sort(a, fromIndex, toIndex);
jaroslav@557
   538
    }
jaroslav@557
   539
jaroslav@557
   540
    /** To be removed in a future release. */
jaroslav@557
   541
    private static void legacyMergeSort(Object[] a,
jaroslav@557
   542
                                        int fromIndex, int toIndex) {
jaroslav@557
   543
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   544
        Object[] aux = copyOfRange(a, fromIndex, toIndex);
jaroslav@557
   545
        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
jaroslav@557
   546
    }
jaroslav@557
   547
jaroslav@557
   548
    /**
jaroslav@557
   549
     * Tuning parameter: list size at or below which insertion sort will be
jaroslav@557
   550
     * used in preference to mergesort.
jaroslav@557
   551
     * To be removed in a future release.
jaroslav@557
   552
     */
jaroslav@557
   553
    private static final int INSERTIONSORT_THRESHOLD = 7;
jaroslav@557
   554
jaroslav@557
   555
    /**
jaroslav@557
   556
     * Src is the source array that starts at index 0
jaroslav@557
   557
     * Dest is the (possibly larger) array destination with a possible offset
jaroslav@557
   558
     * low is the index in dest to start sorting
jaroslav@557
   559
     * high is the end index in dest to end sorting
jaroslav@557
   560
     * off is the offset to generate corresponding low, high in src
jaroslav@557
   561
     * To be removed in a future release.
jaroslav@557
   562
     */
jaroslav@557
   563
    private static void mergeSort(Object[] src,
jaroslav@557
   564
                                  Object[] dest,
jaroslav@557
   565
                                  int low,
jaroslav@557
   566
                                  int high,
jaroslav@557
   567
                                  int off) {
jaroslav@557
   568
        int length = high - low;
jaroslav@557
   569
jaroslav@557
   570
        // Insertion sort on smallest arrays
jaroslav@557
   571
        if (length < INSERTIONSORT_THRESHOLD) {
jaroslav@557
   572
            for (int i=low; i<high; i++)
jaroslav@557
   573
                for (int j=i; j>low &&
jaroslav@557
   574
                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
jaroslav@557
   575
                    swap(dest, j, j-1);
jaroslav@557
   576
            return;
jaroslav@557
   577
        }
jaroslav@557
   578
jaroslav@557
   579
        // Recursively sort halves of dest into src
jaroslav@557
   580
        int destLow  = low;
jaroslav@557
   581
        int destHigh = high;
jaroslav@557
   582
        low  += off;
jaroslav@557
   583
        high += off;
jaroslav@557
   584
        int mid = (low + high) >>> 1;
jaroslav@557
   585
        mergeSort(dest, src, low, mid, -off);
jaroslav@557
   586
        mergeSort(dest, src, mid, high, -off);
jaroslav@557
   587
jaroslav@557
   588
        // If list is already sorted, just copy from src to dest.  This is an
jaroslav@557
   589
        // optimization that results in faster sorts for nearly ordered lists.
jaroslav@557
   590
        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
jaroslav@557
   591
            System.arraycopy(src, low, dest, destLow, length);
jaroslav@557
   592
            return;
jaroslav@557
   593
        }
jaroslav@557
   594
jaroslav@557
   595
        // Merge sorted halves (now in src) into dest
jaroslav@557
   596
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
jaroslav@557
   597
            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
jaroslav@557
   598
                dest[i] = src[p++];
jaroslav@557
   599
            else
jaroslav@557
   600
                dest[i] = src[q++];
jaroslav@557
   601
        }
jaroslav@557
   602
    }
jaroslav@557
   603
jaroslav@557
   604
    /**
jaroslav@557
   605
     * Swaps x[a] with x[b].
jaroslav@557
   606
     */
jaroslav@557
   607
    private static void swap(Object[] x, int a, int b) {
jaroslav@557
   608
        Object t = x[a];
jaroslav@557
   609
        x[a] = x[b];
jaroslav@557
   610
        x[b] = t;
jaroslav@557
   611
    }
jaroslav@557
   612
jaroslav@557
   613
    /**
jaroslav@557
   614
     * Sorts the specified array of objects according to the order induced by
jaroslav@557
   615
     * the specified comparator.  All elements in the array must be
jaroslav@557
   616
     * <i>mutually comparable</i> by the specified comparator (that is,
jaroslav@557
   617
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
jaroslav@557
   618
     * for any elements {@code e1} and {@code e2} in the array).
jaroslav@557
   619
     *
jaroslav@557
   620
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
jaroslav@557
   621
     * not be reordered as a result of the sort.
jaroslav@557
   622
     *
jaroslav@557
   623
     * <p>Implementation note: This implementation is a stable, adaptive,
jaroslav@557
   624
     * iterative mergesort that requires far fewer than n lg(n) comparisons
jaroslav@557
   625
     * when the input array is partially sorted, while offering the
jaroslav@557
   626
     * performance of a traditional mergesort when the input array is
jaroslav@557
   627
     * randomly ordered.  If the input array is nearly sorted, the
jaroslav@557
   628
     * implementation requires approximately n comparisons.  Temporary
jaroslav@557
   629
     * storage requirements vary from a small constant for nearly sorted
jaroslav@557
   630
     * input arrays to n/2 object references for randomly ordered input
jaroslav@557
   631
     * arrays.
jaroslav@557
   632
     *
jaroslav@557
   633
     * <p>The implementation takes equal advantage of ascending and
jaroslav@557
   634
     * descending order in its input array, and can take advantage of
jaroslav@557
   635
     * ascending and descending order in different parts of the the same
jaroslav@557
   636
     * input array.  It is well-suited to merging two or more sorted arrays:
jaroslav@557
   637
     * simply concatenate the arrays and sort the resulting array.
jaroslav@557
   638
     *
jaroslav@557
   639
     * <p>The implementation was adapted from Tim Peters's list sort for Python
jaroslav@557
   640
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
jaroslav@557
   641
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
jaroslav@557
   642
     * Sorting and Information Theoretic Complexity", in Proceedings of the
jaroslav@557
   643
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
jaroslav@557
   644
     * January 1993.
jaroslav@557
   645
     *
jaroslav@557
   646
     * @param a the array to be sorted
jaroslav@557
   647
     * @param c the comparator to determine the order of the array.  A
jaroslav@557
   648
     *        {@code null} value indicates that the elements'
jaroslav@557
   649
     *        {@linkplain Comparable natural ordering} should be used.
jaroslav@557
   650
     * @throws ClassCastException if the array contains elements that are
jaroslav@557
   651
     *         not <i>mutually comparable</i> using the specified comparator
jaroslav@557
   652
     * @throws IllegalArgumentException (optional) if the comparator is
jaroslav@557
   653
     *         found to violate the {@link Comparator} contract
jaroslav@557
   654
     */
jaroslav@557
   655
    public static <T> void sort(T[] a, Comparator<? super T> c) {
jaroslav@557
   656
        if (LegacyMergeSort.userRequested)
jaroslav@557
   657
            legacyMergeSort(a, c);
jaroslav@557
   658
        else
jaroslav@557
   659
            TimSort.sort(a, c);
jaroslav@557
   660
    }
jaroslav@557
   661
jaroslav@557
   662
    /** To be removed in a future release. */
jaroslav@557
   663
    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
jaroslav@557
   664
        T[] aux = a.clone();
jaroslav@557
   665
        if (c==null)
jaroslav@557
   666
            mergeSort(aux, a, 0, a.length, 0);
jaroslav@557
   667
        else
jaroslav@557
   668
            mergeSort(aux, a, 0, a.length, 0, c);
jaroslav@557
   669
    }
jaroslav@557
   670
jaroslav@557
   671
    /**
jaroslav@557
   672
     * Sorts the specified range of the specified array of objects according
jaroslav@557
   673
     * to the order induced by the specified comparator.  The range to be
jaroslav@557
   674
     * sorted extends from index {@code fromIndex}, inclusive, to index
jaroslav@557
   675
     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
jaroslav@557
   676
     * range to be sorted is empty.)  All elements in the range must be
jaroslav@557
   677
     * <i>mutually comparable</i> by the specified comparator (that is,
jaroslav@557
   678
     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
jaroslav@557
   679
     * for any elements {@code e1} and {@code e2} in the range).
jaroslav@557
   680
     *
jaroslav@557
   681
     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
jaroslav@557
   682
     * not be reordered as a result of the sort.
jaroslav@557
   683
     *
jaroslav@557
   684
     * <p>Implementation note: This implementation is a stable, adaptive,
jaroslav@557
   685
     * iterative mergesort that requires far fewer than n lg(n) comparisons
jaroslav@557
   686
     * when the input array is partially sorted, while offering the
jaroslav@557
   687
     * performance of a traditional mergesort when the input array is
jaroslav@557
   688
     * randomly ordered.  If the input array is nearly sorted, the
jaroslav@557
   689
     * implementation requires approximately n comparisons.  Temporary
jaroslav@557
   690
     * storage requirements vary from a small constant for nearly sorted
jaroslav@557
   691
     * input arrays to n/2 object references for randomly ordered input
jaroslav@557
   692
     * arrays.
jaroslav@557
   693
     *
jaroslav@557
   694
     * <p>The implementation takes equal advantage of ascending and
jaroslav@557
   695
     * descending order in its input array, and can take advantage of
jaroslav@557
   696
     * ascending and descending order in different parts of the the same
jaroslav@557
   697
     * input array.  It is well-suited to merging two or more sorted arrays:
jaroslav@557
   698
     * simply concatenate the arrays and sort the resulting array.
jaroslav@557
   699
     *
jaroslav@557
   700
     * <p>The implementation was adapted from Tim Peters's list sort for Python
jaroslav@557
   701
     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
jaroslav@557
   702
     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
jaroslav@557
   703
     * Sorting and Information Theoretic Complexity", in Proceedings of the
jaroslav@557
   704
     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
jaroslav@557
   705
     * January 1993.
jaroslav@557
   706
     *
jaroslav@557
   707
     * @param a the array to be sorted
jaroslav@557
   708
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
   709
     *        sorted
jaroslav@557
   710
     * @param toIndex the index of the last element (exclusive) to be sorted
jaroslav@557
   711
     * @param c the comparator to determine the order of the array.  A
jaroslav@557
   712
     *        {@code null} value indicates that the elements'
jaroslav@557
   713
     *        {@linkplain Comparable natural ordering} should be used.
jaroslav@557
   714
     * @throws ClassCastException if the array contains elements that are not
jaroslav@557
   715
     *         <i>mutually comparable</i> using the specified comparator.
jaroslav@557
   716
     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
jaroslav@557
   717
     *         (optional) if the comparator is found to violate the
jaroslav@557
   718
     *         {@link Comparator} contract
jaroslav@557
   719
     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
jaroslav@557
   720
     *         {@code toIndex > a.length}
jaroslav@557
   721
     */
jaroslav@557
   722
    public static <T> void sort(T[] a, int fromIndex, int toIndex,
jaroslav@557
   723
                                Comparator<? super T> c) {
jaroslav@557
   724
        if (LegacyMergeSort.userRequested)
jaroslav@557
   725
            legacyMergeSort(a, fromIndex, toIndex, c);
jaroslav@557
   726
        else
jaroslav@557
   727
            TimSort.sort(a, fromIndex, toIndex, c);
jaroslav@557
   728
    }
jaroslav@557
   729
jaroslav@557
   730
    /** To be removed in a future release. */
jaroslav@557
   731
    private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,
jaroslav@557
   732
                                            Comparator<? super T> c) {
jaroslav@557
   733
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   734
        T[] aux = copyOfRange(a, fromIndex, toIndex);
jaroslav@557
   735
        if (c==null)
jaroslav@557
   736
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
jaroslav@557
   737
        else
jaroslav@557
   738
            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
jaroslav@557
   739
    }
jaroslav@557
   740
jaroslav@557
   741
    /**
jaroslav@557
   742
     * Src is the source array that starts at index 0
jaroslav@557
   743
     * Dest is the (possibly larger) array destination with a possible offset
jaroslav@557
   744
     * low is the index in dest to start sorting
jaroslav@557
   745
     * high is the end index in dest to end sorting
jaroslav@557
   746
     * off is the offset into src corresponding to low in dest
jaroslav@557
   747
     * To be removed in a future release.
jaroslav@557
   748
     */
jaroslav@557
   749
    private static void mergeSort(Object[] src,
jaroslav@557
   750
                                  Object[] dest,
jaroslav@557
   751
                                  int low, int high, int off,
jaroslav@557
   752
                                  Comparator c) {
jaroslav@557
   753
        int length = high - low;
jaroslav@557
   754
jaroslav@557
   755
        // Insertion sort on smallest arrays
jaroslav@557
   756
        if (length < INSERTIONSORT_THRESHOLD) {
jaroslav@557
   757
            for (int i=low; i<high; i++)
jaroslav@557
   758
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
jaroslav@557
   759
                    swap(dest, j, j-1);
jaroslav@557
   760
            return;
jaroslav@557
   761
        }
jaroslav@557
   762
jaroslav@557
   763
        // Recursively sort halves of dest into src
jaroslav@557
   764
        int destLow  = low;
jaroslav@557
   765
        int destHigh = high;
jaroslav@557
   766
        low  += off;
jaroslav@557
   767
        high += off;
jaroslav@557
   768
        int mid = (low + high) >>> 1;
jaroslav@557
   769
        mergeSort(dest, src, low, mid, -off, c);
jaroslav@557
   770
        mergeSort(dest, src, mid, high, -off, c);
jaroslav@557
   771
jaroslav@557
   772
        // If list is already sorted, just copy from src to dest.  This is an
jaroslav@557
   773
        // optimization that results in faster sorts for nearly ordered lists.
jaroslav@557
   774
        if (c.compare(src[mid-1], src[mid]) <= 0) {
jaroslav@557
   775
           System.arraycopy(src, low, dest, destLow, length);
jaroslav@557
   776
           return;
jaroslav@557
   777
        }
jaroslav@557
   778
jaroslav@557
   779
        // Merge sorted halves (now in src) into dest
jaroslav@557
   780
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
jaroslav@557
   781
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
jaroslav@557
   782
                dest[i] = src[p++];
jaroslav@557
   783
            else
jaroslav@557
   784
                dest[i] = src[q++];
jaroslav@557
   785
        }
jaroslav@557
   786
    }
jaroslav@557
   787
jaroslav@557
   788
    /**
jaroslav@557
   789
     * Checks that {@code fromIndex} and {@code toIndex} are in
jaroslav@557
   790
     * the range and throws an appropriate exception, if they aren't.
jaroslav@557
   791
     */
jaroslav@557
   792
    private static void rangeCheck(int length, int fromIndex, int toIndex) {
jaroslav@557
   793
        if (fromIndex > toIndex) {
jaroslav@557
   794
            throw new IllegalArgumentException(
jaroslav@557
   795
                "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
jaroslav@557
   796
        }
jaroslav@557
   797
        if (fromIndex < 0) {
jaroslav@557
   798
            throw new ArrayIndexOutOfBoundsException(fromIndex);
jaroslav@557
   799
        }
jaroslav@557
   800
        if (toIndex > length) {
jaroslav@557
   801
            throw new ArrayIndexOutOfBoundsException(toIndex);
jaroslav@557
   802
        }
jaroslav@557
   803
    }
jaroslav@557
   804
jaroslav@557
   805
    // Searching
jaroslav@557
   806
jaroslav@557
   807
    /**
jaroslav@557
   808
     * Searches the specified array of longs for the specified value using the
jaroslav@557
   809
     * binary search algorithm.  The array must be sorted (as
jaroslav@557
   810
     * by the {@link #sort(long[])} method) prior to making this call.  If it
jaroslav@557
   811
     * is not sorted, the results are undefined.  If the array contains
jaroslav@557
   812
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
   813
     * one will be found.
jaroslav@557
   814
     *
jaroslav@557
   815
     * @param a the array to be searched
jaroslav@557
   816
     * @param key the value to be searched for
jaroslav@557
   817
     * @return index of the search key, if it is contained in the array;
jaroslav@557
   818
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
   819
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
   820
     *         key would be inserted into the array: the index of the first
jaroslav@557
   821
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
   822
     *         elements in the array are less than the specified key.  Note
jaroslav@557
   823
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
   824
     *         and only if the key is found.
jaroslav@557
   825
     */
jaroslav@557
   826
    public static int binarySearch(long[] a, long key) {
jaroslav@557
   827
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
   828
    }
jaroslav@557
   829
jaroslav@557
   830
    /**
jaroslav@557
   831
     * Searches a range of
jaroslav@557
   832
     * the specified array of longs for the specified value using the
jaroslav@557
   833
     * binary search algorithm.
jaroslav@557
   834
     * The range must be sorted (as
jaroslav@557
   835
     * by the {@link #sort(long[], int, int)} method)
jaroslav@557
   836
     * prior to making this call.  If it
jaroslav@557
   837
     * is not sorted, the results are undefined.  If the range contains
jaroslav@557
   838
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
   839
     * one will be found.
jaroslav@557
   840
     *
jaroslav@557
   841
     * @param a the array to be searched
jaroslav@557
   842
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
   843
     *          searched
jaroslav@557
   844
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
   845
     * @param key the value to be searched for
jaroslav@557
   846
     * @return index of the search key, if it is contained in the array
jaroslav@557
   847
     *         within the specified range;
jaroslav@557
   848
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
   849
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
   850
     *         key would be inserted into the array: the index of the first
jaroslav@557
   851
     *         element in the range greater than the key,
jaroslav@557
   852
     *         or <tt>toIndex</tt> if all
jaroslav@557
   853
     *         elements in the range are less than the specified key.  Note
jaroslav@557
   854
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
   855
     *         and only if the key is found.
jaroslav@557
   856
     * @throws IllegalArgumentException
jaroslav@557
   857
     *         if {@code fromIndex > toIndex}
jaroslav@557
   858
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   859
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
   860
     * @since 1.6
jaroslav@557
   861
     */
jaroslav@557
   862
    public static int binarySearch(long[] a, int fromIndex, int toIndex,
jaroslav@557
   863
                                   long key) {
jaroslav@557
   864
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   865
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
   866
    }
jaroslav@557
   867
jaroslav@557
   868
    // Like public version, but without range checks.
jaroslav@557
   869
    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
jaroslav@557
   870
                                     long key) {
jaroslav@557
   871
        int low = fromIndex;
jaroslav@557
   872
        int high = toIndex - 1;
jaroslav@557
   873
jaroslav@557
   874
        while (low <= high) {
jaroslav@557
   875
            int mid = (low + high) >>> 1;
jaroslav@557
   876
            long midVal = a[mid];
jaroslav@557
   877
jaroslav@557
   878
            if (midVal < key)
jaroslav@557
   879
                low = mid + 1;
jaroslav@557
   880
            else if (midVal > key)
jaroslav@557
   881
                high = mid - 1;
jaroslav@557
   882
            else
jaroslav@557
   883
                return mid; // key found
jaroslav@557
   884
        }
jaroslav@557
   885
        return -(low + 1);  // key not found.
jaroslav@557
   886
    }
jaroslav@557
   887
jaroslav@557
   888
    /**
jaroslav@557
   889
     * Searches the specified array of ints for the specified value using the
jaroslav@557
   890
     * binary search algorithm.  The array must be sorted (as
jaroslav@557
   891
     * by the {@link #sort(int[])} method) prior to making this call.  If it
jaroslav@557
   892
     * is not sorted, the results are undefined.  If the array contains
jaroslav@557
   893
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
   894
     * one will be found.
jaroslav@557
   895
     *
jaroslav@557
   896
     * @param a the array to be searched
jaroslav@557
   897
     * @param key the value to be searched for
jaroslav@557
   898
     * @return index of the search key, if it is contained in the array;
jaroslav@557
   899
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
   900
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
   901
     *         key would be inserted into the array: the index of the first
jaroslav@557
   902
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
   903
     *         elements in the array are less than the specified key.  Note
jaroslav@557
   904
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
   905
     *         and only if the key is found.
jaroslav@557
   906
     */
jaroslav@557
   907
    public static int binarySearch(int[] a, int key) {
jaroslav@557
   908
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
   909
    }
jaroslav@557
   910
jaroslav@557
   911
    /**
jaroslav@557
   912
     * Searches a range of
jaroslav@557
   913
     * the specified array of ints for the specified value using the
jaroslav@557
   914
     * binary search algorithm.
jaroslav@557
   915
     * The range must be sorted (as
jaroslav@557
   916
     * by the {@link #sort(int[], int, int)} method)
jaroslav@557
   917
     * prior to making this call.  If it
jaroslav@557
   918
     * is not sorted, the results are undefined.  If the range contains
jaroslav@557
   919
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
   920
     * one will be found.
jaroslav@557
   921
     *
jaroslav@557
   922
     * @param a the array to be searched
jaroslav@557
   923
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
   924
     *          searched
jaroslav@557
   925
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
   926
     * @param key the value to be searched for
jaroslav@557
   927
     * @return index of the search key, if it is contained in the array
jaroslav@557
   928
     *         within the specified range;
jaroslav@557
   929
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
   930
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
   931
     *         key would be inserted into the array: the index of the first
jaroslav@557
   932
     *         element in the range greater than the key,
jaroslav@557
   933
     *         or <tt>toIndex</tt> if all
jaroslav@557
   934
     *         elements in the range are less than the specified key.  Note
jaroslav@557
   935
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
   936
     *         and only if the key is found.
jaroslav@557
   937
     * @throws IllegalArgumentException
jaroslav@557
   938
     *         if {@code fromIndex > toIndex}
jaroslav@557
   939
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
   940
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
   941
     * @since 1.6
jaroslav@557
   942
     */
jaroslav@557
   943
    public static int binarySearch(int[] a, int fromIndex, int toIndex,
jaroslav@557
   944
                                   int key) {
jaroslav@557
   945
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
   946
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
   947
    }
jaroslav@557
   948
jaroslav@557
   949
    // Like public version, but without range checks.
jaroslav@557
   950
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
jaroslav@557
   951
                                     int key) {
jaroslav@557
   952
        int low = fromIndex;
jaroslav@557
   953
        int high = toIndex - 1;
jaroslav@557
   954
jaroslav@557
   955
        while (low <= high) {
jaroslav@557
   956
            int mid = (low + high) >>> 1;
jaroslav@557
   957
            int midVal = a[mid];
jaroslav@557
   958
jaroslav@557
   959
            if (midVal < key)
jaroslav@557
   960
                low = mid + 1;
jaroslav@557
   961
            else if (midVal > key)
jaroslav@557
   962
                high = mid - 1;
jaroslav@557
   963
            else
jaroslav@557
   964
                return mid; // key found
jaroslav@557
   965
        }
jaroslav@557
   966
        return -(low + 1);  // key not found.
jaroslav@557
   967
    }
jaroslav@557
   968
jaroslav@557
   969
    /**
jaroslav@557
   970
     * Searches the specified array of shorts for the specified value using
jaroslav@557
   971
     * the binary search algorithm.  The array must be sorted
jaroslav@557
   972
     * (as by the {@link #sort(short[])} method) prior to making this call.  If
jaroslav@557
   973
     * it is not sorted, the results are undefined.  If the array contains
jaroslav@557
   974
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
   975
     * one will be found.
jaroslav@557
   976
     *
jaroslav@557
   977
     * @param a the array to be searched
jaroslav@557
   978
     * @param key the value to be searched for
jaroslav@557
   979
     * @return index of the search key, if it is contained in the array;
jaroslav@557
   980
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
   981
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
   982
     *         key would be inserted into the array: the index of the first
jaroslav@557
   983
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
   984
     *         elements in the array are less than the specified key.  Note
jaroslav@557
   985
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
   986
     *         and only if the key is found.
jaroslav@557
   987
     */
jaroslav@557
   988
    public static int binarySearch(short[] a, short key) {
jaroslav@557
   989
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
   990
    }
jaroslav@557
   991
jaroslav@557
   992
    /**
jaroslav@557
   993
     * Searches a range of
jaroslav@557
   994
     * the specified array of shorts for the specified value using
jaroslav@557
   995
     * the binary search algorithm.
jaroslav@557
   996
     * The range must be sorted
jaroslav@557
   997
     * (as by the {@link #sort(short[], int, int)} method)
jaroslav@557
   998
     * prior to making this call.  If
jaroslav@557
   999
     * it is not sorted, the results are undefined.  If the range contains
jaroslav@557
  1000
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1001
     * one will be found.
jaroslav@557
  1002
     *
jaroslav@557
  1003
     * @param a the array to be searched
jaroslav@557
  1004
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1005
     *          searched
jaroslav@557
  1006
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
  1007
     * @param key the value to be searched for
jaroslav@557
  1008
     * @return index of the search key, if it is contained in the array
jaroslav@557
  1009
     *         within the specified range;
jaroslav@557
  1010
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1011
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1012
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1013
     *         element in the range greater than the key,
jaroslav@557
  1014
     *         or <tt>toIndex</tt> if all
jaroslav@557
  1015
     *         elements in the range are less than the specified key.  Note
jaroslav@557
  1016
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1017
     *         and only if the key is found.
jaroslav@557
  1018
     * @throws IllegalArgumentException
jaroslav@557
  1019
     *         if {@code fromIndex > toIndex}
jaroslav@557
  1020
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
  1021
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
  1022
     * @since 1.6
jaroslav@557
  1023
     */
jaroslav@557
  1024
    public static int binarySearch(short[] a, int fromIndex, int toIndex,
jaroslav@557
  1025
                                   short key) {
jaroslav@557
  1026
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1027
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
  1028
    }
jaroslav@557
  1029
jaroslav@557
  1030
    // Like public version, but without range checks.
jaroslav@557
  1031
    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
jaroslav@557
  1032
                                     short key) {
jaroslav@557
  1033
        int low = fromIndex;
jaroslav@557
  1034
        int high = toIndex - 1;
jaroslav@557
  1035
jaroslav@557
  1036
        while (low <= high) {
jaroslav@557
  1037
            int mid = (low + high) >>> 1;
jaroslav@557
  1038
            short midVal = a[mid];
jaroslav@557
  1039
jaroslav@557
  1040
            if (midVal < key)
jaroslav@557
  1041
                low = mid + 1;
jaroslav@557
  1042
            else if (midVal > key)
jaroslav@557
  1043
                high = mid - 1;
jaroslav@557
  1044
            else
jaroslav@557
  1045
                return mid; // key found
jaroslav@557
  1046
        }
jaroslav@557
  1047
        return -(low + 1);  // key not found.
jaroslav@557
  1048
    }
jaroslav@557
  1049
jaroslav@557
  1050
    /**
jaroslav@557
  1051
     * Searches the specified array of chars for the specified value using the
jaroslav@557
  1052
     * binary search algorithm.  The array must be sorted (as
jaroslav@557
  1053
     * by the {@link #sort(char[])} method) prior to making this call.  If it
jaroslav@557
  1054
     * is not sorted, the results are undefined.  If the array contains
jaroslav@557
  1055
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1056
     * one will be found.
jaroslav@557
  1057
     *
jaroslav@557
  1058
     * @param a the array to be searched
jaroslav@557
  1059
     * @param key the value to be searched for
jaroslav@557
  1060
     * @return index of the search key, if it is contained in the array;
jaroslav@557
  1061
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1062
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1063
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1064
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
  1065
     *         elements in the array are less than the specified key.  Note
jaroslav@557
  1066
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1067
     *         and only if the key is found.
jaroslav@557
  1068
     */
jaroslav@557
  1069
    public static int binarySearch(char[] a, char key) {
jaroslav@557
  1070
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
  1071
    }
jaroslav@557
  1072
jaroslav@557
  1073
    /**
jaroslav@557
  1074
     * Searches a range of
jaroslav@557
  1075
     * the specified array of chars for the specified value using the
jaroslav@557
  1076
     * binary search algorithm.
jaroslav@557
  1077
     * The range must be sorted (as
jaroslav@557
  1078
     * by the {@link #sort(char[], int, int)} method)
jaroslav@557
  1079
     * prior to making this call.  If it
jaroslav@557
  1080
     * is not sorted, the results are undefined.  If the range contains
jaroslav@557
  1081
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1082
     * one will be found.
jaroslav@557
  1083
     *
jaroslav@557
  1084
     * @param a the array to be searched
jaroslav@557
  1085
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1086
     *          searched
jaroslav@557
  1087
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
  1088
     * @param key the value to be searched for
jaroslav@557
  1089
     * @return index of the search key, if it is contained in the array
jaroslav@557
  1090
     *         within the specified range;
jaroslav@557
  1091
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1092
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1093
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1094
     *         element in the range greater than the key,
jaroslav@557
  1095
     *         or <tt>toIndex</tt> if all
jaroslav@557
  1096
     *         elements in the range are less than the specified key.  Note
jaroslav@557
  1097
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1098
     *         and only if the key is found.
jaroslav@557
  1099
     * @throws IllegalArgumentException
jaroslav@557
  1100
     *         if {@code fromIndex > toIndex}
jaroslav@557
  1101
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
  1102
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
  1103
     * @since 1.6
jaroslav@557
  1104
     */
jaroslav@557
  1105
    public static int binarySearch(char[] a, int fromIndex, int toIndex,
jaroslav@557
  1106
                                   char key) {
jaroslav@557
  1107
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1108
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
  1109
    }
jaroslav@557
  1110
jaroslav@557
  1111
    // Like public version, but without range checks.
jaroslav@557
  1112
    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
jaroslav@557
  1113
                                     char key) {
jaroslav@557
  1114
        int low = fromIndex;
jaroslav@557
  1115
        int high = toIndex - 1;
jaroslav@557
  1116
jaroslav@557
  1117
        while (low <= high) {
jaroslav@557
  1118
            int mid = (low + high) >>> 1;
jaroslav@557
  1119
            char midVal = a[mid];
jaroslav@557
  1120
jaroslav@557
  1121
            if (midVal < key)
jaroslav@557
  1122
                low = mid + 1;
jaroslav@557
  1123
            else if (midVal > key)
jaroslav@557
  1124
                high = mid - 1;
jaroslav@557
  1125
            else
jaroslav@557
  1126
                return mid; // key found
jaroslav@557
  1127
        }
jaroslav@557
  1128
        return -(low + 1);  // key not found.
jaroslav@557
  1129
    }
jaroslav@557
  1130
jaroslav@557
  1131
    /**
jaroslav@557
  1132
     * Searches the specified array of bytes for the specified value using the
jaroslav@557
  1133
     * binary search algorithm.  The array must be sorted (as
jaroslav@557
  1134
     * by the {@link #sort(byte[])} method) prior to making this call.  If it
jaroslav@557
  1135
     * is not sorted, the results are undefined.  If the array contains
jaroslav@557
  1136
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1137
     * one will be found.
jaroslav@557
  1138
     *
jaroslav@557
  1139
     * @param a the array to be searched
jaroslav@557
  1140
     * @param key the value to be searched for
jaroslav@557
  1141
     * @return index of the search key, if it is contained in the array;
jaroslav@557
  1142
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1143
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1144
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1145
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
  1146
     *         elements in the array are less than the specified key.  Note
jaroslav@557
  1147
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1148
     *         and only if the key is found.
jaroslav@557
  1149
     */
jaroslav@557
  1150
    public static int binarySearch(byte[] a, byte key) {
jaroslav@557
  1151
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
  1152
    }
jaroslav@557
  1153
jaroslav@557
  1154
    /**
jaroslav@557
  1155
     * Searches a range of
jaroslav@557
  1156
     * the specified array of bytes for the specified value using the
jaroslav@557
  1157
     * binary search algorithm.
jaroslav@557
  1158
     * The range must be sorted (as
jaroslav@557
  1159
     * by the {@link #sort(byte[], int, int)} method)
jaroslav@557
  1160
     * prior to making this call.  If it
jaroslav@557
  1161
     * is not sorted, the results are undefined.  If the range contains
jaroslav@557
  1162
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1163
     * one will be found.
jaroslav@557
  1164
     *
jaroslav@557
  1165
     * @param a the array to be searched
jaroslav@557
  1166
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1167
     *          searched
jaroslav@557
  1168
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
  1169
     * @param key the value to be searched for
jaroslav@557
  1170
     * @return index of the search key, if it is contained in the array
jaroslav@557
  1171
     *         within the specified range;
jaroslav@557
  1172
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1173
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1174
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1175
     *         element in the range greater than the key,
jaroslav@557
  1176
     *         or <tt>toIndex</tt> if all
jaroslav@557
  1177
     *         elements in the range are less than the specified key.  Note
jaroslav@557
  1178
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1179
     *         and only if the key is found.
jaroslav@557
  1180
     * @throws IllegalArgumentException
jaroslav@557
  1181
     *         if {@code fromIndex > toIndex}
jaroslav@557
  1182
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
  1183
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
  1184
     * @since 1.6
jaroslav@557
  1185
     */
jaroslav@557
  1186
    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
jaroslav@557
  1187
                                   byte key) {
jaroslav@557
  1188
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1189
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
  1190
    }
jaroslav@557
  1191
jaroslav@557
  1192
    // Like public version, but without range checks.
jaroslav@557
  1193
    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
jaroslav@557
  1194
                                     byte key) {
jaroslav@557
  1195
        int low = fromIndex;
jaroslav@557
  1196
        int high = toIndex - 1;
jaroslav@557
  1197
jaroslav@557
  1198
        while (low <= high) {
jaroslav@557
  1199
            int mid = (low + high) >>> 1;
jaroslav@557
  1200
            byte midVal = a[mid];
jaroslav@557
  1201
jaroslav@557
  1202
            if (midVal < key)
jaroslav@557
  1203
                low = mid + 1;
jaroslav@557
  1204
            else if (midVal > key)
jaroslav@557
  1205
                high = mid - 1;
jaroslav@557
  1206
            else
jaroslav@557
  1207
                return mid; // key found
jaroslav@557
  1208
        }
jaroslav@557
  1209
        return -(low + 1);  // key not found.
jaroslav@557
  1210
    }
jaroslav@557
  1211
jaroslav@557
  1212
    /**
jaroslav@557
  1213
     * Searches the specified array of doubles for the specified value using
jaroslav@557
  1214
     * the binary search algorithm.  The array must be sorted
jaroslav@557
  1215
     * (as by the {@link #sort(double[])} method) prior to making this call.
jaroslav@557
  1216
     * If it is not sorted, the results are undefined.  If the array contains
jaroslav@557
  1217
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1218
     * one will be found.  This method considers all NaN values to be
jaroslav@557
  1219
     * equivalent and equal.
jaroslav@557
  1220
     *
jaroslav@557
  1221
     * @param a the array to be searched
jaroslav@557
  1222
     * @param key the value to be searched for
jaroslav@557
  1223
     * @return index of the search key, if it is contained in the array;
jaroslav@557
  1224
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1225
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1226
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1227
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
  1228
     *         elements in the array are less than the specified key.  Note
jaroslav@557
  1229
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1230
     *         and only if the key is found.
jaroslav@557
  1231
     */
jaroslav@557
  1232
    public static int binarySearch(double[] a, double key) {
jaroslav@557
  1233
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
  1234
    }
jaroslav@557
  1235
jaroslav@557
  1236
    /**
jaroslav@557
  1237
     * Searches a range of
jaroslav@557
  1238
     * the specified array of doubles for the specified value using
jaroslav@557
  1239
     * the binary search algorithm.
jaroslav@557
  1240
     * The range must be sorted
jaroslav@557
  1241
     * (as by the {@link #sort(double[], int, int)} method)
jaroslav@557
  1242
     * prior to making this call.
jaroslav@557
  1243
     * If it is not sorted, the results are undefined.  If the range contains
jaroslav@557
  1244
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1245
     * one will be found.  This method considers all NaN values to be
jaroslav@557
  1246
     * equivalent and equal.
jaroslav@557
  1247
     *
jaroslav@557
  1248
     * @param a the array to be searched
jaroslav@557
  1249
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1250
     *          searched
jaroslav@557
  1251
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
  1252
     * @param key the value to be searched for
jaroslav@557
  1253
     * @return index of the search key, if it is contained in the array
jaroslav@557
  1254
     *         within the specified range;
jaroslav@557
  1255
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1256
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1257
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1258
     *         element in the range greater than the key,
jaroslav@557
  1259
     *         or <tt>toIndex</tt> if all
jaroslav@557
  1260
     *         elements in the range are less than the specified key.  Note
jaroslav@557
  1261
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1262
     *         and only if the key is found.
jaroslav@557
  1263
     * @throws IllegalArgumentException
jaroslav@557
  1264
     *         if {@code fromIndex > toIndex}
jaroslav@557
  1265
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
  1266
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
  1267
     * @since 1.6
jaroslav@557
  1268
     */
jaroslav@557
  1269
    public static int binarySearch(double[] a, int fromIndex, int toIndex,
jaroslav@557
  1270
                                   double key) {
jaroslav@557
  1271
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1272
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
  1273
    }
jaroslav@557
  1274
jaroslav@557
  1275
    // Like public version, but without range checks.
jaroslav@557
  1276
    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
jaroslav@557
  1277
                                     double key) {
jaroslav@557
  1278
        int low = fromIndex;
jaroslav@557
  1279
        int high = toIndex - 1;
jaroslav@557
  1280
jaroslav@557
  1281
        while (low <= high) {
jaroslav@557
  1282
            int mid = (low + high) >>> 1;
jaroslav@557
  1283
            double midVal = a[mid];
jaroslav@557
  1284
jaroslav@557
  1285
            if (midVal < key)
jaroslav@557
  1286
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
jaroslav@557
  1287
            else if (midVal > key)
jaroslav@557
  1288
                high = mid - 1; // Neither val is NaN, thisVal is larger
jaroslav@557
  1289
            else {
jaroslav@557
  1290
                long midBits = Double.doubleToLongBits(midVal);
jaroslav@557
  1291
                long keyBits = Double.doubleToLongBits(key);
jaroslav@557
  1292
                if (midBits == keyBits)     // Values are equal
jaroslav@557
  1293
                    return mid;             // Key found
jaroslav@557
  1294
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
jaroslav@557
  1295
                    low = mid + 1;
jaroslav@557
  1296
                else                        // (0.0, -0.0) or (NaN, !NaN)
jaroslav@557
  1297
                    high = mid - 1;
jaroslav@557
  1298
            }
jaroslav@557
  1299
        }
jaroslav@557
  1300
        return -(low + 1);  // key not found.
jaroslav@557
  1301
    }
jaroslav@557
  1302
jaroslav@557
  1303
    /**
jaroslav@557
  1304
     * Searches the specified array of floats for the specified value using
jaroslav@557
  1305
     * the binary search algorithm. The array must be sorted
jaroslav@557
  1306
     * (as by the {@link #sort(float[])} method) prior to making this call. If
jaroslav@557
  1307
     * it is not sorted, the results are undefined. If the array contains
jaroslav@557
  1308
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1309
     * one will be found. This method considers all NaN values to be
jaroslav@557
  1310
     * equivalent and equal.
jaroslav@557
  1311
     *
jaroslav@557
  1312
     * @param a the array to be searched
jaroslav@557
  1313
     * @param key the value to be searched for
jaroslav@557
  1314
     * @return index of the search key, if it is contained in the array;
jaroslav@557
  1315
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
jaroslav@557
  1316
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1317
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1318
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
  1319
     *         elements in the array are less than the specified key. Note
jaroslav@557
  1320
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1321
     *         and only if the key is found.
jaroslav@557
  1322
     */
jaroslav@557
  1323
    public static int binarySearch(float[] a, float key) {
jaroslav@557
  1324
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
  1325
    }
jaroslav@557
  1326
jaroslav@557
  1327
    /**
jaroslav@557
  1328
     * Searches a range of
jaroslav@557
  1329
     * the specified array of floats for the specified value using
jaroslav@557
  1330
     * the binary search algorithm.
jaroslav@557
  1331
     * The range must be sorted
jaroslav@557
  1332
     * (as by the {@link #sort(float[], int, int)} method)
jaroslav@557
  1333
     * prior to making this call. If
jaroslav@557
  1334
     * it is not sorted, the results are undefined. If the range contains
jaroslav@557
  1335
     * multiple elements with the specified value, there is no guarantee which
jaroslav@557
  1336
     * one will be found. This method considers all NaN values to be
jaroslav@557
  1337
     * equivalent and equal.
jaroslav@557
  1338
     *
jaroslav@557
  1339
     * @param a the array to be searched
jaroslav@557
  1340
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1341
     *          searched
jaroslav@557
  1342
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
  1343
     * @param key the value to be searched for
jaroslav@557
  1344
     * @return index of the search key, if it is contained in the array
jaroslav@557
  1345
     *         within the specified range;
jaroslav@557
  1346
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
jaroslav@557
  1347
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1348
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1349
     *         element in the range greater than the key,
jaroslav@557
  1350
     *         or <tt>toIndex</tt> if all
jaroslav@557
  1351
     *         elements in the range are less than the specified key. Note
jaroslav@557
  1352
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1353
     *         and only if the key is found.
jaroslav@557
  1354
     * @throws IllegalArgumentException
jaroslav@557
  1355
     *         if {@code fromIndex > toIndex}
jaroslav@557
  1356
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
  1357
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
  1358
     * @since 1.6
jaroslav@557
  1359
     */
jaroslav@557
  1360
    public static int binarySearch(float[] a, int fromIndex, int toIndex,
jaroslav@557
  1361
                                   float key) {
jaroslav@557
  1362
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1363
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
  1364
    }
jaroslav@557
  1365
jaroslav@557
  1366
    // Like public version, but without range checks.
jaroslav@557
  1367
    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
jaroslav@557
  1368
                                     float key) {
jaroslav@557
  1369
        int low = fromIndex;
jaroslav@557
  1370
        int high = toIndex - 1;
jaroslav@557
  1371
jaroslav@557
  1372
        while (low <= high) {
jaroslav@557
  1373
            int mid = (low + high) >>> 1;
jaroslav@557
  1374
            float midVal = a[mid];
jaroslav@557
  1375
jaroslav@557
  1376
            if (midVal < key)
jaroslav@557
  1377
                low = mid + 1;  // Neither val is NaN, thisVal is smaller
jaroslav@557
  1378
            else if (midVal > key)
jaroslav@557
  1379
                high = mid - 1; // Neither val is NaN, thisVal is larger
jaroslav@557
  1380
            else {
jaroslav@557
  1381
                int midBits = Float.floatToIntBits(midVal);
jaroslav@557
  1382
                int keyBits = Float.floatToIntBits(key);
jaroslav@557
  1383
                if (midBits == keyBits)     // Values are equal
jaroslav@557
  1384
                    return mid;             // Key found
jaroslav@557
  1385
                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
jaroslav@557
  1386
                    low = mid + 1;
jaroslav@557
  1387
                else                        // (0.0, -0.0) or (NaN, !NaN)
jaroslav@557
  1388
                    high = mid - 1;
jaroslav@557
  1389
            }
jaroslav@557
  1390
        }
jaroslav@557
  1391
        return -(low + 1);  // key not found.
jaroslav@557
  1392
    }
jaroslav@557
  1393
jaroslav@557
  1394
    /**
jaroslav@557
  1395
     * Searches the specified array for the specified object using the binary
jaroslav@557
  1396
     * search algorithm. The array must be sorted into ascending order
jaroslav@557
  1397
     * according to the
jaroslav@557
  1398
     * {@linkplain Comparable natural ordering}
jaroslav@557
  1399
     * of its elements (as by the
jaroslav@557
  1400
     * {@link #sort(Object[])} method) prior to making this call.
jaroslav@557
  1401
     * If it is not sorted, the results are undefined.
jaroslav@557
  1402
     * (If the array contains elements that are not mutually comparable (for
jaroslav@557
  1403
     * example, strings and integers), it <i>cannot</i> be sorted according
jaroslav@557
  1404
     * to the natural ordering of its elements, hence results are undefined.)
jaroslav@557
  1405
     * If the array contains multiple
jaroslav@557
  1406
     * elements equal to the specified object, there is no guarantee which
jaroslav@557
  1407
     * one will be found.
jaroslav@557
  1408
     *
jaroslav@557
  1409
     * @param a the array to be searched
jaroslav@557
  1410
     * @param key the value to be searched for
jaroslav@557
  1411
     * @return index of the search key, if it is contained in the array;
jaroslav@557
  1412
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1413
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1414
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1415
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
  1416
     *         elements in the array are less than the specified key.  Note
jaroslav@557
  1417
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1418
     *         and only if the key is found.
jaroslav@557
  1419
     * @throws ClassCastException if the search key is not comparable to the
jaroslav@557
  1420
     *         elements of the array.
jaroslav@557
  1421
     */
jaroslav@557
  1422
    public static int binarySearch(Object[] a, Object key) {
jaroslav@557
  1423
        return binarySearch0(a, 0, a.length, key);
jaroslav@557
  1424
    }
jaroslav@557
  1425
jaroslav@557
  1426
    /**
jaroslav@557
  1427
     * Searches a range of
jaroslav@557
  1428
     * the specified array for the specified object using the binary
jaroslav@557
  1429
     * search algorithm.
jaroslav@557
  1430
     * The range must be sorted into ascending order
jaroslav@557
  1431
     * according to the
jaroslav@557
  1432
     * {@linkplain Comparable natural ordering}
jaroslav@557
  1433
     * of its elements (as by the
jaroslav@557
  1434
     * {@link #sort(Object[], int, int)} method) prior to making this
jaroslav@557
  1435
     * call.  If it is not sorted, the results are undefined.
jaroslav@557
  1436
     * (If the range contains elements that are not mutually comparable (for
jaroslav@557
  1437
     * example, strings and integers), it <i>cannot</i> be sorted according
jaroslav@557
  1438
     * to the natural ordering of its elements, hence results are undefined.)
jaroslav@557
  1439
     * If the range contains multiple
jaroslav@557
  1440
     * elements equal to the specified object, there is no guarantee which
jaroslav@557
  1441
     * one will be found.
jaroslav@557
  1442
     *
jaroslav@557
  1443
     * @param a the array to be searched
jaroslav@557
  1444
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1445
     *          searched
jaroslav@557
  1446
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
  1447
     * @param key the value to be searched for
jaroslav@557
  1448
     * @return index of the search key, if it is contained in the array
jaroslav@557
  1449
     *         within the specified range;
jaroslav@557
  1450
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1451
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1452
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1453
     *         element in the range greater than the key,
jaroslav@557
  1454
     *         or <tt>toIndex</tt> if all
jaroslav@557
  1455
     *         elements in the range are less than the specified key.  Note
jaroslav@557
  1456
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1457
     *         and only if the key is found.
jaroslav@557
  1458
     * @throws ClassCastException if the search key is not comparable to the
jaroslav@557
  1459
     *         elements of the array within the specified range.
jaroslav@557
  1460
     * @throws IllegalArgumentException
jaroslav@557
  1461
     *         if {@code fromIndex > toIndex}
jaroslav@557
  1462
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
  1463
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
  1464
     * @since 1.6
jaroslav@557
  1465
     */
jaroslav@557
  1466
    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
jaroslav@557
  1467
                                   Object key) {
jaroslav@557
  1468
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1469
        return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
  1470
    }
jaroslav@557
  1471
jaroslav@557
  1472
    // Like public version, but without range checks.
jaroslav@557
  1473
    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
jaroslav@557
  1474
                                     Object key) {
jaroslav@557
  1475
        int low = fromIndex;
jaroslav@557
  1476
        int high = toIndex - 1;
jaroslav@557
  1477
jaroslav@557
  1478
        while (low <= high) {
jaroslav@557
  1479
            int mid = (low + high) >>> 1;
jaroslav@557
  1480
            Comparable midVal = (Comparable)a[mid];
jaroslav@557
  1481
            int cmp = midVal.compareTo(key);
jaroslav@557
  1482
jaroslav@557
  1483
            if (cmp < 0)
jaroslav@557
  1484
                low = mid + 1;
jaroslav@557
  1485
            else if (cmp > 0)
jaroslav@557
  1486
                high = mid - 1;
jaroslav@557
  1487
            else
jaroslav@557
  1488
                return mid; // key found
jaroslav@557
  1489
        }
jaroslav@557
  1490
        return -(low + 1);  // key not found.
jaroslav@557
  1491
    }
jaroslav@557
  1492
jaroslav@557
  1493
    /**
jaroslav@557
  1494
     * Searches the specified array for the specified object using the binary
jaroslav@557
  1495
     * search algorithm.  The array must be sorted into ascending order
jaroslav@557
  1496
     * according to the specified comparator (as by the
jaroslav@557
  1497
     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
jaroslav@557
  1498
     * method) prior to making this call.  If it is
jaroslav@557
  1499
     * not sorted, the results are undefined.
jaroslav@557
  1500
     * If the array contains multiple
jaroslav@557
  1501
     * elements equal to the specified object, there is no guarantee which one
jaroslav@557
  1502
     * will be found.
jaroslav@557
  1503
     *
jaroslav@557
  1504
     * @param a the array to be searched
jaroslav@557
  1505
     * @param key the value to be searched for
jaroslav@557
  1506
     * @param c the comparator by which the array is ordered.  A
jaroslav@557
  1507
     *        <tt>null</tt> value indicates that the elements'
jaroslav@557
  1508
     *        {@linkplain Comparable natural ordering} should be used.
jaroslav@557
  1509
     * @return index of the search key, if it is contained in the array;
jaroslav@557
  1510
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1511
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1512
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1513
     *         element greater than the key, or <tt>a.length</tt> if all
jaroslav@557
  1514
     *         elements in the array are less than the specified key.  Note
jaroslav@557
  1515
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1516
     *         and only if the key is found.
jaroslav@557
  1517
     * @throws ClassCastException if the array contains elements that are not
jaroslav@557
  1518
     *         <i>mutually comparable</i> using the specified comparator,
jaroslav@557
  1519
     *         or the search key is not comparable to the
jaroslav@557
  1520
     *         elements of the array using this comparator.
jaroslav@557
  1521
     */
jaroslav@557
  1522
    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
jaroslav@557
  1523
        return binarySearch0(a, 0, a.length, key, c);
jaroslav@557
  1524
    }
jaroslav@557
  1525
jaroslav@557
  1526
    /**
jaroslav@557
  1527
     * Searches a range of
jaroslav@557
  1528
     * the specified array for the specified object using the binary
jaroslav@557
  1529
     * search algorithm.
jaroslav@557
  1530
     * The range must be sorted into ascending order
jaroslav@557
  1531
     * according to the specified comparator (as by the
jaroslav@557
  1532
     * {@link #sort(Object[], int, int, Comparator)
jaroslav@557
  1533
     * sort(T[], int, int, Comparator)}
jaroslav@557
  1534
     * method) prior to making this call.
jaroslav@557
  1535
     * If it is not sorted, the results are undefined.
jaroslav@557
  1536
     * If the range contains multiple elements equal to the specified object,
jaroslav@557
  1537
     * there is no guarantee which one will be found.
jaroslav@557
  1538
     *
jaroslav@557
  1539
     * @param a the array to be searched
jaroslav@557
  1540
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1541
     *          searched
jaroslav@557
  1542
     * @param toIndex the index of the last element (exclusive) to be searched
jaroslav@557
  1543
     * @param key the value to be searched for
jaroslav@557
  1544
     * @param c the comparator by which the array is ordered.  A
jaroslav@557
  1545
     *        <tt>null</tt> value indicates that the elements'
jaroslav@557
  1546
     *        {@linkplain Comparable natural ordering} should be used.
jaroslav@557
  1547
     * @return index of the search key, if it is contained in the array
jaroslav@557
  1548
     *         within the specified range;
jaroslav@557
  1549
     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
jaroslav@557
  1550
     *         <i>insertion point</i> is defined as the point at which the
jaroslav@557
  1551
     *         key would be inserted into the array: the index of the first
jaroslav@557
  1552
     *         element in the range greater than the key,
jaroslav@557
  1553
     *         or <tt>toIndex</tt> if all
jaroslav@557
  1554
     *         elements in the range are less than the specified key.  Note
jaroslav@557
  1555
     *         that this guarantees that the return value will be &gt;= 0 if
jaroslav@557
  1556
     *         and only if the key is found.
jaroslav@557
  1557
     * @throws ClassCastException if the range contains elements that are not
jaroslav@557
  1558
     *         <i>mutually comparable</i> using the specified comparator,
jaroslav@557
  1559
     *         or the search key is not comparable to the
jaroslav@557
  1560
     *         elements in the range using this comparator.
jaroslav@557
  1561
     * @throws IllegalArgumentException
jaroslav@557
  1562
     *         if {@code fromIndex > toIndex}
jaroslav@557
  1563
     * @throws ArrayIndexOutOfBoundsException
jaroslav@557
  1564
     *         if {@code fromIndex < 0 or toIndex > a.length}
jaroslav@557
  1565
     * @since 1.6
jaroslav@557
  1566
     */
jaroslav@557
  1567
    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
jaroslav@557
  1568
                                       T key, Comparator<? super T> c) {
jaroslav@557
  1569
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1570
        return binarySearch0(a, fromIndex, toIndex, key, c);
jaroslav@557
  1571
    }
jaroslav@557
  1572
jaroslav@557
  1573
    // Like public version, but without range checks.
jaroslav@557
  1574
    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
jaroslav@557
  1575
                                         T key, Comparator<? super T> c) {
jaroslav@557
  1576
        if (c == null) {
jaroslav@557
  1577
            return binarySearch0(a, fromIndex, toIndex, key);
jaroslav@557
  1578
        }
jaroslav@557
  1579
        int low = fromIndex;
jaroslav@557
  1580
        int high = toIndex - 1;
jaroslav@557
  1581
jaroslav@557
  1582
        while (low <= high) {
jaroslav@557
  1583
            int mid = (low + high) >>> 1;
jaroslav@557
  1584
            T midVal = a[mid];
jaroslav@557
  1585
            int cmp = c.compare(midVal, key);
jaroslav@557
  1586
            if (cmp < 0)
jaroslav@557
  1587
                low = mid + 1;
jaroslav@557
  1588
            else if (cmp > 0)
jaroslav@557
  1589
                high = mid - 1;
jaroslav@557
  1590
            else
jaroslav@557
  1591
                return mid; // key found
jaroslav@557
  1592
        }
jaroslav@557
  1593
        return -(low + 1);  // key not found.
jaroslav@557
  1594
    }
jaroslav@557
  1595
jaroslav@557
  1596
    // Equality Testing
jaroslav@557
  1597
jaroslav@557
  1598
    /**
jaroslav@557
  1599
     * Returns <tt>true</tt> if the two specified arrays of longs are
jaroslav@557
  1600
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1601
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1602
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1603
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1604
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1605
     *
jaroslav@557
  1606
     * @param a one array to be tested for equality
jaroslav@557
  1607
     * @param a2 the other array to be tested for equality
jaroslav@557
  1608
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1609
     */
jaroslav@557
  1610
    public static boolean equals(long[] a, long[] a2) {
jaroslav@557
  1611
        if (a==a2)
jaroslav@557
  1612
            return true;
jaroslav@557
  1613
        if (a==null || a2==null)
jaroslav@557
  1614
            return false;
jaroslav@557
  1615
jaroslav@557
  1616
        int length = a.length;
jaroslav@557
  1617
        if (a2.length != length)
jaroslav@557
  1618
            return false;
jaroslav@557
  1619
jaroslav@557
  1620
        for (int i=0; i<length; i++)
jaroslav@557
  1621
            if (a[i] != a2[i])
jaroslav@557
  1622
                return false;
jaroslav@557
  1623
jaroslav@557
  1624
        return true;
jaroslav@557
  1625
    }
jaroslav@557
  1626
jaroslav@557
  1627
    /**
jaroslav@557
  1628
     * Returns <tt>true</tt> if the two specified arrays of ints are
jaroslav@557
  1629
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1630
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1631
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1632
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1633
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1634
     *
jaroslav@557
  1635
     * @param a one array to be tested for equality
jaroslav@557
  1636
     * @param a2 the other array to be tested for equality
jaroslav@557
  1637
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1638
     */
jaroslav@557
  1639
    public static boolean equals(int[] a, int[] a2) {
jaroslav@557
  1640
        if (a==a2)
jaroslav@557
  1641
            return true;
jaroslav@557
  1642
        if (a==null || a2==null)
jaroslav@557
  1643
            return false;
jaroslav@557
  1644
jaroslav@557
  1645
        int length = a.length;
jaroslav@557
  1646
        if (a2.length != length)
jaroslav@557
  1647
            return false;
jaroslav@557
  1648
jaroslav@557
  1649
        for (int i=0; i<length; i++)
jaroslav@557
  1650
            if (a[i] != a2[i])
jaroslav@557
  1651
                return false;
jaroslav@557
  1652
jaroslav@557
  1653
        return true;
jaroslav@557
  1654
    }
jaroslav@557
  1655
jaroslav@557
  1656
    /**
jaroslav@557
  1657
     * Returns <tt>true</tt> if the two specified arrays of shorts are
jaroslav@557
  1658
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1659
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1660
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1661
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1662
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1663
     *
jaroslav@557
  1664
     * @param a one array to be tested for equality
jaroslav@557
  1665
     * @param a2 the other array to be tested for equality
jaroslav@557
  1666
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1667
     */
jaroslav@557
  1668
    public static boolean equals(short[] a, short a2[]) {
jaroslav@557
  1669
        if (a==a2)
jaroslav@557
  1670
            return true;
jaroslav@557
  1671
        if (a==null || a2==null)
jaroslav@557
  1672
            return false;
jaroslav@557
  1673
jaroslav@557
  1674
        int length = a.length;
jaroslav@557
  1675
        if (a2.length != length)
jaroslav@557
  1676
            return false;
jaroslav@557
  1677
jaroslav@557
  1678
        for (int i=0; i<length; i++)
jaroslav@557
  1679
            if (a[i] != a2[i])
jaroslav@557
  1680
                return false;
jaroslav@557
  1681
jaroslav@557
  1682
        return true;
jaroslav@557
  1683
    }
jaroslav@557
  1684
jaroslav@557
  1685
    /**
jaroslav@557
  1686
     * Returns <tt>true</tt> if the two specified arrays of chars are
jaroslav@557
  1687
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1688
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1689
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1690
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1691
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1692
     *
jaroslav@557
  1693
     * @param a one array to be tested for equality
jaroslav@557
  1694
     * @param a2 the other array to be tested for equality
jaroslav@557
  1695
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1696
     */
jaroslav@557
  1697
    public static boolean equals(char[] a, char[] a2) {
jaroslav@557
  1698
        if (a==a2)
jaroslav@557
  1699
            return true;
jaroslav@557
  1700
        if (a==null || a2==null)
jaroslav@557
  1701
            return false;
jaroslav@557
  1702
jaroslav@557
  1703
        int length = a.length;
jaroslav@557
  1704
        if (a2.length != length)
jaroslav@557
  1705
            return false;
jaroslav@557
  1706
jaroslav@557
  1707
        for (int i=0; i<length; i++)
jaroslav@557
  1708
            if (a[i] != a2[i])
jaroslav@557
  1709
                return false;
jaroslav@557
  1710
jaroslav@557
  1711
        return true;
jaroslav@557
  1712
    }
jaroslav@557
  1713
jaroslav@557
  1714
    /**
jaroslav@557
  1715
     * Returns <tt>true</tt> if the two specified arrays of bytes are
jaroslav@557
  1716
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1717
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1718
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1719
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1720
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1721
     *
jaroslav@557
  1722
     * @param a one array to be tested for equality
jaroslav@557
  1723
     * @param a2 the other array to be tested for equality
jaroslav@557
  1724
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1725
     */
jaroslav@557
  1726
    public static boolean equals(byte[] a, byte[] a2) {
jaroslav@557
  1727
        if (a==a2)
jaroslav@557
  1728
            return true;
jaroslav@557
  1729
        if (a==null || a2==null)
jaroslav@557
  1730
            return false;
jaroslav@557
  1731
jaroslav@557
  1732
        int length = a.length;
jaroslav@557
  1733
        if (a2.length != length)
jaroslav@557
  1734
            return false;
jaroslav@557
  1735
jaroslav@557
  1736
        for (int i=0; i<length; i++)
jaroslav@557
  1737
            if (a[i] != a2[i])
jaroslav@557
  1738
                return false;
jaroslav@557
  1739
jaroslav@557
  1740
        return true;
jaroslav@557
  1741
    }
jaroslav@557
  1742
jaroslav@557
  1743
    /**
jaroslav@557
  1744
     * Returns <tt>true</tt> if the two specified arrays of booleans are
jaroslav@557
  1745
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1746
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1747
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1748
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1749
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1750
     *
jaroslav@557
  1751
     * @param a one array to be tested for equality
jaroslav@557
  1752
     * @param a2 the other array to be tested for equality
jaroslav@557
  1753
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1754
     */
jaroslav@557
  1755
    public static boolean equals(boolean[] a, boolean[] a2) {
jaroslav@557
  1756
        if (a==a2)
jaroslav@557
  1757
            return true;
jaroslav@557
  1758
        if (a==null || a2==null)
jaroslav@557
  1759
            return false;
jaroslav@557
  1760
jaroslav@557
  1761
        int length = a.length;
jaroslav@557
  1762
        if (a2.length != length)
jaroslav@557
  1763
            return false;
jaroslav@557
  1764
jaroslav@557
  1765
        for (int i=0; i<length; i++)
jaroslav@557
  1766
            if (a[i] != a2[i])
jaroslav@557
  1767
                return false;
jaroslav@557
  1768
jaroslav@557
  1769
        return true;
jaroslav@557
  1770
    }
jaroslav@557
  1771
jaroslav@557
  1772
    /**
jaroslav@557
  1773
     * Returns <tt>true</tt> if the two specified arrays of doubles are
jaroslav@557
  1774
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1775
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1776
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1777
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1778
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1779
     *
jaroslav@557
  1780
     * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
jaroslav@557
  1781
     * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
jaroslav@557
  1782
     * (Unlike the <tt>==</tt> operator, this method considers
jaroslav@557
  1783
     * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
jaroslav@557
  1784
     *
jaroslav@557
  1785
     * @param a one array to be tested for equality
jaroslav@557
  1786
     * @param a2 the other array to be tested for equality
jaroslav@557
  1787
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1788
     * @see Double#equals(Object)
jaroslav@557
  1789
     */
jaroslav@557
  1790
    public static boolean equals(double[] a, double[] a2) {
jaroslav@557
  1791
        if (a==a2)
jaroslav@557
  1792
            return true;
jaroslav@557
  1793
        if (a==null || a2==null)
jaroslav@557
  1794
            return false;
jaroslav@557
  1795
jaroslav@557
  1796
        int length = a.length;
jaroslav@557
  1797
        if (a2.length != length)
jaroslav@557
  1798
            return false;
jaroslav@557
  1799
jaroslav@557
  1800
        for (int i=0; i<length; i++)
jaroslav@557
  1801
            if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
jaroslav@557
  1802
                return false;
jaroslav@557
  1803
jaroslav@557
  1804
        return true;
jaroslav@557
  1805
    }
jaroslav@557
  1806
jaroslav@557
  1807
    /**
jaroslav@557
  1808
     * Returns <tt>true</tt> if the two specified arrays of floats are
jaroslav@557
  1809
     * <i>equal</i> to one another.  Two arrays are considered equal if both
jaroslav@557
  1810
     * arrays contain the same number of elements, and all corresponding pairs
jaroslav@557
  1811
     * of elements in the two arrays are equal.  In other words, two arrays
jaroslav@557
  1812
     * are equal if they contain the same elements in the same order.  Also,
jaroslav@557
  1813
     * two array references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1814
     *
jaroslav@557
  1815
     * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
jaroslav@557
  1816
     * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
jaroslav@557
  1817
     * (Unlike the <tt>==</tt> operator, this method considers
jaroslav@557
  1818
     * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
jaroslav@557
  1819
     *
jaroslav@557
  1820
     * @param a one array to be tested for equality
jaroslav@557
  1821
     * @param a2 the other array to be tested for equality
jaroslav@557
  1822
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1823
     * @see Float#equals(Object)
jaroslav@557
  1824
     */
jaroslav@557
  1825
    public static boolean equals(float[] a, float[] a2) {
jaroslav@557
  1826
        if (a==a2)
jaroslav@557
  1827
            return true;
jaroslav@557
  1828
        if (a==null || a2==null)
jaroslav@557
  1829
            return false;
jaroslav@557
  1830
jaroslav@557
  1831
        int length = a.length;
jaroslav@557
  1832
        if (a2.length != length)
jaroslav@557
  1833
            return false;
jaroslav@557
  1834
jaroslav@557
  1835
        for (int i=0; i<length; i++)
jaroslav@557
  1836
            if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
jaroslav@557
  1837
                return false;
jaroslav@557
  1838
jaroslav@557
  1839
        return true;
jaroslav@557
  1840
    }
jaroslav@557
  1841
jaroslav@557
  1842
    /**
jaroslav@557
  1843
     * Returns <tt>true</tt> if the two specified arrays of Objects are
jaroslav@557
  1844
     * <i>equal</i> to one another.  The two arrays are considered equal if
jaroslav@557
  1845
     * both arrays contain the same number of elements, and all corresponding
jaroslav@557
  1846
     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
jaroslav@557
  1847
     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
jaroslav@557
  1848
     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
jaroslav@557
  1849
     * they contain the same elements in the same order.  Also, two array
jaroslav@557
  1850
     * references are considered equal if both are <tt>null</tt>.<p>
jaroslav@557
  1851
     *
jaroslav@557
  1852
     * @param a one array to be tested for equality
jaroslav@557
  1853
     * @param a2 the other array to be tested for equality
jaroslav@557
  1854
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  1855
     */
jaroslav@557
  1856
    public static boolean equals(Object[] a, Object[] a2) {
jaroslav@557
  1857
        if (a==a2)
jaroslav@557
  1858
            return true;
jaroslav@557
  1859
        if (a==null || a2==null)
jaroslav@557
  1860
            return false;
jaroslav@557
  1861
jaroslav@557
  1862
        int length = a.length;
jaroslav@557
  1863
        if (a2.length != length)
jaroslav@557
  1864
            return false;
jaroslav@557
  1865
jaroslav@557
  1866
        for (int i=0; i<length; i++) {
jaroslav@557
  1867
            Object o1 = a[i];
jaroslav@557
  1868
            Object o2 = a2[i];
jaroslav@557
  1869
            if (!(o1==null ? o2==null : o1.equals(o2)))
jaroslav@557
  1870
                return false;
jaroslav@557
  1871
        }
jaroslav@557
  1872
jaroslav@557
  1873
        return true;
jaroslav@557
  1874
    }
jaroslav@557
  1875
jaroslav@557
  1876
    // Filling
jaroslav@557
  1877
jaroslav@557
  1878
    /**
jaroslav@557
  1879
     * Assigns the specified long value to each element of the specified array
jaroslav@557
  1880
     * of longs.
jaroslav@557
  1881
     *
jaroslav@557
  1882
     * @param a the array to be filled
jaroslav@557
  1883
     * @param val the value to be stored in all elements of the array
jaroslav@557
  1884
     */
jaroslav@557
  1885
    public static void fill(long[] a, long val) {
jaroslav@557
  1886
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  1887
            a[i] = val;
jaroslav@557
  1888
    }
jaroslav@557
  1889
jaroslav@557
  1890
    /**
jaroslav@557
  1891
     * Assigns the specified long value to each element of the specified
jaroslav@557
  1892
     * range of the specified array of longs.  The range to be filled
jaroslav@557
  1893
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  1894
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  1895
     * range to be filled is empty.)
jaroslav@557
  1896
     *
jaroslav@557
  1897
     * @param a the array to be filled
jaroslav@557
  1898
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1899
     *        filled with the specified value
jaroslav@557
  1900
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  1901
     *        filled with the specified value
jaroslav@557
  1902
     * @param val the value to be stored in all elements of the array
jaroslav@557
  1903
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  1904
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  1905
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  1906
     */
jaroslav@557
  1907
    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
jaroslav@557
  1908
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1909
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  1910
            a[i] = val;
jaroslav@557
  1911
    }
jaroslav@557
  1912
jaroslav@557
  1913
    /**
jaroslav@557
  1914
     * Assigns the specified int value to each element of the specified array
jaroslav@557
  1915
     * of ints.
jaroslav@557
  1916
     *
jaroslav@557
  1917
     * @param a the array to be filled
jaroslav@557
  1918
     * @param val the value to be stored in all elements of the array
jaroslav@557
  1919
     */
jaroslav@557
  1920
    public static void fill(int[] a, int val) {
jaroslav@557
  1921
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  1922
            a[i] = val;
jaroslav@557
  1923
    }
jaroslav@557
  1924
jaroslav@557
  1925
    /**
jaroslav@557
  1926
     * Assigns the specified int value to each element of the specified
jaroslav@557
  1927
     * range of the specified array of ints.  The range to be filled
jaroslav@557
  1928
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  1929
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  1930
     * range to be filled is empty.)
jaroslav@557
  1931
     *
jaroslav@557
  1932
     * @param a the array to be filled
jaroslav@557
  1933
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1934
     *        filled with the specified value
jaroslav@557
  1935
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  1936
     *        filled with the specified value
jaroslav@557
  1937
     * @param val the value to be stored in all elements of the array
jaroslav@557
  1938
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  1939
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  1940
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  1941
     */
jaroslav@557
  1942
    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
jaroslav@557
  1943
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1944
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  1945
            a[i] = val;
jaroslav@557
  1946
    }
jaroslav@557
  1947
jaroslav@557
  1948
    /**
jaroslav@557
  1949
     * Assigns the specified short value to each element of the specified array
jaroslav@557
  1950
     * of shorts.
jaroslav@557
  1951
     *
jaroslav@557
  1952
     * @param a the array to be filled
jaroslav@557
  1953
     * @param val the value to be stored in all elements of the array
jaroslav@557
  1954
     */
jaroslav@557
  1955
    public static void fill(short[] a, short val) {
jaroslav@557
  1956
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  1957
            a[i] = val;
jaroslav@557
  1958
    }
jaroslav@557
  1959
jaroslav@557
  1960
    /**
jaroslav@557
  1961
     * Assigns the specified short value to each element of the specified
jaroslav@557
  1962
     * range of the specified array of shorts.  The range to be filled
jaroslav@557
  1963
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  1964
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  1965
     * range to be filled is empty.)
jaroslav@557
  1966
     *
jaroslav@557
  1967
     * @param a the array to be filled
jaroslav@557
  1968
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  1969
     *        filled with the specified value
jaroslav@557
  1970
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  1971
     *        filled with the specified value
jaroslav@557
  1972
     * @param val the value to be stored in all elements of the array
jaroslav@557
  1973
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  1974
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  1975
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  1976
     */
jaroslav@557
  1977
    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
jaroslav@557
  1978
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  1979
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  1980
            a[i] = val;
jaroslav@557
  1981
    }
jaroslav@557
  1982
jaroslav@557
  1983
    /**
jaroslav@557
  1984
     * Assigns the specified char value to each element of the specified array
jaroslav@557
  1985
     * of chars.
jaroslav@557
  1986
     *
jaroslav@557
  1987
     * @param a the array to be filled
jaroslav@557
  1988
     * @param val the value to be stored in all elements of the array
jaroslav@557
  1989
     */
jaroslav@557
  1990
    public static void fill(char[] a, char val) {
jaroslav@557
  1991
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  1992
            a[i] = val;
jaroslav@557
  1993
    }
jaroslav@557
  1994
jaroslav@557
  1995
    /**
jaroslav@557
  1996
     * Assigns the specified char value to each element of the specified
jaroslav@557
  1997
     * range of the specified array of chars.  The range to be filled
jaroslav@557
  1998
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  1999
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  2000
     * range to be filled is empty.)
jaroslav@557
  2001
     *
jaroslav@557
  2002
     * @param a the array to be filled
jaroslav@557
  2003
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  2004
     *        filled with the specified value
jaroslav@557
  2005
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  2006
     *        filled with the specified value
jaroslav@557
  2007
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2008
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  2009
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  2010
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  2011
     */
jaroslav@557
  2012
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
jaroslav@557
  2013
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  2014
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  2015
            a[i] = val;
jaroslav@557
  2016
    }
jaroslav@557
  2017
jaroslav@557
  2018
    /**
jaroslav@557
  2019
     * Assigns the specified byte value to each element of the specified array
jaroslav@557
  2020
     * of bytes.
jaroslav@557
  2021
     *
jaroslav@557
  2022
     * @param a the array to be filled
jaroslav@557
  2023
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2024
     */
jaroslav@557
  2025
    public static void fill(byte[] a, byte val) {
jaroslav@557
  2026
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  2027
            a[i] = val;
jaroslav@557
  2028
    }
jaroslav@557
  2029
jaroslav@557
  2030
    /**
jaroslav@557
  2031
     * Assigns the specified byte value to each element of the specified
jaroslav@557
  2032
     * range of the specified array of bytes.  The range to be filled
jaroslav@557
  2033
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  2034
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  2035
     * range to be filled is empty.)
jaroslav@557
  2036
     *
jaroslav@557
  2037
     * @param a the array to be filled
jaroslav@557
  2038
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  2039
     *        filled with the specified value
jaroslav@557
  2040
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  2041
     *        filled with the specified value
jaroslav@557
  2042
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2043
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  2044
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  2045
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  2046
     */
jaroslav@557
  2047
    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
jaroslav@557
  2048
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  2049
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  2050
            a[i] = val;
jaroslav@557
  2051
    }
jaroslav@557
  2052
jaroslav@557
  2053
    /**
jaroslav@557
  2054
     * Assigns the specified boolean value to each element of the specified
jaroslav@557
  2055
     * array of booleans.
jaroslav@557
  2056
     *
jaroslav@557
  2057
     * @param a the array to be filled
jaroslav@557
  2058
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2059
     */
jaroslav@557
  2060
    public static void fill(boolean[] a, boolean val) {
jaroslav@557
  2061
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  2062
            a[i] = val;
jaroslav@557
  2063
    }
jaroslav@557
  2064
jaroslav@557
  2065
    /**
jaroslav@557
  2066
     * Assigns the specified boolean value to each element of the specified
jaroslav@557
  2067
     * range of the specified array of booleans.  The range to be filled
jaroslav@557
  2068
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  2069
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  2070
     * range to be filled is empty.)
jaroslav@557
  2071
     *
jaroslav@557
  2072
     * @param a the array to be filled
jaroslav@557
  2073
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  2074
     *        filled with the specified value
jaroslav@557
  2075
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  2076
     *        filled with the specified value
jaroslav@557
  2077
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2078
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  2079
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  2080
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  2081
     */
jaroslav@557
  2082
    public static void fill(boolean[] a, int fromIndex, int toIndex,
jaroslav@557
  2083
                            boolean val) {
jaroslav@557
  2084
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  2085
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  2086
            a[i] = val;
jaroslav@557
  2087
    }
jaroslav@557
  2088
jaroslav@557
  2089
    /**
jaroslav@557
  2090
     * Assigns the specified double value to each element of the specified
jaroslav@557
  2091
     * array of doubles.
jaroslav@557
  2092
     *
jaroslav@557
  2093
     * @param a the array to be filled
jaroslav@557
  2094
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2095
     */
jaroslav@557
  2096
    public static void fill(double[] a, double val) {
jaroslav@557
  2097
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  2098
            a[i] = val;
jaroslav@557
  2099
    }
jaroslav@557
  2100
jaroslav@557
  2101
    /**
jaroslav@557
  2102
     * Assigns the specified double value to each element of the specified
jaroslav@557
  2103
     * range of the specified array of doubles.  The range to be filled
jaroslav@557
  2104
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  2105
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  2106
     * range to be filled is empty.)
jaroslav@557
  2107
     *
jaroslav@557
  2108
     * @param a the array to be filled
jaroslav@557
  2109
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  2110
     *        filled with the specified value
jaroslav@557
  2111
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  2112
     *        filled with the specified value
jaroslav@557
  2113
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2114
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  2115
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  2116
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  2117
     */
jaroslav@557
  2118
    public static void fill(double[] a, int fromIndex, int toIndex,double val){
jaroslav@557
  2119
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  2120
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  2121
            a[i] = val;
jaroslav@557
  2122
    }
jaroslav@557
  2123
jaroslav@557
  2124
    /**
jaroslav@557
  2125
     * Assigns the specified float value to each element of the specified array
jaroslav@557
  2126
     * of floats.
jaroslav@557
  2127
     *
jaroslav@557
  2128
     * @param a the array to be filled
jaroslav@557
  2129
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2130
     */
jaroslav@557
  2131
    public static void fill(float[] a, float val) {
jaroslav@557
  2132
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  2133
            a[i] = val;
jaroslav@557
  2134
    }
jaroslav@557
  2135
jaroslav@557
  2136
    /**
jaroslav@557
  2137
     * Assigns the specified float value to each element of the specified
jaroslav@557
  2138
     * range of the specified array of floats.  The range to be filled
jaroslav@557
  2139
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  2140
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  2141
     * range to be filled is empty.)
jaroslav@557
  2142
     *
jaroslav@557
  2143
     * @param a the array to be filled
jaroslav@557
  2144
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  2145
     *        filled with the specified value
jaroslav@557
  2146
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  2147
     *        filled with the specified value
jaroslav@557
  2148
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2149
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  2150
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  2151
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  2152
     */
jaroslav@557
  2153
    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
jaroslav@557
  2154
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  2155
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  2156
            a[i] = val;
jaroslav@557
  2157
    }
jaroslav@557
  2158
jaroslav@557
  2159
    /**
jaroslav@557
  2160
     * Assigns the specified Object reference to each element of the specified
jaroslav@557
  2161
     * array of Objects.
jaroslav@557
  2162
     *
jaroslav@557
  2163
     * @param a the array to be filled
jaroslav@557
  2164
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2165
     * @throws ArrayStoreException if the specified value is not of a
jaroslav@557
  2166
     *         runtime type that can be stored in the specified array
jaroslav@557
  2167
     */
jaroslav@557
  2168
    public static void fill(Object[] a, Object val) {
jaroslav@557
  2169
        for (int i = 0, len = a.length; i < len; i++)
jaroslav@557
  2170
            a[i] = val;
jaroslav@557
  2171
    }
jaroslav@557
  2172
jaroslav@557
  2173
    /**
jaroslav@557
  2174
     * Assigns the specified Object reference to each element of the specified
jaroslav@557
  2175
     * range of the specified array of Objects.  The range to be filled
jaroslav@557
  2176
     * extends from index <tt>fromIndex</tt>, inclusive, to index
jaroslav@557
  2177
     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
jaroslav@557
  2178
     * range to be filled is empty.)
jaroslav@557
  2179
     *
jaroslav@557
  2180
     * @param a the array to be filled
jaroslav@557
  2181
     * @param fromIndex the index of the first element (inclusive) to be
jaroslav@557
  2182
     *        filled with the specified value
jaroslav@557
  2183
     * @param toIndex the index of the last element (exclusive) to be
jaroslav@557
  2184
     *        filled with the specified value
jaroslav@557
  2185
     * @param val the value to be stored in all elements of the array
jaroslav@557
  2186
     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
jaroslav@557
  2187
     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
jaroslav@557
  2188
     *         <tt>toIndex &gt; a.length</tt>
jaroslav@557
  2189
     * @throws ArrayStoreException if the specified value is not of a
jaroslav@557
  2190
     *         runtime type that can be stored in the specified array
jaroslav@557
  2191
     */
jaroslav@557
  2192
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
jaroslav@557
  2193
        rangeCheck(a.length, fromIndex, toIndex);
jaroslav@557
  2194
        for (int i = fromIndex; i < toIndex; i++)
jaroslav@557
  2195
            a[i] = val;
jaroslav@557
  2196
    }
jaroslav@557
  2197
jaroslav@557
  2198
    // Cloning
jaroslav@557
  2199
jaroslav@557
  2200
    /**
jaroslav@557
  2201
     * Copies the specified array, truncating or padding with nulls (if necessary)
jaroslav@557
  2202
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2203
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2204
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2205
     * copy but not the original, the copy will contain <tt>null</tt>.
jaroslav@557
  2206
     * Such indices will exist if and only if the specified length
jaroslav@557
  2207
     * is greater than that of the original array.
jaroslav@557
  2208
     * The resulting array is of exactly the same class as the original array.
jaroslav@557
  2209
     *
jaroslav@557
  2210
     * @param original the array to be copied
jaroslav@557
  2211
     * @param newLength the length of the copy to be returned
jaroslav@557
  2212
     * @return a copy of the original array, truncated or padded with nulls
jaroslav@557
  2213
     *     to obtain the specified length
jaroslav@557
  2214
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2215
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2216
     * @since 1.6
jaroslav@557
  2217
     */
jaroslav@557
  2218
    public static <T> T[] copyOf(T[] original, int newLength) {
jaroslav@557
  2219
        return (T[]) copyOf(original, newLength, original.getClass());
jaroslav@557
  2220
    }
jaroslav@557
  2221
jaroslav@557
  2222
    /**
jaroslav@557
  2223
     * Copies the specified array, truncating or padding with nulls (if necessary)
jaroslav@557
  2224
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2225
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2226
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2227
     * copy but not the original, the copy will contain <tt>null</tt>.
jaroslav@557
  2228
     * Such indices will exist if and only if the specified length
jaroslav@557
  2229
     * is greater than that of the original array.
jaroslav@557
  2230
     * The resulting array is of the class <tt>newType</tt>.
jaroslav@557
  2231
     *
jaroslav@557
  2232
     * @param original the array to be copied
jaroslav@557
  2233
     * @param newLength the length of the copy to be returned
jaroslav@557
  2234
     * @param newType the class of the copy to be returned
jaroslav@557
  2235
     * @return a copy of the original array, truncated or padded with nulls
jaroslav@557
  2236
     *     to obtain the specified length
jaroslav@557
  2237
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2238
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2239
     * @throws ArrayStoreException if an element copied from
jaroslav@557
  2240
     *     <tt>original</tt> is not of a runtime type that can be stored in
jaroslav@557
  2241
     *     an array of class <tt>newType</tt>
jaroslav@557
  2242
     * @since 1.6
jaroslav@557
  2243
     */
jaroslav@557
  2244
    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
jaroslav@557
  2245
        T[] copy = ((Object)newType == (Object)Object[].class)
jaroslav@557
  2246
            ? (T[]) new Object[newLength]
jaroslav@557
  2247
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
jaroslav@557
  2248
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2249
                         Math.min(original.length, newLength));
jaroslav@557
  2250
        return copy;
jaroslav@557
  2251
    }
jaroslav@557
  2252
jaroslav@557
  2253
    /**
jaroslav@557
  2254
     * Copies the specified array, truncating or padding with zeros (if necessary)
jaroslav@557
  2255
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2256
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2257
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2258
     * copy but not the original, the copy will contain <tt>(byte)0</tt>.
jaroslav@557
  2259
     * Such indices will exist if and only if the specified length
jaroslav@557
  2260
     * is greater than that of the original array.
jaroslav@557
  2261
     *
jaroslav@557
  2262
     * @param original the array to be copied
jaroslav@557
  2263
     * @param newLength the length of the copy to be returned
jaroslav@557
  2264
     * @return a copy of the original array, truncated or padded with zeros
jaroslav@557
  2265
     *     to obtain the specified length
jaroslav@557
  2266
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2267
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2268
     * @since 1.6
jaroslav@557
  2269
     */
jaroslav@557
  2270
    public static byte[] copyOf(byte[] original, int newLength) {
jaroslav@557
  2271
        byte[] copy = new byte[newLength];
jaroslav@557
  2272
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2273
                         Math.min(original.length, newLength));
jaroslav@557
  2274
        return copy;
jaroslav@557
  2275
    }
jaroslav@557
  2276
jaroslav@557
  2277
    /**
jaroslav@557
  2278
     * Copies the specified array, truncating or padding with zeros (if necessary)
jaroslav@557
  2279
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2280
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2281
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2282
     * copy but not the original, the copy will contain <tt>(short)0</tt>.
jaroslav@557
  2283
     * Such indices will exist if and only if the specified length
jaroslav@557
  2284
     * is greater than that of the original array.
jaroslav@557
  2285
     *
jaroslav@557
  2286
     * @param original the array to be copied
jaroslav@557
  2287
     * @param newLength the length of the copy to be returned
jaroslav@557
  2288
     * @return a copy of the original array, truncated or padded with zeros
jaroslav@557
  2289
     *     to obtain the specified length
jaroslav@557
  2290
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2291
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2292
     * @since 1.6
jaroslav@557
  2293
     */
jaroslav@557
  2294
    public static short[] copyOf(short[] original, int newLength) {
jaroslav@557
  2295
        short[] copy = new short[newLength];
jaroslav@557
  2296
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2297
                         Math.min(original.length, newLength));
jaroslav@557
  2298
        return copy;
jaroslav@557
  2299
    }
jaroslav@557
  2300
jaroslav@557
  2301
    /**
jaroslav@557
  2302
     * Copies the specified array, truncating or padding with zeros (if necessary)
jaroslav@557
  2303
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2304
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2305
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2306
     * copy but not the original, the copy will contain <tt>0</tt>.
jaroslav@557
  2307
     * Such indices will exist if and only if the specified length
jaroslav@557
  2308
     * is greater than that of the original array.
jaroslav@557
  2309
     *
jaroslav@557
  2310
     * @param original the array to be copied
jaroslav@557
  2311
     * @param newLength the length of the copy to be returned
jaroslav@557
  2312
     * @return a copy of the original array, truncated or padded with zeros
jaroslav@557
  2313
     *     to obtain the specified length
jaroslav@557
  2314
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2315
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2316
     * @since 1.6
jaroslav@557
  2317
     */
jaroslav@557
  2318
    public static int[] copyOf(int[] original, int newLength) {
jaroslav@557
  2319
        int[] copy = new int[newLength];
jaroslav@557
  2320
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2321
                         Math.min(original.length, newLength));
jaroslav@557
  2322
        return copy;
jaroslav@557
  2323
    }
jaroslav@557
  2324
jaroslav@557
  2325
    /**
jaroslav@557
  2326
     * Copies the specified array, truncating or padding with zeros (if necessary)
jaroslav@557
  2327
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2328
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2329
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2330
     * copy but not the original, the copy will contain <tt>0L</tt>.
jaroslav@557
  2331
     * Such indices will exist if and only if the specified length
jaroslav@557
  2332
     * is greater than that of the original array.
jaroslav@557
  2333
     *
jaroslav@557
  2334
     * @param original the array to be copied
jaroslav@557
  2335
     * @param newLength the length of the copy to be returned
jaroslav@557
  2336
     * @return a copy of the original array, truncated or padded with zeros
jaroslav@557
  2337
     *     to obtain the specified length
jaroslav@557
  2338
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2339
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2340
     * @since 1.6
jaroslav@557
  2341
     */
jaroslav@557
  2342
    public static long[] copyOf(long[] original, int newLength) {
jaroslav@557
  2343
        long[] copy = new long[newLength];
jaroslav@557
  2344
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2345
                         Math.min(original.length, newLength));
jaroslav@557
  2346
        return copy;
jaroslav@557
  2347
    }
jaroslav@557
  2348
jaroslav@557
  2349
    /**
jaroslav@557
  2350
     * Copies the specified array, truncating or padding with null characters (if necessary)
jaroslav@557
  2351
     * so the copy has the specified length.  For all indices that are valid
jaroslav@557
  2352
     * in both the original array and the copy, the two arrays will contain
jaroslav@557
  2353
     * identical values.  For any indices that are valid in the copy but not
jaroslav@557
  2354
     * the original, the copy will contain <tt>'\\u000'</tt>.  Such indices
jaroslav@557
  2355
     * will exist if and only if the specified length is greater than that of
jaroslav@557
  2356
     * the original array.
jaroslav@557
  2357
     *
jaroslav@557
  2358
     * @param original the array to be copied
jaroslav@557
  2359
     * @param newLength the length of the copy to be returned
jaroslav@557
  2360
     * @return a copy of the original array, truncated or padded with null characters
jaroslav@557
  2361
     *     to obtain the specified length
jaroslav@557
  2362
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2363
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2364
     * @since 1.6
jaroslav@557
  2365
     */
jaroslav@557
  2366
    public static char[] copyOf(char[] original, int newLength) {
jaroslav@557
  2367
        char[] copy = new char[newLength];
jaroslav@557
  2368
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2369
                         Math.min(original.length, newLength));
jaroslav@557
  2370
        return copy;
jaroslav@557
  2371
    }
jaroslav@557
  2372
jaroslav@557
  2373
    /**
jaroslav@557
  2374
     * Copies the specified array, truncating or padding with zeros (if necessary)
jaroslav@557
  2375
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2376
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2377
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2378
     * copy but not the original, the copy will contain <tt>0f</tt>.
jaroslav@557
  2379
     * Such indices will exist if and only if the specified length
jaroslav@557
  2380
     * is greater than that of the original array.
jaroslav@557
  2381
     *
jaroslav@557
  2382
     * @param original the array to be copied
jaroslav@557
  2383
     * @param newLength the length of the copy to be returned
jaroslav@557
  2384
     * @return a copy of the original array, truncated or padded with zeros
jaroslav@557
  2385
     *     to obtain the specified length
jaroslav@557
  2386
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2387
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2388
     * @since 1.6
jaroslav@557
  2389
     */
jaroslav@557
  2390
    public static float[] copyOf(float[] original, int newLength) {
jaroslav@557
  2391
        float[] copy = new float[newLength];
jaroslav@557
  2392
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2393
                         Math.min(original.length, newLength));
jaroslav@557
  2394
        return copy;
jaroslav@557
  2395
    }
jaroslav@557
  2396
jaroslav@557
  2397
    /**
jaroslav@557
  2398
     * Copies the specified array, truncating or padding with zeros (if necessary)
jaroslav@557
  2399
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2400
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2401
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2402
     * copy but not the original, the copy will contain <tt>0d</tt>.
jaroslav@557
  2403
     * Such indices will exist if and only if the specified length
jaroslav@557
  2404
     * is greater than that of the original array.
jaroslav@557
  2405
     *
jaroslav@557
  2406
     * @param original the array to be copied
jaroslav@557
  2407
     * @param newLength the length of the copy to be returned
jaroslav@557
  2408
     * @return a copy of the original array, truncated or padded with zeros
jaroslav@557
  2409
     *     to obtain the specified length
jaroslav@557
  2410
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2411
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2412
     * @since 1.6
jaroslav@557
  2413
     */
jaroslav@557
  2414
    public static double[] copyOf(double[] original, int newLength) {
jaroslav@557
  2415
        double[] copy = new double[newLength];
jaroslav@557
  2416
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2417
                         Math.min(original.length, newLength));
jaroslav@557
  2418
        return copy;
jaroslav@557
  2419
    }
jaroslav@557
  2420
jaroslav@557
  2421
    /**
jaroslav@557
  2422
     * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
jaroslav@557
  2423
     * so the copy has the specified length.  For all indices that are
jaroslav@557
  2424
     * valid in both the original array and the copy, the two arrays will
jaroslav@557
  2425
     * contain identical values.  For any indices that are valid in the
jaroslav@557
  2426
     * copy but not the original, the copy will contain <tt>false</tt>.
jaroslav@557
  2427
     * Such indices will exist if and only if the specified length
jaroslav@557
  2428
     * is greater than that of the original array.
jaroslav@557
  2429
     *
jaroslav@557
  2430
     * @param original the array to be copied
jaroslav@557
  2431
     * @param newLength the length of the copy to be returned
jaroslav@557
  2432
     * @return a copy of the original array, truncated or padded with false elements
jaroslav@557
  2433
     *     to obtain the specified length
jaroslav@557
  2434
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
jaroslav@557
  2435
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2436
     * @since 1.6
jaroslav@557
  2437
     */
jaroslav@557
  2438
    public static boolean[] copyOf(boolean[] original, int newLength) {
jaroslav@557
  2439
        boolean[] copy = new boolean[newLength];
jaroslav@557
  2440
        System.arraycopy(original, 0, copy, 0,
jaroslav@557
  2441
                         Math.min(original.length, newLength));
jaroslav@557
  2442
        return copy;
jaroslav@557
  2443
    }
jaroslav@557
  2444
jaroslav@557
  2445
    /**
jaroslav@557
  2446
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2447
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2448
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2449
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2450
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2451
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2452
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2453
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2454
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2455
     * <tt>null</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2456
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2457
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2458
     * <p>
jaroslav@557
  2459
     * The resulting array is of exactly the same class as the original array.
jaroslav@557
  2460
     *
jaroslav@557
  2461
     * @param original the array from which a range is to be copied
jaroslav@557
  2462
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2463
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2464
     *     (This index may lie outside the array.)
jaroslav@557
  2465
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2466
     *     truncated or padded with nulls to obtain the required length
jaroslav@557
  2467
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2468
     *     or {@code from > original.length}
jaroslav@557
  2469
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2470
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2471
     * @since 1.6
jaroslav@557
  2472
     */
jaroslav@557
  2473
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
jaroslav@557
  2474
        return copyOfRange(original, from, to, (Class<T[]>) original.getClass());
jaroslav@557
  2475
    }
jaroslav@557
  2476
jaroslav@557
  2477
    /**
jaroslav@557
  2478
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2479
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2480
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2481
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2482
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2483
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2484
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2485
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2486
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2487
     * <tt>null</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2488
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2489
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2490
     * The resulting array is of the class <tt>newType</tt>.
jaroslav@557
  2491
     *
jaroslav@557
  2492
     * @param original the array from which a range is to be copied
jaroslav@557
  2493
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2494
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2495
     *     (This index may lie outside the array.)
jaroslav@557
  2496
     * @param newType the class of the copy to be returned
jaroslav@557
  2497
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2498
     *     truncated or padded with nulls to obtain the required length
jaroslav@557
  2499
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2500
     *     or {@code from > original.length}
jaroslav@557
  2501
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2502
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2503
     * @throws ArrayStoreException if an element copied from
jaroslav@557
  2504
     *     <tt>original</tt> is not of a runtime type that can be stored in
jaroslav@557
  2505
     *     an array of class <tt>newType</tt>.
jaroslav@557
  2506
     * @since 1.6
jaroslav@557
  2507
     */
jaroslav@557
  2508
    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
jaroslav@557
  2509
        int newLength = to - from;
jaroslav@557
  2510
        if (newLength < 0)
jaroslav@557
  2511
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2512
        T[] copy = ((Object)newType == (Object)Object[].class)
jaroslav@557
  2513
            ? (T[]) new Object[newLength]
jaroslav@557
  2514
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
jaroslav@557
  2515
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2516
                         Math.min(original.length - from, newLength));
jaroslav@557
  2517
        return copy;
jaroslav@557
  2518
    }
jaroslav@557
  2519
jaroslav@557
  2520
    /**
jaroslav@557
  2521
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2522
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2523
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2524
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2525
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2526
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2527
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2528
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2529
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2530
     * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2531
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2532
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2533
     *
jaroslav@557
  2534
     * @param original the array from which a range is to be copied
jaroslav@557
  2535
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2536
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2537
     *     (This index may lie outside the array.)
jaroslav@557
  2538
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2539
     *     truncated or padded with zeros to obtain the required length
jaroslav@557
  2540
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2541
     *     or {@code from > original.length}
jaroslav@557
  2542
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2543
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2544
     * @since 1.6
jaroslav@557
  2545
     */
jaroslav@557
  2546
    public static byte[] copyOfRange(byte[] original, int from, int to) {
jaroslav@557
  2547
        int newLength = to - from;
jaroslav@557
  2548
        if (newLength < 0)
jaroslav@557
  2549
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2550
        byte[] copy = new byte[newLength];
jaroslav@557
  2551
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2552
                         Math.min(original.length - from, newLength));
jaroslav@557
  2553
        return copy;
jaroslav@557
  2554
    }
jaroslav@557
  2555
jaroslav@557
  2556
    /**
jaroslav@557
  2557
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2558
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2559
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2560
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2561
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2562
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2563
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2564
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2565
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2566
     * <tt>(short)0</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2567
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2568
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2569
     *
jaroslav@557
  2570
     * @param original the array from which a range is to be copied
jaroslav@557
  2571
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2572
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2573
     *     (This index may lie outside the array.)
jaroslav@557
  2574
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2575
     *     truncated or padded with zeros to obtain the required length
jaroslav@557
  2576
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2577
     *     or {@code from > original.length}
jaroslav@557
  2578
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2579
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2580
     * @since 1.6
jaroslav@557
  2581
     */
jaroslav@557
  2582
    public static short[] copyOfRange(short[] original, int from, int to) {
jaroslav@557
  2583
        int newLength = to - from;
jaroslav@557
  2584
        if (newLength < 0)
jaroslav@557
  2585
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2586
        short[] copy = new short[newLength];
jaroslav@557
  2587
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2588
                         Math.min(original.length - from, newLength));
jaroslav@557
  2589
        return copy;
jaroslav@557
  2590
    }
jaroslav@557
  2591
jaroslav@557
  2592
    /**
jaroslav@557
  2593
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2594
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2595
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2596
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2597
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2598
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2599
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2600
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2601
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2602
     * <tt>0</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2603
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2604
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2605
     *
jaroslav@557
  2606
     * @param original the array from which a range is to be copied
jaroslav@557
  2607
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2608
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2609
     *     (This index may lie outside the array.)
jaroslav@557
  2610
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2611
     *     truncated or padded with zeros to obtain the required length
jaroslav@557
  2612
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2613
     *     or {@code from > original.length}
jaroslav@557
  2614
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2615
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2616
     * @since 1.6
jaroslav@557
  2617
     */
jaroslav@557
  2618
    public static int[] copyOfRange(int[] original, int from, int to) {
jaroslav@557
  2619
        int newLength = to - from;
jaroslav@557
  2620
        if (newLength < 0)
jaroslav@557
  2621
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2622
        int[] copy = new int[newLength];
jaroslav@557
  2623
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2624
                         Math.min(original.length - from, newLength));
jaroslav@557
  2625
        return copy;
jaroslav@557
  2626
    }
jaroslav@557
  2627
jaroslav@557
  2628
    /**
jaroslav@557
  2629
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2630
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2631
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2632
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2633
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2634
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2635
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2636
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2637
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2638
     * <tt>0L</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2639
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2640
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2641
     *
jaroslav@557
  2642
     * @param original the array from which a range is to be copied
jaroslav@557
  2643
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2644
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2645
     *     (This index may lie outside the array.)
jaroslav@557
  2646
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2647
     *     truncated or padded with zeros to obtain the required length
jaroslav@557
  2648
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2649
     *     or {@code from > original.length}
jaroslav@557
  2650
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2651
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2652
     * @since 1.6
jaroslav@557
  2653
     */
jaroslav@557
  2654
    public static long[] copyOfRange(long[] original, int from, int to) {
jaroslav@557
  2655
        int newLength = to - from;
jaroslav@557
  2656
        if (newLength < 0)
jaroslav@557
  2657
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2658
        long[] copy = new long[newLength];
jaroslav@557
  2659
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2660
                         Math.min(original.length - from, newLength));
jaroslav@557
  2661
        return copy;
jaroslav@557
  2662
    }
jaroslav@557
  2663
jaroslav@557
  2664
    /**
jaroslav@557
  2665
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2666
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2667
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2668
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2669
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2670
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2671
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2672
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2673
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2674
     * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2675
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2676
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2677
     *
jaroslav@557
  2678
     * @param original the array from which a range is to be copied
jaroslav@557
  2679
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2680
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2681
     *     (This index may lie outside the array.)
jaroslav@557
  2682
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2683
     *     truncated or padded with null characters to obtain the required length
jaroslav@557
  2684
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2685
     *     or {@code from > original.length}
jaroslav@557
  2686
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2687
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2688
     * @since 1.6
jaroslav@557
  2689
     */
jaroslav@557
  2690
    public static char[] copyOfRange(char[] original, int from, int to) {
jaroslav@557
  2691
        int newLength = to - from;
jaroslav@557
  2692
        if (newLength < 0)
jaroslav@557
  2693
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2694
        char[] copy = new char[newLength];
jaroslav@557
  2695
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2696
                         Math.min(original.length - from, newLength));
jaroslav@557
  2697
        return copy;
jaroslav@557
  2698
    }
jaroslav@557
  2699
jaroslav@557
  2700
    /**
jaroslav@557
  2701
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2702
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2703
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2704
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2705
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2706
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2707
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2708
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2709
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2710
     * <tt>0f</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2711
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2712
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2713
     *
jaroslav@557
  2714
     * @param original the array from which a range is to be copied
jaroslav@557
  2715
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2716
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2717
     *     (This index may lie outside the array.)
jaroslav@557
  2718
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2719
     *     truncated or padded with zeros to obtain the required length
jaroslav@557
  2720
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2721
     *     or {@code from > original.length}
jaroslav@557
  2722
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2723
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2724
     * @since 1.6
jaroslav@557
  2725
     */
jaroslav@557
  2726
    public static float[] copyOfRange(float[] original, int from, int to) {
jaroslav@557
  2727
        int newLength = to - from;
jaroslav@557
  2728
        if (newLength < 0)
jaroslav@557
  2729
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2730
        float[] copy = new float[newLength];
jaroslav@557
  2731
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2732
                         Math.min(original.length - from, newLength));
jaroslav@557
  2733
        return copy;
jaroslav@557
  2734
    }
jaroslav@557
  2735
jaroslav@557
  2736
    /**
jaroslav@557
  2737
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2738
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2739
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2740
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2741
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2742
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2743
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2744
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2745
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2746
     * <tt>0d</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2747
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2748
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2749
     *
jaroslav@557
  2750
     * @param original the array from which a range is to be copied
jaroslav@557
  2751
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2752
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2753
     *     (This index may lie outside the array.)
jaroslav@557
  2754
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2755
     *     truncated or padded with zeros to obtain the required length
jaroslav@557
  2756
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2757
     *     or {@code from > original.length}
jaroslav@557
  2758
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2759
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2760
     * @since 1.6
jaroslav@557
  2761
     */
jaroslav@557
  2762
    public static double[] copyOfRange(double[] original, int from, int to) {
jaroslav@557
  2763
        int newLength = to - from;
jaroslav@557
  2764
        if (newLength < 0)
jaroslav@557
  2765
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2766
        double[] copy = new double[newLength];
jaroslav@557
  2767
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2768
                         Math.min(original.length - from, newLength));
jaroslav@557
  2769
        return copy;
jaroslav@557
  2770
    }
jaroslav@557
  2771
jaroslav@557
  2772
    /**
jaroslav@557
  2773
     * Copies the specified range of the specified array into a new array.
jaroslav@557
  2774
     * The initial index of the range (<tt>from</tt>) must lie between zero
jaroslav@557
  2775
     * and <tt>original.length</tt>, inclusive.  The value at
jaroslav@557
  2776
     * <tt>original[from]</tt> is placed into the initial element of the copy
jaroslav@557
  2777
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
jaroslav@557
  2778
     * Values from subsequent elements in the original array are placed into
jaroslav@557
  2779
     * subsequent elements in the copy.  The final index of the range
jaroslav@557
  2780
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
jaroslav@557
  2781
     * may be greater than <tt>original.length</tt>, in which case
jaroslav@557
  2782
     * <tt>false</tt> is placed in all elements of the copy whose index is
jaroslav@557
  2783
     * greater than or equal to <tt>original.length - from</tt>.  The length
jaroslav@557
  2784
     * of the returned array will be <tt>to - from</tt>.
jaroslav@557
  2785
     *
jaroslav@557
  2786
     * @param original the array from which a range is to be copied
jaroslav@557
  2787
     * @param from the initial index of the range to be copied, inclusive
jaroslav@557
  2788
     * @param to the final index of the range to be copied, exclusive.
jaroslav@557
  2789
     *     (This index may lie outside the array.)
jaroslav@557
  2790
     * @return a new array containing the specified range from the original array,
jaroslav@557
  2791
     *     truncated or padded with false elements to obtain the required length
jaroslav@557
  2792
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
jaroslav@557
  2793
     *     or {@code from > original.length}
jaroslav@557
  2794
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
jaroslav@557
  2795
     * @throws NullPointerException if <tt>original</tt> is null
jaroslav@557
  2796
     * @since 1.6
jaroslav@557
  2797
     */
jaroslav@557
  2798
    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
jaroslav@557
  2799
        int newLength = to - from;
jaroslav@557
  2800
        if (newLength < 0)
jaroslav@557
  2801
            throw new IllegalArgumentException(from + " > " + to);
jaroslav@557
  2802
        boolean[] copy = new boolean[newLength];
jaroslav@557
  2803
        System.arraycopy(original, from, copy, 0,
jaroslav@557
  2804
                         Math.min(original.length - from, newLength));
jaroslav@557
  2805
        return copy;
jaroslav@557
  2806
    }
jaroslav@557
  2807
jaroslav@557
  2808
    // Misc
jaroslav@557
  2809
jaroslav@557
  2810
    /**
jaroslav@557
  2811
     * Returns a fixed-size list backed by the specified array.  (Changes to
jaroslav@557
  2812
     * the returned list "write through" to the array.)  This method acts
jaroslav@557
  2813
     * as bridge between array-based and collection-based APIs, in
jaroslav@557
  2814
     * combination with {@link Collection#toArray}.  The returned list is
jaroslav@557
  2815
     * serializable and implements {@link RandomAccess}.
jaroslav@557
  2816
     *
jaroslav@557
  2817
     * <p>This method also provides a convenient way to create a fixed-size
jaroslav@557
  2818
     * list initialized to contain several elements:
jaroslav@557
  2819
     * <pre>
jaroslav@557
  2820
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
jaroslav@557
  2821
     * </pre>
jaroslav@557
  2822
     *
jaroslav@557
  2823
     * @param a the array by which the list will be backed
jaroslav@557
  2824
     * @return a list view of the specified array
jaroslav@557
  2825
     */
jaroslav@557
  2826
    @SafeVarargs
jaroslav@557
  2827
    public static <T> List<T> asList(T... a) {
jaroslav@557
  2828
        return new ArrayList<>(a);
jaroslav@557
  2829
    }
jaroslav@557
  2830
jaroslav@557
  2831
    /**
jaroslav@557
  2832
     * @serial include
jaroslav@557
  2833
     */
jaroslav@557
  2834
    private static class ArrayList<E> extends AbstractList<E>
jaroslav@557
  2835
        implements RandomAccess, java.io.Serializable
jaroslav@557
  2836
    {
jaroslav@557
  2837
        private static final long serialVersionUID = -2764017481108945198L;
jaroslav@557
  2838
        private final E[] a;
jaroslav@557
  2839
jaroslav@557
  2840
        ArrayList(E[] array) {
jaroslav@557
  2841
            if (array==null)
jaroslav@557
  2842
                throw new NullPointerException();
jaroslav@557
  2843
            a = array;
jaroslav@557
  2844
        }
jaroslav@557
  2845
jaroslav@557
  2846
        public int size() {
jaroslav@557
  2847
            return a.length;
jaroslav@557
  2848
        }
jaroslav@557
  2849
jaroslav@557
  2850
        public Object[] toArray() {
jaroslav@557
  2851
            return a.clone();
jaroslav@557
  2852
        }
jaroslav@557
  2853
jaroslav@557
  2854
        public <T> T[] toArray(T[] a) {
jaroslav@557
  2855
            int size = size();
jaroslav@557
  2856
            if (a.length < size)
jaroslav@557
  2857
                return Arrays.copyOf(this.a, size,
jaroslav@557
  2858
                                     (Class<? extends T[]>) a.getClass());
jaroslav@557
  2859
            System.arraycopy(this.a, 0, a, 0, size);
jaroslav@557
  2860
            if (a.length > size)
jaroslav@557
  2861
                a[size] = null;
jaroslav@557
  2862
            return a;
jaroslav@557
  2863
        }
jaroslav@557
  2864
jaroslav@557
  2865
        public E get(int index) {
jaroslav@557
  2866
            return a[index];
jaroslav@557
  2867
        }
jaroslav@557
  2868
jaroslav@557
  2869
        public E set(int index, E element) {
jaroslav@557
  2870
            E oldValue = a[index];
jaroslav@557
  2871
            a[index] = element;
jaroslav@557
  2872
            return oldValue;
jaroslav@557
  2873
        }
jaroslav@557
  2874
jaroslav@557
  2875
        public int indexOf(Object o) {
jaroslav@557
  2876
            if (o==null) {
jaroslav@557
  2877
                for (int i=0; i<a.length; i++)
jaroslav@557
  2878
                    if (a[i]==null)
jaroslav@557
  2879
                        return i;
jaroslav@557
  2880
            } else {
jaroslav@557
  2881
                for (int i=0; i<a.length; i++)
jaroslav@557
  2882
                    if (o.equals(a[i]))
jaroslav@557
  2883
                        return i;
jaroslav@557
  2884
            }
jaroslav@557
  2885
            return -1;
jaroslav@557
  2886
        }
jaroslav@557
  2887
jaroslav@557
  2888
        public boolean contains(Object o) {
jaroslav@557
  2889
            return indexOf(o) != -1;
jaroslav@557
  2890
        }
jaroslav@557
  2891
    }
jaroslav@557
  2892
jaroslav@557
  2893
    /**
jaroslav@557
  2894
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  2895
     * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  2896
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  2897
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  2898
     *
jaroslav@557
  2899
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  2900
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  2901
     * method on a {@link List} containing a sequence of {@link Long}
jaroslav@557
  2902
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  2903
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  2904
     *
jaroslav@557
  2905
     * @param a the array whose hash value to compute
jaroslav@557
  2906
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  2907
     * @since 1.5
jaroslav@557
  2908
     */
jaroslav@557
  2909
    public static int hashCode(long a[]) {
jaroslav@557
  2910
        if (a == null)
jaroslav@557
  2911
            return 0;
jaroslav@557
  2912
jaroslav@557
  2913
        int result = 1;
jaroslav@557
  2914
        for (long element : a) {
jaroslav@557
  2915
            int elementHash = (int)(element ^ (element >>> 32));
jaroslav@557
  2916
            result = 31 * result + elementHash;
jaroslav@557
  2917
        }
jaroslav@557
  2918
jaroslav@557
  2919
        return result;
jaroslav@557
  2920
    }
jaroslav@557
  2921
jaroslav@557
  2922
    /**
jaroslav@557
  2923
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  2924
     * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  2925
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  2926
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  2927
     *
jaroslav@557
  2928
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  2929
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  2930
     * method on a {@link List} containing a sequence of {@link Integer}
jaroslav@557
  2931
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  2932
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  2933
     *
jaroslav@557
  2934
     * @param a the array whose hash value to compute
jaroslav@557
  2935
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  2936
     * @since 1.5
jaroslav@557
  2937
     */
jaroslav@557
  2938
    public static int hashCode(int a[]) {
jaroslav@557
  2939
        if (a == null)
jaroslav@557
  2940
            return 0;
jaroslav@557
  2941
jaroslav@557
  2942
        int result = 1;
jaroslav@557
  2943
        for (int element : a)
jaroslav@557
  2944
            result = 31 * result + element;
jaroslav@557
  2945
jaroslav@557
  2946
        return result;
jaroslav@557
  2947
    }
jaroslav@557
  2948
jaroslav@557
  2949
    /**
jaroslav@557
  2950
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  2951
     * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  2952
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  2953
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  2954
     *
jaroslav@557
  2955
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  2956
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  2957
     * method on a {@link List} containing a sequence of {@link Short}
jaroslav@557
  2958
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  2959
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  2960
     *
jaroslav@557
  2961
     * @param a the array whose hash value to compute
jaroslav@557
  2962
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  2963
     * @since 1.5
jaroslav@557
  2964
     */
jaroslav@557
  2965
    public static int hashCode(short a[]) {
jaroslav@557
  2966
        if (a == null)
jaroslav@557
  2967
            return 0;
jaroslav@557
  2968
jaroslav@557
  2969
        int result = 1;
jaroslav@557
  2970
        for (short element : a)
jaroslav@557
  2971
            result = 31 * result + element;
jaroslav@557
  2972
jaroslav@557
  2973
        return result;
jaroslav@557
  2974
    }
jaroslav@557
  2975
jaroslav@557
  2976
    /**
jaroslav@557
  2977
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  2978
     * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  2979
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  2980
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  2981
     *
jaroslav@557
  2982
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  2983
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  2984
     * method on a {@link List} containing a sequence of {@link Character}
jaroslav@557
  2985
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  2986
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  2987
     *
jaroslav@557
  2988
     * @param a the array whose hash value to compute
jaroslav@557
  2989
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  2990
     * @since 1.5
jaroslav@557
  2991
     */
jaroslav@557
  2992
    public static int hashCode(char a[]) {
jaroslav@557
  2993
        if (a == null)
jaroslav@557
  2994
            return 0;
jaroslav@557
  2995
jaroslav@557
  2996
        int result = 1;
jaroslav@557
  2997
        for (char element : a)
jaroslav@557
  2998
            result = 31 * result + element;
jaroslav@557
  2999
jaroslav@557
  3000
        return result;
jaroslav@557
  3001
    }
jaroslav@557
  3002
jaroslav@557
  3003
    /**
jaroslav@557
  3004
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  3005
     * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  3006
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  3007
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  3008
     *
jaroslav@557
  3009
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  3010
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  3011
     * method on a {@link List} containing a sequence of {@link Byte}
jaroslav@557
  3012
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  3013
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  3014
     *
jaroslav@557
  3015
     * @param a the array whose hash value to compute
jaroslav@557
  3016
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  3017
     * @since 1.5
jaroslav@557
  3018
     */
jaroslav@557
  3019
    public static int hashCode(byte a[]) {
jaroslav@557
  3020
        if (a == null)
jaroslav@557
  3021
            return 0;
jaroslav@557
  3022
jaroslav@557
  3023
        int result = 1;
jaroslav@557
  3024
        for (byte element : a)
jaroslav@557
  3025
            result = 31 * result + element;
jaroslav@557
  3026
jaroslav@557
  3027
        return result;
jaroslav@557
  3028
    }
jaroslav@557
  3029
jaroslav@557
  3030
    /**
jaroslav@557
  3031
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  3032
     * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  3033
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  3034
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  3035
     *
jaroslav@557
  3036
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  3037
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  3038
     * method on a {@link List} containing a sequence of {@link Boolean}
jaroslav@557
  3039
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  3040
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  3041
     *
jaroslav@557
  3042
     * @param a the array whose hash value to compute
jaroslav@557
  3043
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  3044
     * @since 1.5
jaroslav@557
  3045
     */
jaroslav@557
  3046
    public static int hashCode(boolean a[]) {
jaroslav@557
  3047
        if (a == null)
jaroslav@557
  3048
            return 0;
jaroslav@557
  3049
jaroslav@557
  3050
        int result = 1;
jaroslav@557
  3051
        for (boolean element : a)
jaroslav@557
  3052
            result = 31 * result + (element ? 1231 : 1237);
jaroslav@557
  3053
jaroslav@557
  3054
        return result;
jaroslav@557
  3055
    }
jaroslav@557
  3056
jaroslav@557
  3057
    /**
jaroslav@557
  3058
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  3059
     * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  3060
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  3061
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  3062
     *
jaroslav@557
  3063
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  3064
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  3065
     * method on a {@link List} containing a sequence of {@link Float}
jaroslav@557
  3066
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  3067
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  3068
     *
jaroslav@557
  3069
     * @param a the array whose hash value to compute
jaroslav@557
  3070
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  3071
     * @since 1.5
jaroslav@557
  3072
     */
jaroslav@557
  3073
    public static int hashCode(float a[]) {
jaroslav@557
  3074
        if (a == null)
jaroslav@557
  3075
            return 0;
jaroslav@557
  3076
jaroslav@557
  3077
        int result = 1;
jaroslav@557
  3078
        for (float element : a)
jaroslav@557
  3079
            result = 31 * result + Float.floatToIntBits(element);
jaroslav@557
  3080
jaroslav@557
  3081
        return result;
jaroslav@557
  3082
    }
jaroslav@557
  3083
jaroslav@557
  3084
    /**
jaroslav@557
  3085
     * Returns a hash code based on the contents of the specified array.
jaroslav@557
  3086
     * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
jaroslav@557
  3087
     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  3088
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  3089
     *
jaroslav@557
  3090
     * <p>The value returned by this method is the same value that would be
jaroslav@557
  3091
     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
jaroslav@557
  3092
     * method on a {@link List} containing a sequence of {@link Double}
jaroslav@557
  3093
     * instances representing the elements of <tt>a</tt> in the same order.
jaroslav@557
  3094
     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
jaroslav@557
  3095
     *
jaroslav@557
  3096
     * @param a the array whose hash value to compute
jaroslav@557
  3097
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  3098
     * @since 1.5
jaroslav@557
  3099
     */
jaroslav@557
  3100
    public static int hashCode(double a[]) {
jaroslav@557
  3101
        if (a == null)
jaroslav@557
  3102
            return 0;
jaroslav@557
  3103
jaroslav@557
  3104
        int result = 1;
jaroslav@557
  3105
        for (double element : a) {
jaroslav@557
  3106
            long bits = Double.doubleToLongBits(element);
jaroslav@557
  3107
            result = 31 * result + (int)(bits ^ (bits >>> 32));
jaroslav@557
  3108
        }
jaroslav@557
  3109
        return result;
jaroslav@557
  3110
    }
jaroslav@557
  3111
jaroslav@557
  3112
    /**
jaroslav@557
  3113
     * Returns a hash code based on the contents of the specified array.  If
jaroslav@557
  3114
     * the array contains other arrays as elements, the hash code is based on
jaroslav@557
  3115
     * their identities rather than their contents.  It is therefore
jaroslav@557
  3116
     * acceptable to invoke this method on an array that contains itself as an
jaroslav@557
  3117
     * element,  either directly or indirectly through one or more levels of
jaroslav@557
  3118
     * arrays.
jaroslav@557
  3119
     *
jaroslav@557
  3120
     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
jaroslav@557
  3121
     * <tt>Arrays.equals(a, b)</tt>, it is also the case that
jaroslav@557
  3122
     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
jaroslav@557
  3123
     *
jaroslav@557
  3124
     * <p>The value returned by this method is equal to the value that would
jaroslav@557
  3125
     * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
jaroslav@557
  3126
     * is <tt>null</tt>, in which case <tt>0</tt> is returned.
jaroslav@557
  3127
     *
jaroslav@557
  3128
     * @param a the array whose content-based hash code to compute
jaroslav@557
  3129
     * @return a content-based hash code for <tt>a</tt>
jaroslav@557
  3130
     * @see #deepHashCode(Object[])
jaroslav@557
  3131
     * @since 1.5
jaroslav@557
  3132
     */
jaroslav@557
  3133
    public static int hashCode(Object a[]) {
jaroslav@557
  3134
        if (a == null)
jaroslav@557
  3135
            return 0;
jaroslav@557
  3136
jaroslav@557
  3137
        int result = 1;
jaroslav@557
  3138
jaroslav@557
  3139
        for (Object element : a)
jaroslav@557
  3140
            result = 31 * result + (element == null ? 0 : element.hashCode());
jaroslav@557
  3141
jaroslav@557
  3142
        return result;
jaroslav@557
  3143
    }
jaroslav@557
  3144
jaroslav@557
  3145
    /**
jaroslav@557
  3146
     * Returns a hash code based on the "deep contents" of the specified
jaroslav@557
  3147
     * array.  If the array contains other arrays as elements, the
jaroslav@557
  3148
     * hash code is based on their contents and so on, ad infinitum.
jaroslav@557
  3149
     * It is therefore unacceptable to invoke this method on an array that
jaroslav@557
  3150
     * contains itself as an element, either directly or indirectly through
jaroslav@557
  3151
     * one or more levels of arrays.  The behavior of such an invocation is
jaroslav@557
  3152
     * undefined.
jaroslav@557
  3153
     *
jaroslav@557
  3154
     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
jaroslav@557
  3155
     * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
jaroslav@557
  3156
     * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
jaroslav@557
  3157
     *
jaroslav@557
  3158
     * <p>The computation of the value returned by this method is similar to
jaroslav@557
  3159
     * that of the value returned by {@link List#hashCode()} on a list
jaroslav@557
  3160
     * containing the same elements as <tt>a</tt> in the same order, with one
jaroslav@557
  3161
     * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
jaroslav@557
  3162
     * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
jaroslav@557
  3163
     * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
jaroslav@557
  3164
     * if <tt>e</tt> is an array of a primitive type, or as by calling
jaroslav@557
  3165
     * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
jaroslav@557
  3166
     * of a reference type.  If <tt>a</tt> is <tt>null</tt>, this method
jaroslav@557
  3167
     * returns 0.
jaroslav@557
  3168
     *
jaroslav@557
  3169
     * @param a the array whose deep-content-based hash code to compute
jaroslav@557
  3170
     * @return a deep-content-based hash code for <tt>a</tt>
jaroslav@557
  3171
     * @see #hashCode(Object[])
jaroslav@557
  3172
     * @since 1.5
jaroslav@557
  3173
     */
jaroslav@557
  3174
    public static int deepHashCode(Object a[]) {
jaroslav@557
  3175
        if (a == null)
jaroslav@557
  3176
            return 0;
jaroslav@557
  3177
jaroslav@557
  3178
        int result = 1;
jaroslav@557
  3179
jaroslav@557
  3180
        for (Object element : a) {
jaroslav@557
  3181
            int elementHash = 0;
jaroslav@557
  3182
            if (element instanceof Object[])
jaroslav@557
  3183
                elementHash = deepHashCode((Object[]) element);
jaroslav@557
  3184
            else if (element instanceof byte[])
jaroslav@557
  3185
                elementHash = hashCode((byte[]) element);
jaroslav@557
  3186
            else if (element instanceof short[])
jaroslav@557
  3187
                elementHash = hashCode((short[]) element);
jaroslav@557
  3188
            else if (element instanceof int[])
jaroslav@557
  3189
                elementHash = hashCode((int[]) element);
jaroslav@557
  3190
            else if (element instanceof long[])
jaroslav@557
  3191
                elementHash = hashCode((long[]) element);
jaroslav@557
  3192
            else if (element instanceof char[])
jaroslav@557
  3193
                elementHash = hashCode((char[]) element);
jaroslav@557
  3194
            else if (element instanceof float[])
jaroslav@557
  3195
                elementHash = hashCode((float[]) element);
jaroslav@557
  3196
            else if (element instanceof double[])
jaroslav@557
  3197
                elementHash = hashCode((double[]) element);
jaroslav@557
  3198
            else if (element instanceof boolean[])
jaroslav@557
  3199
                elementHash = hashCode((boolean[]) element);
jaroslav@557
  3200
            else if (element != null)
jaroslav@557
  3201
                elementHash = element.hashCode();
jaroslav@557
  3202
jaroslav@557
  3203
            result = 31 * result + elementHash;
jaroslav@557
  3204
        }
jaroslav@557
  3205
jaroslav@557
  3206
        return result;
jaroslav@557
  3207
    }
jaroslav@557
  3208
jaroslav@557
  3209
    /**
jaroslav@557
  3210
     * Returns <tt>true</tt> if the two specified arrays are <i>deeply
jaroslav@557
  3211
     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
jaroslav@557
  3212
     * method, this method is appropriate for use with nested arrays of
jaroslav@557
  3213
     * arbitrary depth.
jaroslav@557
  3214
     *
jaroslav@557
  3215
     * <p>Two array references are considered deeply equal if both
jaroslav@557
  3216
     * are <tt>null</tt>, or if they refer to arrays that contain the same
jaroslav@557
  3217
     * number of elements and all corresponding pairs of elements in the two
jaroslav@557
  3218
     * arrays are deeply equal.
jaroslav@557
  3219
     *
jaroslav@557
  3220
     * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
jaroslav@557
  3221
     * deeply equal if any of the following conditions hold:
jaroslav@557
  3222
     * <ul>
jaroslav@557
  3223
     *    <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
jaroslav@557
  3224
     *         types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
jaroslav@557
  3225
     *    <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
jaroslav@557
  3226
     *         type, and the appropriate overloading of
jaroslav@557
  3227
     *         <tt>Arrays.equals(e1, e2)</tt> would return true.
jaroslav@557
  3228
     *    <li> <tt>e1 == e2</tt>
jaroslav@557
  3229
     *    <li> <tt>e1.equals(e2)</tt> would return true.
jaroslav@557
  3230
     * </ul>
jaroslav@557
  3231
     * Note that this definition permits <tt>null</tt> elements at any depth.
jaroslav@557
  3232
     *
jaroslav@557
  3233
     * <p>If either of the specified arrays contain themselves as elements
jaroslav@557
  3234
     * either directly or indirectly through one or more levels of arrays,
jaroslav@557
  3235
     * the behavior of this method is undefined.
jaroslav@557
  3236
     *
jaroslav@557
  3237
     * @param a1 one array to be tested for equality
jaroslav@557
  3238
     * @param a2 the other array to be tested for equality
jaroslav@557
  3239
     * @return <tt>true</tt> if the two arrays are equal
jaroslav@557
  3240
     * @see #equals(Object[],Object[])
jaroslav@557
  3241
     * @see Objects#deepEquals(Object, Object)
jaroslav@557
  3242
     * @since 1.5
jaroslav@557
  3243
     */
jaroslav@557
  3244
    public static boolean deepEquals(Object[] a1, Object[] a2) {
jaroslav@557
  3245
        if (a1 == a2)
jaroslav@557
  3246
            return true;
jaroslav@557
  3247
        if (a1 == null || a2==null)
jaroslav@557
  3248
            return false;
jaroslav@557
  3249
        int length = a1.length;
jaroslav@557
  3250
        if (a2.length != length)
jaroslav@557
  3251
            return false;
jaroslav@557
  3252
jaroslav@557
  3253
        for (int i = 0; i < length; i++) {
jaroslav@557
  3254
            Object e1 = a1[i];
jaroslav@557
  3255
            Object e2 = a2[i];
jaroslav@557
  3256
jaroslav@557
  3257
            if (e1 == e2)
jaroslav@557
  3258
                continue;
jaroslav@557
  3259
            if (e1 == null)
jaroslav@557
  3260
                return false;
jaroslav@557
  3261
jaroslav@557
  3262
            // Figure out whether the two elements are equal
jaroslav@557
  3263
            boolean eq = deepEquals0(e1, e2);
jaroslav@557
  3264
jaroslav@557
  3265
            if (!eq)
jaroslav@557
  3266
                return false;
jaroslav@557
  3267
        }
jaroslav@557
  3268
        return true;
jaroslav@557
  3269
    }
jaroslav@557
  3270
jaroslav@557
  3271
    static boolean deepEquals0(Object e1, Object e2) {
jaroslav@557
  3272
        assert e1 != null;
jaroslav@557
  3273
        boolean eq;
jaroslav@557
  3274
        if (e1 instanceof Object[] && e2 instanceof Object[])
jaroslav@557
  3275
            eq = deepEquals ((Object[]) e1, (Object[]) e2);
jaroslav@557
  3276
        else if (e1 instanceof byte[] && e2 instanceof byte[])
jaroslav@557
  3277
            eq = equals((byte[]) e1, (byte[]) e2);
jaroslav@557
  3278
        else if (e1 instanceof short[] && e2 instanceof short[])
jaroslav@557
  3279
            eq = equals((short[]) e1, (short[]) e2);
jaroslav@557
  3280
        else if (e1 instanceof int[] && e2 instanceof int[])
jaroslav@557
  3281
            eq = equals((int[]) e1, (int[]) e2);
jaroslav@557
  3282
        else if (e1 instanceof long[] && e2 instanceof long[])
jaroslav@557
  3283
            eq = equals((long[]) e1, (long[]) e2);
jaroslav@557
  3284
        else if (e1 instanceof char[] && e2 instanceof char[])
jaroslav@557
  3285
            eq = equals((char[]) e1, (char[]) e2);
jaroslav@557
  3286
        else if (e1 instanceof float[] && e2 instanceof float[])
jaroslav@557
  3287
            eq = equals((float[]) e1, (float[]) e2);
jaroslav@557
  3288
        else if (e1 instanceof double[] && e2 instanceof double[])
jaroslav@557
  3289
            eq = equals((double[]) e1, (double[]) e2);
jaroslav@557
  3290
        else if (e1 instanceof boolean[] && e2 instanceof boolean[])
jaroslav@557
  3291
            eq = equals((boolean[]) e1, (boolean[]) e2);
jaroslav@557
  3292
        else
jaroslav@557
  3293
            eq = e1.equals(e2);
jaroslav@557
  3294
        return eq;
jaroslav@557
  3295
    }
jaroslav@557
  3296
jaroslav@557
  3297
    /**
jaroslav@557
  3298
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3299
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3300
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
jaroslav@557
  3301
     * separated by the characters <tt>", "</tt> (a comma followed by a
jaroslav@557
  3302
     * space).  Elements are converted to strings as by
jaroslav@557
  3303
     * <tt>String.valueOf(long)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
jaroslav@557
  3304
     * is <tt>null</tt>.
jaroslav@557
  3305
     *
jaroslav@557
  3306
     * @param a the array whose string representation to return
jaroslav@557
  3307
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3308
     * @since 1.5
jaroslav@557
  3309
     */
jaroslav@557
  3310
    public static String toString(long[] a) {
jaroslav@557
  3311
        if (a == null)
jaroslav@557
  3312
            return "null";
jaroslav@557
  3313
        int iMax = a.length - 1;
jaroslav@557
  3314
        if (iMax == -1)
jaroslav@557
  3315
            return "[]";
jaroslav@557
  3316
jaroslav@557
  3317
        StringBuilder b = new StringBuilder();
jaroslav@557
  3318
        b.append('[');
jaroslav@557
  3319
        for (int i = 0; ; i++) {
jaroslav@557
  3320
            b.append(a[i]);
jaroslav@557
  3321
            if (i == iMax)
jaroslav@557
  3322
                return b.append(']').toString();
jaroslav@557
  3323
            b.append(", ");
jaroslav@557
  3324
        }
jaroslav@557
  3325
    }
jaroslav@557
  3326
jaroslav@557
  3327
    /**
jaroslav@557
  3328
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3329
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3330
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
jaroslav@557
  3331
     * separated by the characters <tt>", "</tt> (a comma followed by a
jaroslav@557
  3332
     * space).  Elements are converted to strings as by
jaroslav@557
  3333
     * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
jaroslav@557
  3334
     * <tt>null</tt>.
jaroslav@557
  3335
     *
jaroslav@557
  3336
     * @param a the array whose string representation to return
jaroslav@557
  3337
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3338
     * @since 1.5
jaroslav@557
  3339
     */
jaroslav@557
  3340
    public static String toString(int[] a) {
jaroslav@557
  3341
        if (a == null)
jaroslav@557
  3342
            return "null";
jaroslav@557
  3343
        int iMax = a.length - 1;
jaroslav@557
  3344
        if (iMax == -1)
jaroslav@557
  3345
            return "[]";
jaroslav@557
  3346
jaroslav@557
  3347
        StringBuilder b = new StringBuilder();
jaroslav@557
  3348
        b.append('[');
jaroslav@557
  3349
        for (int i = 0; ; i++) {
jaroslav@557
  3350
            b.append(a[i]);
jaroslav@557
  3351
            if (i == iMax)
jaroslav@557
  3352
                return b.append(']').toString();
jaroslav@557
  3353
            b.append(", ");
jaroslav@557
  3354
        }
jaroslav@557
  3355
    }
jaroslav@557
  3356
jaroslav@557
  3357
    /**
jaroslav@557
  3358
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3359
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3360
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
jaroslav@557
  3361
     * separated by the characters <tt>", "</tt> (a comma followed by a
jaroslav@557
  3362
     * space).  Elements are converted to strings as by
jaroslav@557
  3363
     * <tt>String.valueOf(short)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
jaroslav@557
  3364
     * is <tt>null</tt>.
jaroslav@557
  3365
     *
jaroslav@557
  3366
     * @param a the array whose string representation to return
jaroslav@557
  3367
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3368
     * @since 1.5
jaroslav@557
  3369
     */
jaroslav@557
  3370
    public static String toString(short[] a) {
jaroslav@557
  3371
        if (a == null)
jaroslav@557
  3372
            return "null";
jaroslav@557
  3373
        int iMax = a.length - 1;
jaroslav@557
  3374
        if (iMax == -1)
jaroslav@557
  3375
            return "[]";
jaroslav@557
  3376
jaroslav@557
  3377
        StringBuilder b = new StringBuilder();
jaroslav@557
  3378
        b.append('[');
jaroslav@557
  3379
        for (int i = 0; ; i++) {
jaroslav@557
  3380
            b.append(a[i]);
jaroslav@557
  3381
            if (i == iMax)
jaroslav@557
  3382
                return b.append(']').toString();
jaroslav@557
  3383
            b.append(", ");
jaroslav@557
  3384
        }
jaroslav@557
  3385
    }
jaroslav@557
  3386
jaroslav@557
  3387
    /**
jaroslav@557
  3388
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3389
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3390
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
jaroslav@557
  3391
     * separated by the characters <tt>", "</tt> (a comma followed by a
jaroslav@557
  3392
     * space).  Elements are converted to strings as by
jaroslav@557
  3393
     * <tt>String.valueOf(char)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
jaroslav@557
  3394
     * is <tt>null</tt>.
jaroslav@557
  3395
     *
jaroslav@557
  3396
     * @param a the array whose string representation to return
jaroslav@557
  3397
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3398
     * @since 1.5
jaroslav@557
  3399
     */
jaroslav@557
  3400
    public static String toString(char[] a) {
jaroslav@557
  3401
        if (a == null)
jaroslav@557
  3402
            return "null";
jaroslav@557
  3403
        int iMax = a.length - 1;
jaroslav@557
  3404
        if (iMax == -1)
jaroslav@557
  3405
            return "[]";
jaroslav@557
  3406
jaroslav@557
  3407
        StringBuilder b = new StringBuilder();
jaroslav@557
  3408
        b.append('[');
jaroslav@557
  3409
        for (int i = 0; ; i++) {
jaroslav@557
  3410
            b.append(a[i]);
jaroslav@557
  3411
            if (i == iMax)
jaroslav@557
  3412
                return b.append(']').toString();
jaroslav@557
  3413
            b.append(", ");
jaroslav@557
  3414
        }
jaroslav@557
  3415
    }
jaroslav@557
  3416
jaroslav@557
  3417
    /**
jaroslav@557
  3418
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3419
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3420
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements
jaroslav@557
  3421
     * are separated by the characters <tt>", "</tt> (a comma followed
jaroslav@557
  3422
     * by a space).  Elements are converted to strings as by
jaroslav@557
  3423
     * <tt>String.valueOf(byte)</tt>.  Returns <tt>"null"</tt> if
jaroslav@557
  3424
     * <tt>a</tt> is <tt>null</tt>.
jaroslav@557
  3425
     *
jaroslav@557
  3426
     * @param a the array whose string representation to return
jaroslav@557
  3427
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3428
     * @since 1.5
jaroslav@557
  3429
     */
jaroslav@557
  3430
    public static String toString(byte[] a) {
jaroslav@557
  3431
        if (a == null)
jaroslav@557
  3432
            return "null";
jaroslav@557
  3433
        int iMax = a.length - 1;
jaroslav@557
  3434
        if (iMax == -1)
jaroslav@557
  3435
            return "[]";
jaroslav@557
  3436
jaroslav@557
  3437
        StringBuilder b = new StringBuilder();
jaroslav@557
  3438
        b.append('[');
jaroslav@557
  3439
        for (int i = 0; ; i++) {
jaroslav@557
  3440
            b.append(a[i]);
jaroslav@557
  3441
            if (i == iMax)
jaroslav@557
  3442
                return b.append(']').toString();
jaroslav@557
  3443
            b.append(", ");
jaroslav@557
  3444
        }
jaroslav@557
  3445
    }
jaroslav@557
  3446
jaroslav@557
  3447
    /**
jaroslav@557
  3448
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3449
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3450
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
jaroslav@557
  3451
     * separated by the characters <tt>", "</tt> (a comma followed by a
jaroslav@557
  3452
     * space).  Elements are converted to strings as by
jaroslav@557
  3453
     * <tt>String.valueOf(boolean)</tt>.  Returns <tt>"null"</tt> if
jaroslav@557
  3454
     * <tt>a</tt> is <tt>null</tt>.
jaroslav@557
  3455
     *
jaroslav@557
  3456
     * @param a the array whose string representation to return
jaroslav@557
  3457
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3458
     * @since 1.5
jaroslav@557
  3459
     */
jaroslav@557
  3460
    public static String toString(boolean[] a) {
jaroslav@557
  3461
        if (a == null)
jaroslav@557
  3462
            return "null";
jaroslav@557
  3463
        int iMax = a.length - 1;
jaroslav@557
  3464
        if (iMax == -1)
jaroslav@557
  3465
            return "[]";
jaroslav@557
  3466
jaroslav@557
  3467
        StringBuilder b = new StringBuilder();
jaroslav@557
  3468
        b.append('[');
jaroslav@557
  3469
        for (int i = 0; ; i++) {
jaroslav@557
  3470
            b.append(a[i]);
jaroslav@557
  3471
            if (i == iMax)
jaroslav@557
  3472
                return b.append(']').toString();
jaroslav@557
  3473
            b.append(", ");
jaroslav@557
  3474
        }
jaroslav@557
  3475
    }
jaroslav@557
  3476
jaroslav@557
  3477
    /**
jaroslav@557
  3478
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3479
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3480
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
jaroslav@557
  3481
     * separated by the characters <tt>", "</tt> (a comma followed by a
jaroslav@557
  3482
     * space).  Elements are converted to strings as by
jaroslav@557
  3483
     * <tt>String.valueOf(float)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
jaroslav@557
  3484
     * is <tt>null</tt>.
jaroslav@557
  3485
     *
jaroslav@557
  3486
     * @param a the array whose string representation to return
jaroslav@557
  3487
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3488
     * @since 1.5
jaroslav@557
  3489
     */
jaroslav@557
  3490
    public static String toString(float[] a) {
jaroslav@557
  3491
        if (a == null)
jaroslav@557
  3492
            return "null";
jaroslav@557
  3493
jaroslav@557
  3494
        int iMax = a.length - 1;
jaroslav@557
  3495
        if (iMax == -1)
jaroslav@557
  3496
            return "[]";
jaroslav@557
  3497
jaroslav@557
  3498
        StringBuilder b = new StringBuilder();
jaroslav@557
  3499
        b.append('[');
jaroslav@557
  3500
        for (int i = 0; ; i++) {
jaroslav@557
  3501
            b.append(a[i]);
jaroslav@557
  3502
            if (i == iMax)
jaroslav@557
  3503
                return b.append(']').toString();
jaroslav@557
  3504
            b.append(", ");
jaroslav@557
  3505
        }
jaroslav@557
  3506
    }
jaroslav@557
  3507
jaroslav@557
  3508
    /**
jaroslav@557
  3509
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3510
     * The string representation consists of a list of the array's elements,
jaroslav@557
  3511
     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
jaroslav@557
  3512
     * separated by the characters <tt>", "</tt> (a comma followed by a
jaroslav@557
  3513
     * space).  Elements are converted to strings as by
jaroslav@557
  3514
     * <tt>String.valueOf(double)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
jaroslav@557
  3515
     * is <tt>null</tt>.
jaroslav@557
  3516
     *
jaroslav@557
  3517
     * @param a the array whose string representation to return
jaroslav@557
  3518
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3519
     * @since 1.5
jaroslav@557
  3520
     */
jaroslav@557
  3521
    public static String toString(double[] a) {
jaroslav@557
  3522
        if (a == null)
jaroslav@557
  3523
            return "null";
jaroslav@557
  3524
        int iMax = a.length - 1;
jaroslav@557
  3525
        if (iMax == -1)
jaroslav@557
  3526
            return "[]";
jaroslav@557
  3527
jaroslav@557
  3528
        StringBuilder b = new StringBuilder();
jaroslav@557
  3529
        b.append('[');
jaroslav@557
  3530
        for (int i = 0; ; i++) {
jaroslav@557
  3531
            b.append(a[i]);
jaroslav@557
  3532
            if (i == iMax)
jaroslav@557
  3533
                return b.append(']').toString();
jaroslav@557
  3534
            b.append(", ");
jaroslav@557
  3535
        }
jaroslav@557
  3536
    }
jaroslav@557
  3537
jaroslav@557
  3538
    /**
jaroslav@557
  3539
     * Returns a string representation of the contents of the specified array.
jaroslav@557
  3540
     * If the array contains other arrays as elements, they are converted to
jaroslav@557
  3541
     * strings by the {@link Object#toString} method inherited from
jaroslav@557
  3542
     * <tt>Object</tt>, which describes their <i>identities</i> rather than
jaroslav@557
  3543
     * their contents.
jaroslav@557
  3544
     *
jaroslav@557
  3545
     * <p>The value returned by this method is equal to the value that would
jaroslav@557
  3546
     * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
jaroslav@557
  3547
     * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
jaroslav@557
  3548
     *
jaroslav@557
  3549
     * @param a the array whose string representation to return
jaroslav@557
  3550
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3551
     * @see #deepToString(Object[])
jaroslav@557
  3552
     * @since 1.5
jaroslav@557
  3553
     */
jaroslav@557
  3554
    public static String toString(Object[] a) {
jaroslav@557
  3555
        if (a == null)
jaroslav@557
  3556
            return "null";
jaroslav@557
  3557
jaroslav@557
  3558
        int iMax = a.length - 1;
jaroslav@557
  3559
        if (iMax == -1)
jaroslav@557
  3560
            return "[]";
jaroslav@557
  3561
jaroslav@557
  3562
        StringBuilder b = new StringBuilder();
jaroslav@557
  3563
        b.append('[');
jaroslav@557
  3564
        for (int i = 0; ; i++) {
jaroslav@557
  3565
            b.append(String.valueOf(a[i]));
jaroslav@557
  3566
            if (i == iMax)
jaroslav@557
  3567
                return b.append(']').toString();
jaroslav@557
  3568
            b.append(", ");
jaroslav@557
  3569
        }
jaroslav@557
  3570
    }
jaroslav@557
  3571
jaroslav@557
  3572
    /**
jaroslav@557
  3573
     * Returns a string representation of the "deep contents" of the specified
jaroslav@557
  3574
     * array.  If the array contains other arrays as elements, the string
jaroslav@557
  3575
     * representation contains their contents and so on.  This method is
jaroslav@557
  3576
     * designed for converting multidimensional arrays to strings.
jaroslav@557
  3577
     *
jaroslav@557
  3578
     * <p>The string representation consists of a list of the array's
jaroslav@557
  3579
     * elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent
jaroslav@557
  3580
     * elements are separated by the characters <tt>", "</tt> (a comma
jaroslav@557
  3581
     * followed by a space).  Elements are converted to strings as by
jaroslav@557
  3582
     * <tt>String.valueOf(Object)</tt>, unless they are themselves
jaroslav@557
  3583
     * arrays.
jaroslav@557
  3584
     *
jaroslav@557
  3585
     * <p>If an element <tt>e</tt> is an array of a primitive type, it is
jaroslav@557
  3586
     * converted to a string as by invoking the appropriate overloading of
jaroslav@557
  3587
     * <tt>Arrays.toString(e)</tt>.  If an element <tt>e</tt> is an array of a
jaroslav@557
  3588
     * reference type, it is converted to a string as by invoking
jaroslav@557
  3589
     * this method recursively.
jaroslav@557
  3590
     *
jaroslav@557
  3591
     * <p>To avoid infinite recursion, if the specified array contains itself
jaroslav@557
  3592
     * as an element, or contains an indirect reference to itself through one
jaroslav@557
  3593
     * or more levels of arrays, the self-reference is converted to the string
jaroslav@557
  3594
     * <tt>"[...]"</tt>.  For example, an array containing only a reference
jaroslav@557
  3595
     * to itself would be rendered as <tt>"[[...]]"</tt>.
jaroslav@557
  3596
     *
jaroslav@557
  3597
     * <p>This method returns <tt>"null"</tt> if the specified array
jaroslav@557
  3598
     * is <tt>null</tt>.
jaroslav@557
  3599
     *
jaroslav@557
  3600
     * @param a the array whose string representation to return
jaroslav@557
  3601
     * @return a string representation of <tt>a</tt>
jaroslav@557
  3602
     * @see #toString(Object[])
jaroslav@557
  3603
     * @since 1.5
jaroslav@557
  3604
     */
jaroslav@557
  3605
    public static String deepToString(Object[] a) {
jaroslav@557
  3606
        if (a == null)
jaroslav@557
  3607
            return "null";
jaroslav@557
  3608
jaroslav@557
  3609
        int bufLen = 20 * a.length;
jaroslav@557
  3610
        if (a.length != 0 && bufLen <= 0)
jaroslav@557
  3611
            bufLen = Integer.MAX_VALUE;
jaroslav@557
  3612
        StringBuilder buf = new StringBuilder(bufLen);
jaroslav@557
  3613
        deepToString(a, buf, new HashSet<Object[]>());
jaroslav@557
  3614
        return buf.toString();
jaroslav@557
  3615
    }
jaroslav@557
  3616
jaroslav@557
  3617
    private static void deepToString(Object[] a, StringBuilder buf,
jaroslav@557
  3618
                                     Set<Object[]> dejaVu) {
jaroslav@557
  3619
        if (a == null) {
jaroslav@557
  3620
            buf.append("null");
jaroslav@557
  3621
            return;
jaroslav@557
  3622
        }
jaroslav@557
  3623
        int iMax = a.length - 1;
jaroslav@557
  3624
        if (iMax == -1) {
jaroslav@557
  3625
            buf.append("[]");
jaroslav@557
  3626
            return;
jaroslav@557
  3627
        }
jaroslav@557
  3628
jaroslav@557
  3629
        dejaVu.add(a);
jaroslav@557
  3630
        buf.append('[');
jaroslav@557
  3631
        for (int i = 0; ; i++) {
jaroslav@557
  3632
jaroslav@557
  3633
            Object element = a[i];
jaroslav@557
  3634
            if (element == null) {
jaroslav@557
  3635
                buf.append("null");
jaroslav@557
  3636
            } else {
jaroslav@557
  3637
                Class eClass = element.getClass();
jaroslav@557
  3638
jaroslav@557
  3639
                if (eClass.isArray()) {
jaroslav@557
  3640
                    if (eClass == byte[].class)
jaroslav@557
  3641
                        buf.append(toString((byte[]) element));
jaroslav@557
  3642
                    else if (eClass == short[].class)
jaroslav@557
  3643
                        buf.append(toString((short[]) element));
jaroslav@557
  3644
                    else if (eClass == int[].class)
jaroslav@557
  3645
                        buf.append(toString((int[]) element));
jaroslav@557
  3646
                    else if (eClass == long[].class)
jaroslav@557
  3647
                        buf.append(toString((long[]) element));
jaroslav@557
  3648
                    else if (eClass == char[].class)
jaroslav@557
  3649
                        buf.append(toString((char[]) element));
jaroslav@557
  3650
                    else if (eClass == float[].class)
jaroslav@557
  3651
                        buf.append(toString((float[]) element));
jaroslav@557
  3652
                    else if (eClass == double[].class)
jaroslav@557
  3653
                        buf.append(toString((double[]) element));
jaroslav@557
  3654
                    else if (eClass == boolean[].class)
jaroslav@557
  3655
                        buf.append(toString((boolean[]) element));
jaroslav@557
  3656
                    else { // element is an array of object references
jaroslav@557
  3657
                        if (dejaVu.contains(element))
jaroslav@557
  3658
                            buf.append("[...]");
jaroslav@557
  3659
                        else
jaroslav@557
  3660
                            deepToString((Object[])element, buf, dejaVu);
jaroslav@557
  3661
                    }
jaroslav@557
  3662
                } else {  // element is non-null and not an array
jaroslav@557
  3663
                    buf.append(element.toString());
jaroslav@557
  3664
                }
jaroslav@557
  3665
            }
jaroslav@557
  3666
            if (i == iMax)
jaroslav@557
  3667
                break;
jaroslav@557
  3668
            buf.append(", ");
jaroslav@557
  3669
        }
jaroslav@557
  3670
        buf.append(']');
jaroslav@557
  3671
        dejaVu.remove(a);
jaroslav@557
  3672
    }
jaroslav@557
  3673
}