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