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