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