Arrays.java revision c353afc367d0bc83ac1c2bf33afb07e9ae0fbdff
151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project
34c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.util;
2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.*;
3060796efea3a74e02aea384b8eb56103ea21b880bPrzemyslaw Szczepaniakimport java.util.function.Consumer;
3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This class contains various methods for manipulating arrays (such as
3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorting and searching). This class also contains a static factory
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that allows arrays to be viewed as lists.
3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The methods in this class all throw a {@code NullPointerException},
3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the specified array reference is null, except where noted.
3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The documentation for the methods contained in this class includes
4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * briefs description of the <i>implementations</i>. Such descriptions should
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be regarded as <i>implementation notes</i>, rather than parts of the
4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>specification</i>. Implementors should feel free to substitute other
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * algorithms, so long as the specification itself is adhered to. (For
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, the algorithm used by {@code sort(Object[])} does not have to be
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a MergeSort, but it does have to be <i>stable</i>.)
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This class is a member of the
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <a href="{@docRoot}/../technotes/guides/collections/index.html">
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Collections Framework</a>.
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Josh Bloch
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Neal Gafter
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author John Rose
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since  1.2
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic class Arrays {
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Suppresses default constructor, ensuring non-instantiability.
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private Arrays() {}
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting of primitive type arrays.
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(int[] a) {
78c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(int[] a, int fromIndex, int toIndex) {
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
103c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(long[] a) {
118c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(long[] a, int fromIndex, int toIndex) {
14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
143c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(short[] a) {
158c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(short[] a, int fromIndex, int toIndex) {
18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
183c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(char[] a) {
198c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(char[] a, int fromIndex, int toIndex) {
22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
223c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(byte[] a) {
238c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1);
23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(byte[] a, int fromIndex, int toIndex) {
26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
26651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all float
27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Float.NaN} values are considered equal.
27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(float[] a) {
286c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all float
29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0f} and {@code Float.NaN} is considered greater than any
30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Float.NaN} values are considered equal.
30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(float[] a, int fromIndex, int toIndex) {
31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
319c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array into ascending numerical order.
32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all double
32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Double.NaN} values are considered equal.
33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(double[] a) {
342c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the array into ascending order. The range
34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to be sorted extends from the index {@code fromIndex}, inclusive, to
34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range to be sorted is empty.
35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The {@code <} relation does not provide a total order on all double
35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * value compares neither less than, greater than, nor equal to any value,
35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * even itself. This method uses the total order imposed by the method
35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code 0.0d} and {@code Double.NaN} is considered greater than any
35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * other value and all {@code Double.NaN} values are considered equal.
35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers O(n log(n)) performance on many data sets that cause other
36251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * quicksorts to degrade to quadratic performance, and is typically
36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * faster than traditional (one-pivot) Quicksort implementations.
36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element, inclusive, to be sorted
36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element, exclusive, to be sorted
36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex}
37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     if {@code fromIndex < 0} or {@code toIndex > a.length}
37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(double[] a, int fromIndex, int toIndex) {
37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
375c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting of complex type arrays.
38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Old merge sort implementation can be selected (for
38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * compatibility with broken comparators) using a system property.
38551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Cannot be a static boolean in the enclosing class due to
38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * circular dependencies. To be removed in a future release.
38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static final class LegacyMergeSort {
3891ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski        // Android-changed: Never use circular merge sort.
3901ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski        private static final boolean userRequested = false;
39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /*
39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If this platform has an optimizing VM, check whether ComparableTimSort
39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * offers any performance benefit over TimSort in conjunction with a
39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * comparator that returns:
39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    {@code ((Comparable)first).compareTo(Second)}.
39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If not, you are better off deleting ComparableTimSort to
39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * eliminate the code duplication.  In other words, the commented
40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * out code below is the preferable implementation for sorting
40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays of Comparables if it offers sufficient performance.
40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    /**
40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * A comparator that implements the natural ordering of a group of
40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * mutually comparable elements.  Using this comparator saves us
40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * from duplicating most of the code in this file (one version for
40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     * Comparables, one for explicit Comparators).
40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//     */
41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    private static final Comparator<Object> NATURAL_ORDER =
41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//            new Comparator<Object>() {
41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        @SuppressWarnings("unchecked")
41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        public int compare(Object first, Object second) {
41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//            return ((Comparable<Object>)first).compareTo(second);
41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        }
41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    };
41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//
41851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    public static void sort(Object[] a) {
41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        sort(a, 0, a.length, NATURAL_ORDER);
42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    }
42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//
42251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    public static void sort(Object[] a, int fromIndex, int toIndex) {
42351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//        sort(a, fromIndex, toIndex, NATURAL_ORDER);
42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski//    }
42551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array of objects into ascending order, according
42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the {@linkplain Comparable natural ordering} of its elements.
42951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * All elements in the array must implement the {@link Comparable}
43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface.  Furthermore, all elements in the array must be
43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not throw a {@code ClassCastException} for any elements {@code e1}
43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and {@code e2} in the array).
43451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
44151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
45751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> (for example, strings and integers)
46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException (optional) if the natural
46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         ordering of the array elements is found to violate the
46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@link Comparable} contract
46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(Object[] a) {
46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (LegacyMergeSort.userRequested)
47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            legacyMergeSort(a);
47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
472c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void legacyMergeSort(Object[] a) {
47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Object[] aux = a.clone();
47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(aux, a, 0, a.length, 0);
47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
48151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the specified array of objects into
48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending order, according to the
48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering} of its
48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements.  The range to be sorted extends from index
48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
48751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
48851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements in this range must implement the {@link Comparable}
48951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * interface.  Furthermore, all elements in this range must be <i>mutually
49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code ClassCastException} for any elements {@code e1} and
49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code e2} in the array).
49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
52251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        sorted
52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be sorted
52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         (optional) if the natural ordering of the array elements is
52651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         found to violate the {@link Comparable} contract
52751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
52851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@code toIndex > a.length}
52951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are
53051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         not <i>mutually comparable</i> (for example, strings and
53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         integers).
53251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void sort(Object[] a, int fromIndex, int toIndex) {
534c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        rangeCheck(a.length, fromIndex, toIndex);
53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (LegacyMergeSort.userRequested)
53651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            legacyMergeSort(a, fromIndex, toIndex);
53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
538c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0);
53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
54251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void legacyMergeSort(Object[] a,
54351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                        int fromIndex, int toIndex) {
54451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
54551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Object[] aux = copyOfRange(a, fromIndex, toIndex);
54651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
55051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Tuning parameter: list size at or below which insertion sort will be
55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * used in preference to mergesort.
55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static final int INSERTIONSORT_THRESHOLD = 7;
55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Src is the source array that starts at index 0
55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Dest is the (possibly larger) array destination with a possible offset
55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * low is the index in dest to start sorting
56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * high is the end index in dest to end sorting
56151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * off is the offset to generate corresponding low, high in src
56251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void mergeSort(Object[] src,
56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  Object[] dest,
56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int low,
56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int high,
56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int off) {
56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = high - low;
57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Insertion sort on smallest arrays
57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (length < INSERTIONSORT_THRESHOLD) {
57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            for (int i=low; i<high; i++)
57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int j=i; j>low &&
57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    swap(dest, j, j-1);
57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Recursively sort halves of dest into src
58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destLow  = low;
58251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destHigh = high;
58351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        low  += off;
58451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        high += off;
58551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int mid = (low + high) >>> 1;
58651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, low, mid, -off);
58751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, mid, high, -off);
58851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
58951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // If list is already sorted, just copy from src to dest.  This is an
59051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // optimization that results in faster sorts for nearly ordered lists.
59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            System.arraycopy(src, low, dest, destLow, length);
59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Merge sorted halves (now in src) into dest
59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[p++];
60051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[q++];
60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
60351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
60451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
60651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Swaps x[a] with x[b].
60751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
60851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void swap(Object[] x, int a, int b) {
60951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        Object t = x[a];
61051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        x[a] = x[b];
61151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        x[b] = t;
61251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
61351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
61451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
61551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified array of objects according to the order induced by
61651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified comparator.  All elements in the array must be
61751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> by the specified comparator (that is,
61851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
61951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for any elements {@code e1} and {@code e2} in the array).
62051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
62151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
62251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
63051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
63151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
63251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
63351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
63451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
63551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
63651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
63751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
63851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
64451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
64651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
64751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
64851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator to determine the order of the array.  A
64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@code null} value indicates that the elements'
65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
65151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are
65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         not <i>mutually comparable</i> using the specified comparator
65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException (optional) if the comparator is
65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         found to violate the {@link Comparator} contract
65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> void sort(T[] a, Comparator<? super T> c) {
657c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        if (c == null) {
658c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            sort(a);
659c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        } else {
660c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            if (LegacyMergeSort.userRequested)
661c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath                legacyMergeSort(a, c);
662c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            else
663c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath                TimSort.sort(a, 0, a.length, c, null, 0, 0);
664c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        }
66551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
66651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
66751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
66851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
66951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] aux = a.clone();
67051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c==null)
67151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, 0, a.length, 0);
67251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
67351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, 0, a.length, 0, c);
67451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
67551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
67651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
67751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorts the specified range of the specified array of objects according
67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the order induced by the specified comparator.  The range to be
67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * sorted extends from index {@code fromIndex}, inclusive, to index
68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be sorted is empty.)  All elements in the range must be
68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>mutually comparable</i> by the specified comparator (that is,
68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * for any elements {@code e1} and {@code e2} in the range).
68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not be reordered as a result of the sort.
68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Implementation note: This implementation is a stable, adaptive,
69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iterative mergesort that requires far fewer than n lg(n) comparisons
69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * when the input array is partially sorted, while offering the
69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * performance of a traditional mergesort when the input array is
69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * randomly ordered.  If the input array is nearly sorted, the
69451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * implementation requires approximately n comparisons.  Temporary
69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * storage requirements vary from a small constant for nearly sorted
69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input arrays to n/2 object references for randomly ordered input
69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation takes equal advantage of ascending and
70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * descending order in its input array, and can take advantage of
70151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * ascending and descending order in different parts of the the same
70251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * input array.  It is well-suited to merging two or more sorted arrays:
70351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * simply concatenate the arrays and sort the resulting array.
70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The implementation was adapted from Tim Peters's list sort for Python
70651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
70751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Sorting and Information Theoretic Complexity", in Proceedings of the
70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * January 1993.
71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be sorted
71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        sorted
71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be sorted
71651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator to determine the order of the array.  A
71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@code null} value indicates that the elements'
71851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
71951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator.
72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         (optional) if the comparator is found to violate the
72351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@link Comparator} contract
72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         {@code toIndex > a.length}
72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> void sort(T[] a, int fromIndex, int toIndex,
72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                Comparator<? super T> c) {
729c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        if (c == null) {
730c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            sort(a, fromIndex, toIndex);
731c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        } else {
732c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            rangeCheck(a.length, fromIndex, toIndex);
733c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            if (LegacyMergeSort.userRequested)
734c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath                legacyMergeSort(a, fromIndex, toIndex, c);
735c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath            else
736c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath                TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0);
737c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath        }
73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /** To be removed in a future release. */
74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,
74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                            Comparator<? super T> c) {
74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] aux = copyOfRange(a, fromIndex, toIndex);
74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c==null)
74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
74851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Src is the source array that starts at index 0
75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Dest is the (possibly larger) array destination with a possible offset
75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * low is the index in dest to start sorting
75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * high is the end index in dest to end sorting
75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * off is the offset into src corresponding to low in dest
75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * To be removed in a future release.
75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void mergeSort(Object[] src,
76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  Object[] dest,
76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  int low, int high, int off,
76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                  Comparator c) {
76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = high - low;
76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Insertion sort on smallest arrays
76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (length < INSERTIONSORT_THRESHOLD) {
76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            for (int i=low; i<high; i++)
76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    swap(dest, j, j-1);
77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Recursively sort halves of dest into src
77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destLow  = low;
77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int destHigh = high;
77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        low  += off;
77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        high += off;
77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int mid = (low + high) >>> 1;
77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, low, mid, -off, c);
78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        mergeSort(dest, src, mid, high, -off, c);
78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // If list is already sorted, just copy from src to dest.  This is an
78351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // optimization that results in faster sorts for nearly ordered lists.
78451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c.compare(src[mid-1], src[mid]) <= 0) {
78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski           System.arraycopy(src, low, dest, destLow, length);
78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski           return;
78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        // Merge sorted halves (now in src) into dest
79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[p++];
79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                dest[i] = src[q++];
79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Checks that {@code fromIndex} and {@code toIndex} are in
80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the range and throws an appropriate exception, if they aren't.
80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void rangeCheck(int length, int fromIndex, int toIndex) {
80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (fromIndex > toIndex) {
80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(
80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (fromIndex < 0) {
80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new ArrayIndexOutOfBoundsException(fromIndex);
80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (toIndex > length) {
81151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new ArrayIndexOutOfBoundsException(toIndex);
81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Searching
81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of longs for the specified value using the
81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(long[])} method) prior to making this call.  If it
82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
82251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(long[] a, long key) {
83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
83951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of longs for the specified value using the
84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(long[], int, int)} method)
84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
85051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
86451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
86651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
86751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(long[] a, int fromIndex, int toIndex,
87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   long key) {
87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
87851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(long[] a, int fromIndex, int toIndex,
88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     long key) {
88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            long midVal = a[mid];
88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
89751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
89851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
89951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of ints for the specified value using the
90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(int[])} method) prior to making this call.  If it
90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
90551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
91651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(int[] a, int key) {
91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
92351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of ints for the specified value using the
92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
92651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(int[], int, int)} method)
92751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
92851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
92951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
93051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
93151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
93751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
94951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
95051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
95151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
95251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
95351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(int[] a, int fromIndex, int toIndex,
95451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   int key) {
95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
96051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     int key) {
96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int midVal = a[mid];
96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
98051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of shorts for the specified value using
98151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.  The array must be sorted
98251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(short[])} method) prior to making this call.  If
98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined.  If the array contains
98451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
98551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
98651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
98851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
99051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(short[] a, short key) {
99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of shorts for the specified value using
100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(short[], int, int)} method)
100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If
100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined.  If the range contains
101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
103351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
103451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(short[] a, int fromIndex, int toIndex,
103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   short key) {
103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
103951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
104051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
104151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(short[] a, int fromIndex, int toIndex,
104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     short key) {
104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
104651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
104751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
104851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            short midVal = a[mid];
104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
105051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
105151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
105251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
105351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
105551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
105651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
105751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
105851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
106151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of chars for the specified value using the
106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(char[])} method) prior to making this call.  If it
106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
107051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
107351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(char[] a, char key) {
108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
108251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
108551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of chars for the specified value using the
108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(char[], int, int)} method)
108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
109551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
109751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
109951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
110951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
111251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(char[] a, int fromIndex, int toIndex,
111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   char key) {
111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(char[] a, int fromIndex, int toIndex,
112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     char key) {
112451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
112751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            char midVal = a[mid];
113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of bytes for the specified value using the
114351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.  The array must be sorted (as
114451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(byte[])} method) prior to making this call.  If it
114551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the array contains
114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
114951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
115851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
115951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(byte[] a, byte key) {
116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
116451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of bytes for the specified value using the
116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * binary search algorithm.
116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted (as
116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by the {@link #sort(byte[], int, int)} method)
117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.  If it
117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is not sorted, the results are undefined.  If the range contains
117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(byte[] a, int fromIndex, int toIndex,
119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   byte key) {
119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     byte key) {
120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            byte midVal = a[mid];
121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of doubles for the specified value using
122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.  The array must be sorted
122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(double[])} method) prior to making this call.
122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.  If the array contains
122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.  This method considers all NaN values to be
122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(double[] a, double key) {
124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of doubles for the specified value using
124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(double[], int, int)} method)
125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call.
125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.  If the range contains
125451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.  This method considers all NaN values to be
125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(double[] a, int fromIndex, int toIndex,
128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   double key) {
128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(double[] a, int fromIndex, int toIndex,
128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     double key) {
128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            double midVal = a[mid];
129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;  // Neither val is NaN, thisVal is smaller
129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1; // Neither val is NaN, thisVal is larger
129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else {
130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                long midBits = Double.doubleToLongBits(midVal);
130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                long keyBits = Double.doubleToLongBits(key);
130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (midBits == keyBits)     // Values are equal
130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    return mid;             // Key found
130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    low = mid + 1;
130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else                        // (0.0, -0.0) or (NaN, !NaN)
130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    high = mid - 1;
130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array of floats for the specified value using
131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm. The array must be sorted
131651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(float[])} method) prior to making this call. If
131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined. If the array contains
131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found. This method considers all NaN values to be
132051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key. Note
133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(float[] a, float key) {
133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array of floats for the specified value using
134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the binary search algorithm.
134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted
134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (as by the {@link #sort(float[], int, int)} method)
134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * prior to making this call. If
134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * it is not sorted, the results are undefined. If the range contains
134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * multiple elements with the specified value, there is no guarantee which
134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found. This method considers all NaN values to be
134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equivalent and equal.
134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key. Note
136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(float[] a, int fromIndex, int toIndex,
137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   float key) {
137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(float[] a, int fromIndex, int toIndex,
137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     float key) {
137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            float midVal = a[mid];
138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (midVal < key)
138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;  // Neither val is NaN, thisVal is smaller
138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (midVal > key)
138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1; // Neither val is NaN, thisVal is larger
139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else {
139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int midBits = Float.floatToIntBits(midVal);
139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                int keyBits = Float.floatToIntBits(key);
139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (midBits == keyBits)     // Values are equal
139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    return mid;             // Key found
139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    low = mid + 1;
139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                else                        // (0.0, -0.0) or (NaN, !NaN)
139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    high = mid - 1;
139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array for the specified object using the binary
140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm. The array must be sorted into ascending order
140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the
140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering}
140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of its elements (as by the
141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[])} method) prior to making this call.
141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.
141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If the array contains elements that are not mutually comparable (for
141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * example, strings and integers), it <i>cannot</i> be sorted according
141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the natural ordering of its elements, hence results are undefined.)
141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains multiple
141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which
141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the search key is not comparable to the
143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array.
143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(Object[] a, Object key) {
143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key);
143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array for the specified object using the binary
143951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.
144051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted into ascending order
144151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the
144251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@linkplain Comparable natural ordering}
144351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of its elements (as by the
144451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], int, int)} method) prior to making this
144551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * call.  If it is not sorted, the results are undefined.
144651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (If the range contains elements that are not mutually comparable (for
144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * example, strings and integers), it <i>cannot</i> be sorted according
144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the natural ordering of its elements, hence results are undefined.)
144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the range contains multiple
145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which
145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one will be found.
145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the search key is not comparable to the
146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array within the specified range.
147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int binarySearch(Object[] a, int fromIndex, int toIndex,
147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                   Object key) {
147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key);
148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     Object key) {
148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Comparable midVal = (Comparable)a[mid];
149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int cmp = midVal.compareTo(key);
149251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
149351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (cmp < 0)
149451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
149551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (cmp > 0)
149651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches the specified array for the specified object using the binary
150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.  The array must be sorted into ascending order
150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the specified comparator (as by the
150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method) prior to making this call.  If it is
150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * not sorted, the results are undefined.
151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains multiple
151151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements equal to the specified object, there is no guarantee which one
151251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will be found.
151351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
151451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
151551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
151651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator by which the array is ordered.  A
151751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        <tt>null</tt> value indicates that the elements'
151851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
151951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array;
152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element greater than the key, or <tt>a.length</tt> if all
152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the array are less than the specified key.  Note
152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the array contains elements that are not
152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator,
152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or the search key is not comparable to the
153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements of the array using this comparator.
153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, 0, a.length, key, c);
153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Searches a range of
153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the specified array for the specified object using the binary
153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * search algorithm.
154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The range must be sorted into ascending order
154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * according to the specified comparator (as by the
154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * {@link #sort(Object[], int, int, Comparator)
154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * sort(T[], int, int, Comparator)}
154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method) prior to making this call.
154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If it is not sorted, the results are undefined.
154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the range contains multiple elements equal to the specified object,
154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * there is no guarantee which one will be found.
154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be searched
155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          searched
155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be searched
155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param key the value to be searched for
155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param c the comparator by which the array is ordered.  A
155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        <tt>null</tt> value indicates that the elements'
155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        {@linkplain Comparable natural ordering} should be used.
155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return index of the search key, if it is contained in the array
155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         within the specified range;
155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>insertion point</i> is defined as the point at which the
156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         key would be inserted into the array: the index of the first
156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         element in the range greater than the key,
156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or <tt>toIndex</tt> if all
156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range are less than the specified key.  Note
156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         that this guarantees that the return value will be &gt;= 0 if
156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         and only if the key is found.
156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ClassCastException if the range contains elements that are not
156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <i>mutually comparable</i> using the specified comparator,
156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         or the search key is not comparable to the
157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         elements in the range using this comparator.
157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex > toIndex}
157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException
157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         if {@code fromIndex < 0 or toIndex > a.length}
157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                       T key, Comparator<? super T> c) {
157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return binarySearch0(a, fromIndex, toIndex, key, c);
158151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
158251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
158351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Like public version, but without range checks.
158451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
158551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                         T key, Comparator<? super T> c) {
158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (c == null) {
158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return binarySearch0(a, fromIndex, toIndex, key);
158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int low = fromIndex;
159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int high = toIndex - 1;
159151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        while (low <= high) {
159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int mid = (low + high) >>> 1;
159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            T midVal = a[mid];
159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int cmp = c.compare(midVal, key);
159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (cmp < 0)
159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                low = mid + 1;
159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else if (cmp > 0)
159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                high = mid - 1;
160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            else
160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return mid; // key found
160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return -(low + 1);  // key not found.
160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Equality Testing
160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of longs are
161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(long[] a, long[] a2) {
162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
162951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
163151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
163251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
163351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
163451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
163551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
163651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
163751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of ints are
163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
164051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
164151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
164351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
164451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
164551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
164651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(int[] a, int[] a2) {
165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of shorts are
166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(short[] a, short a2[]) {
167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of chars are
169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(char[] a, char[] a2) {
170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of bytes are
172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(byte[] a, byte[] a2) {
173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
174951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of booleans are
175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(boolean[] a, boolean[] a2) {
176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a[i] != a2[i])
177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of doubles are
178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (Unlike the <tt>==</tt> operator, this method considers
179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Double#equals(Object)
179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(double[] a, double[] a2) {
180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
181151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of floats are
181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  Two arrays are considered equal if both
182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays contain the same number of elements, and all corresponding pairs
182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of elements in the two arrays are equal.  In other words, two arrays
182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are equal if they contain the same elements in the same order.  Also,
182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * two array references are considered equal if both are <tt>null</tt>.<p>
182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (Unlike the <tt>==</tt> operator, this method considers
182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Float#equals(Object)
183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(float[] a, float[] a2) {
183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++)
184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays of Objects are
185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <i>equal</i> to one another.  The two arrays are considered equal if
185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * both arrays contain the same number of elements, and all corresponding
185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * they contain the same elements in the same order.  Also, two array
186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * references are considered equal if both are <tt>null</tt>.<p>
186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a one array to be tested for equality
186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean equals(Object[] a, Object[] a2) {
186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==a2)
186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a==null || a2==null)
187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a.length;
187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i=0; i<length; i++) {
187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object o1 = a[i];
187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object o2 = a2[i];
187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!(o1==null ? o2==null : o1.equals(o2)))
188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Filling
188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified long value to each element of the specified array
189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of longs.
189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(long[] a, long val) {
189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified long value to each element of the specified
190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of longs.  The range to be filled
190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
191151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(long[] a, int fromIndex, int toIndex, long val) {
191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified int value to each element of the specified array
192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of ints.
192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(int[] a, int val) {
193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified int value to each element of the specified
193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of ints.  The range to be filled
193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
194151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
194251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
194451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
194551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
194951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(int[] a, int fromIndex, int toIndex, int val) {
195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified short value to each element of the specified array
196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of shorts.
196151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(short[] a, short val) {
196651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
196751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
196851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
196951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
197051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
197151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified short value to each element of the specified
197251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of shorts.  The range to be filled
197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(short[] a, int fromIndex, int toIndex, short val) {
198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified char value to each element of the specified array
199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of chars.
199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(char[] a, char val) {
200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified char value to each element of the specified
200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of chars.  The range to be filled
200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
201951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
202051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
202151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
202251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
202351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
202451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
202551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
202651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
202751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
202851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
202951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified byte value to each element of the specified array
203051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of bytes.
203151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(byte[] a, byte val) {
203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
203751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
203851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
204051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified byte value to each element of the specified
204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of bytes.  The range to be filled
204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified boolean value to each element of the specified
206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of booleans.
206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(boolean[] a, boolean val) {
207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified boolean value to each element of the specified
207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of booleans.  The range to be filled
207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
209051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(boolean[] a, int fromIndex, int toIndex,
209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            boolean val) {
209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified double value to each element of the specified
210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of doubles.
210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(double[] a, double val) {
210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified double value to each element of the specified
211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of doubles.  The range to be filled
211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(double[] a, int fromIndex, int toIndex,double val){
212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified float value to each element of the specified array
213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of floats.
213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(float[] a, float val) {
214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified float value to each element of the specified
214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of floats.  The range to be filled
214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(float[] a, int fromIndex, int toIndex, float val) {
216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified Object reference to each element of the specified
217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array of Objects.
217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if the specified value is not of a
217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         runtime type that can be stored in the specified array
217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(Object[] a, Object val) {
217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0, len = a.length; i < len; i++)
218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Assigns the specified Object reference to each element of the specified
218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range of the specified array of Objects.  The range to be filled
218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * extends from index <tt>fromIndex</tt>, inclusive, to index
218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * range to be filled is empty.)
218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array to be filled
219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param fromIndex the index of the first element (inclusive) to be
219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param toIndex the index of the last element (exclusive) to be
219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *        filled with the specified value
219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param val the value to be stored in all elements of the array
219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>toIndex &gt; a.length</tt>
219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if the specified value is not of a
220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         runtime type that can be stored in the specified array
220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        rangeCheck(a.length, fromIndex, toIndex);
220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = fromIndex; i < toIndex; i++)
220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[i] = val;
220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Cloning
220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with nulls (if necessary)
221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>null</tt>.
221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of exactly the same class as the original array.
221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with nulls
222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> T[] copyOf(T[] original, int newLength) {
222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (T[]) copyOf(original, newLength, original.getClass());
223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with nulls (if necessary)
223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>null</tt>.
223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of the class <tt>newType</tt>.
224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newType the class of the copy to be returned
224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with nulls
224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if an element copied from
225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <tt>original</tt> is not of a runtime type that can be stored in
225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     an array of class <tt>newType</tt>
225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] copy = ((Object)newType == (Object)Object[].class)
225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ? (T[]) new Object[newLength]
225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>(byte)0</tt>.
226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static byte[] copyOf(byte[] original, int newLength) {
228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte[] copy = new byte[newLength];
228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>(short)0</tt>.
229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static short[] copyOf(short[] original, int newLength) {
230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        short[] copy = new short[newLength];
230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0</tt>.
231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int[] copyOf(int[] original, int newLength) {
232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int[] copy = new int[newLength];
233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0L</tt>.
234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static long[] copyOf(long[] original, int newLength) {
235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        long[] copy = new long[newLength];
235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with null characters (if necessary)
236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are valid
236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * in both the original array and the copy, the two arrays will contain
236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * identical values.  For any indices that are valid in the copy but not
236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the original, the copy will contain <tt>'\\u000'</tt>.  Such indices
236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * will exist if and only if the specified length is greater than that of
236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the original array.
236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with null characters
237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static char[] copyOf(char[] original, int newLength) {
237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        char[] copy = new char[newLength];
237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0f</tt>.
238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
240051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static float[] copyOf(float[] original, int newLength) {
240151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        float[] copy = new float[newLength];
240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with zeros (if necessary)
240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>0d</tt>.
241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with zeros
241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static double[] copyOf(double[] original, int newLength) {
242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        double[] copy = new double[newLength];
242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
242951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * so the copy has the specified length.  For all indices that are
243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * valid in both the original array and the copy, the two arrays will
243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contain identical values.  For any indices that are valid in the
243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * copy but not the original, the copy will contain <tt>false</tt>.
243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Such indices will exist if and only if the specified length
243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is greater than that of the original array.
243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array to be copied
244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newLength the length of the copy to be returned
244251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a copy of the original array, truncated or padded with false elements
244351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     to obtain the specified length
244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean[] copyOf(boolean[] original, int newLength) {
244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        boolean[] copy = new boolean[newLength];
245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, 0, copy, 0,
245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length, newLength));
245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
245451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
246251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
246451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt> is placed in all elements of the copy whose index is
246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
246751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
246851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>
246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of exactly the same class as the original array.
247051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
247251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
247551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with nulls to obtain the required length
247751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
248151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
248251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
248351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> T[] copyOfRange(T[] original, int from, int to) {
248451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copyOfRange(original, from, to, (Class<T[]>) original.getClass());
248551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
248651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
248751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
248851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
249051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
249151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
249251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
249351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
249451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
249551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
249651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
249751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt> is placed in all elements of the copy whose index is
249851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
249951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
250051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The resulting array is of the class <tt>newType</tt>.
250151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
250251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
250351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
250451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
250551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
250651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param newType the class of the copy to be returned
250751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
250851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with nulls to obtain the required length
250951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
251051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
251151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
251251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
251351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayStoreException if an element copied from
251451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     <tt>original</tt> is not of a runtime type that can be stored in
251551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     an array of class <tt>newType</tt>.
251651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
251751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
251851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
251951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
252051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
252151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
252251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        T[] copy = ((Object)newType == (Object)Object[].class)
252351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            ? (T[]) new Object[newLength]
252451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
252551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
252651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
252751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
252851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
252951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
253051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
253151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
253251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
253351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
253451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
253551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
253651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
253751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
253851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
253951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
254051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
254151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
254251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
254351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
254451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
254551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
254651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
254751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
254851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
254951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
255051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
255151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
255251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
255351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
255451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
255551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
255651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static byte[] copyOfRange(byte[] original, int from, int to) {
255751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
255851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
255951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
256051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        byte[] copy = new byte[newLength];
256151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
256251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
256351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
256451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
256551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
256651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
256751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
256851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
256951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
257051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
257151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
257251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
257351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
257451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
257551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
257651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>(short)0</tt> is placed in all elements of the copy whose index is
257751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
257851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
257951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
258051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
258151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
258251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
258351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
258451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
258551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
258651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
258751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
258851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
258951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
259051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
259151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
259251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static short[] copyOfRange(short[] original, int from, int to) {
259351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
259451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
259551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
259651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        short[] copy = new short[newLength];
259751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
259851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
259951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
260051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
260151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
260251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
260351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
260451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
260551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
260651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
260751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
260851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
260951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
261051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
261151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
261251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0</tt> is placed in all elements of the copy whose index is
261351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
261451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
261551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
261651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
261751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
261851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
261951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
262051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
262151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
262251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
262351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
262451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
262551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
262651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
262751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
262851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int[] copyOfRange(int[] original, int from, int to) {
262951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
263051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
263151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
263251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int[] copy = new int[newLength];
263351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
263451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
263551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
263651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
263751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
263851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
263951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
264051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
264151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
264251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
264351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
264451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
264551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
264651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
264751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
264851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0L</tt> is placed in all elements of the copy whose index is
264951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
265051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
265151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
265251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
265351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
265451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
265551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
265651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
265751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
265851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
265951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
266051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
266151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
266251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
266351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
266451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static long[] copyOfRange(long[] original, int from, int to) {
266551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
266651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
266751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
266851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        long[] copy = new long[newLength];
266951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
267051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
267151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
267251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
267351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
267451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
267551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
267651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
267751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
267851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
267951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
268051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
268151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
268251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
268351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
268451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
268551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
268651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
268751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
268851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
268951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
269051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
269151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
269251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
269351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with null characters to obtain the required length
269451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
269551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
269651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
269751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
269851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
269951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
270051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static char[] copyOfRange(char[] original, int from, int to) {
270151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
270251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
270351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
270451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        char[] copy = new char[newLength];
270551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
270651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
270751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
270851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
270951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
271051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
271151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
271251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
271351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
271451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
271551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
271651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
271751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
271851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
271951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
272051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0f</tt> is placed in all elements of the copy whose index is
272151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
272251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
272351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
272451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
272551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
272651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
272751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
272851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
272951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
273051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
273151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
273251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
273351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
273451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
273551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
273651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static float[] copyOfRange(float[] original, int from, int to) {
273751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
273851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
273951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
274051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        float[] copy = new float[newLength];
274151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
274251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
274351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
274451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
274551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
274651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
274751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
274851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
274951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
275051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
275151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
275251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
275351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
275451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
275551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
275651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>0d</tt> is placed in all elements of the copy whose index is
275751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
275851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
275951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
276051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
276151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
276251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
276351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
276451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
276551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with zeros to obtain the required length
276651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
276751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
276851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
276951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
277051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
277151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
277251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static double[] copyOfRange(double[] original, int from, int to) {
277351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
277451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
277551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
277651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        double[] copy = new double[newLength];
277751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
277851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
277951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
278051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
278151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
278251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
278351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Copies the specified range of the specified array into a new array.
278451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The initial index of the range (<tt>from</tt>) must lie between zero
278551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * and <tt>original.length</tt>, inclusive.  The value at
278651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>original[from]</tt> is placed into the initial element of the copy
278751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
278851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Values from subsequent elements in the original array are placed into
278951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * subsequent elements in the copy.  The final index of the range
279051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
279151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * may be greater than <tt>original.length</tt>, in which case
279251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>false</tt> is placed in all elements of the copy whose index is
279351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * greater than or equal to <tt>original.length - from</tt>.  The length
279451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of the returned array will be <tt>to - from</tt>.
279551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
279651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param original the array from which a range is to be copied
279751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param from the initial index of the range to be copied, inclusive
279851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param to the final index of the range to be copied, exclusive.
279951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     (This index may lie outside the array.)
280051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a new array containing the specified range from the original array,
280151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     truncated or padded with false elements to obtain the required length
280251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
280351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     or {@code from > original.length}
280451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
280551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NullPointerException if <tt>original</tt> is null
280651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.6
280751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
280851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean[] copyOfRange(boolean[] original, int from, int to) {
280951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int newLength = to - from;
281051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (newLength < 0)
281151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException(from + " > " + to);
281251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        boolean[] copy = new boolean[newLength];
281351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        System.arraycopy(original, from, copy, 0,
281451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                         Math.min(original.length - from, newLength));
281551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return copy;
281651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
281751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
281851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    // Misc
281951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
282051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
282151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a fixed-size list backed by the specified array.  (Changes to
282251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the returned list "write through" to the array.)  This method acts
282351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as bridge between array-based and collection-based APIs, in
282451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * combination with {@link Collection#toArray}.  The returned list is
282551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * serializable and implements {@link RandomAccess}.
282651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
282751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This method also provides a convenient way to create a fixed-size
282851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * list initialized to contain several elements:
282951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <pre>
283051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
283151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </pre>
283251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
283351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array by which the list will be backed
283451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a list view of the specified array
283551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
283651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    @SafeVarargs
283751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static <T> List<T> asList(T... a) {
283851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return new ArrayList<>(a);
283951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
284051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
284151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
284251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @serial include
284351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
284451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static class ArrayList<E> extends AbstractList<E>
284551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        implements RandomAccess, java.io.Serializable
284651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    {
284751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        private static final long serialVersionUID = -2764017481108945198L;
284851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        private final E[] a;
284951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
285051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        ArrayList(E[] array) {
28514c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath            a = Objects.requireNonNull(array);
285251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
285351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28544c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
285551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public int size() {
285651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a.length;
285751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
285851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28594c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
286051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public Object[] toArray() {
286151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a.clone();
286251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
286351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
28654c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @SuppressWarnings("unchecked")
286651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public <T> T[] toArray(T[] a) {
286751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int size = size();
286851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a.length < size)
286951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return Arrays.copyOf(this.a, size,
287051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     (Class<? extends T[]>) a.getClass());
287151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            System.arraycopy(this.a, 0, a, 0, size);
287251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (a.length > size)
287351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                a[size] = null;
287451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a;
287551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
287651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
287851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public E get(int index) {
287951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return a[index];
288051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
288151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
288351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public E set(int index, E element) {
288451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            E oldValue = a[index];
288551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            a[index] = element;
288651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return oldValue;
288751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
288851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
289051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public int indexOf(Object o) {
289151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (o==null) {
289251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int i=0; i<a.length; i++)
289351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (a[i]==null)
289451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        return i;
289551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            } else {
289651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                for (int i=0; i<a.length; i++)
289751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (o.equals(a[i]))
289851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        return i;
289951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
290051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return -1;
290151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
290251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
29034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
290451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        public boolean contains(Object o) {
290551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return indexOf(o) != -1;
290651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
290702f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak
290802f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak        @Override
290902f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak        public void forEach(Consumer<? super E> action) {
291002f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak            Objects.requireNonNull(action);
291102f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak            for (E e : a) {
291202f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak                action.accept(e);
291302f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak            }
291402f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak        }
29154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
29164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        @Override
29174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        public Spliterator<E> spliterator() {
29184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath            return Spliterators.spliterator(a, Spliterator.ORDERED);
29194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        }
292051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
292151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
292251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
292351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
292451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
292551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
292651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
292751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
292851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
292951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
293051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Long}
293151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
293251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
293351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
293451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
293551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
293651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
293751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
293851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(long a[]) {
293951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
294051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
294151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
294251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
294351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (long element : a) {
294451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int elementHash = (int)(element ^ (element >>> 32));
294551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + elementHash;
294651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
294751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
294851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
294951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
295051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
295151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
295251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
295351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
295451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
295551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
295651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
295751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
295851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
295951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Integer}
296051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
296151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
296251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
296351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
296451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
296551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
296651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
296751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(int a[]) {
296851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
296951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
297051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
297151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
297251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int element : a)
297351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
297451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
297551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
297651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
297751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
297851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
297951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
298051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
298151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
298251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
298351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
298451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
298551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
298651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Short}
298751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
298851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
298951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
299051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
299151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
299251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
299351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
299451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(short a[]) {
299551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
299651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
299751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
299851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
299951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (short element : a)
300051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
300151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
300251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
300351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
300451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
300551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
300651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
300751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
300851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
300951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
301051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
301151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
301251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
301351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Character}
301451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
301551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
301651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
301751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
301851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
301951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
302051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
302151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(char a[]) {
302251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
302351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
302451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
302551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
302651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (char element : a)
302751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
302851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
302951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
303051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
303151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
303251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
303351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
303451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
303551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
303651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
303751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
303851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
303951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
304051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Byte}
304151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
304251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
304351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
304451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
304551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
304651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
304751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
304851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(byte a[]) {
304951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
305051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
305151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
305251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
305351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (byte element : a)
305451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + element;
305551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
305651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
305751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
305851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
305951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
306051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
306151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
306251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
306351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
306451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
306551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
306651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
306751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Boolean}
306851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
306951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
307051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
307151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
307251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
307351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
307451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
307551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(boolean a[]) {
307651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
307751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
307851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
307951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
308051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (boolean element : a)
308151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (element ? 1231 : 1237);
308251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
308351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
308451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
308551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
308651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
308751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
308851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
308951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
309051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
309151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
309251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
309351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
309451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Float}
309551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
309651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
309751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
309851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
309951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
310051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
310151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
310251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(float a[]) {
310351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
310451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
310551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
310651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
310751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (float element : a)
310851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + Float.floatToIntBits(element);
310951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
311051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
311151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
311251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
311351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
311451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.
311551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
311651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
311751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
311851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
311951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is the same value that would be
312051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
312151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method on a {@link List} containing a sequence of {@link Double}
312251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * instances representing the elements of <tt>a</tt> in the same order.
312351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
312451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
312551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose hash value to compute
312651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
312751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
312851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
312951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(double a[]) {
313051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
313151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
313251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
313351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
313451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (double element : a) {
313551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            long bits = Double.doubleToLongBits(element);
313651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (int)(bits ^ (bits >>> 32));
313751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
313851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
313951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
314051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
314151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
314251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the contents of the specified array.  If
314351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the array contains other arrays as elements, the hash code is based on
314451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * their identities rather than their contents.  It is therefore
314551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * acceptable to invoke this method on an array that contains itself as an
314651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * element,  either directly or indirectly through one or more levels of
314751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
314851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
314951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
315051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.equals(a, b)</tt>, it is also the case that
315151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
315251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
315351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is equal to the value that would
315451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
315551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>, in which case <tt>0</tt> is returned.
315651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
315751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose content-based hash code to compute
315851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a content-based hash code for <tt>a</tt>
315951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #deepHashCode(Object[])
316051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
316151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
316251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int hashCode(Object a[]) {
316351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
316451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
316551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
316651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
316751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
316851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (Object element : a)
316951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + (element == null ? 0 : element.hashCode());
317051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
317151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
317251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
317351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
317451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
317551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a hash code based on the "deep contents" of the specified
317651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array.  If the array contains other arrays as elements, the
317751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * hash code is based on their contents and so on, ad infinitum.
317851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * It is therefore unacceptable to invoke this method on an array that
317951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * contains itself as an element, either directly or indirectly through
318051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * one or more levels of arrays.  The behavior of such an invocation is
318151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * undefined.
318251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
318351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
318451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
318551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
318651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
318751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The computation of the value returned by this method is similar to
318851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * that of the value returned by {@link List#hashCode()} on a list
318951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * containing the same elements as <tt>a</tt> in the same order, with one
319051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
319151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
319251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
319351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * if <tt>e</tt> is an array of a primitive type, or as by calling
319451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
319551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * of a reference type.  If <tt>a</tt> is <tt>null</tt>, this method
319651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * returns 0.
319751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
319851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose deep-content-based hash code to compute
319951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a deep-content-based hash code for <tt>a</tt>
320051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #hashCode(Object[])
320151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
320251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
320351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static int deepHashCode(Object a[]) {
320451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
320551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return 0;
320651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
320751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int result = 1;
320851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
320951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (Object element : a) {
321051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            int elementHash = 0;
3211669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            if (element != null) {
3212669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                Class<?> cl = element.getClass().getComponentType();
3213669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                if (cl == null)
3214669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = element.hashCode();
3215669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (element instanceof Object[])
3216669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = deepHashCode((Object[]) element);
3217669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == byte.class)
3218669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((byte[]) element);
3219669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == short.class)
3220669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((short[]) element);
3221669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == int.class)
3222669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((int[]) element);
3223669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == long.class)
3224669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((long[]) element);
3225669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == char.class)
3226669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((char[]) element);
3227669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == float.class)
3228669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((float[]) element);
3229669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == double.class)
3230669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((double[]) element);
3231669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else if (cl == boolean.class)
3232669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = hashCode((boolean[]) element);
3233669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                else
3234669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak                    elementHash = element.hashCode();
3235669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            }
323651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            result = 31 * result + elementHash;
323751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
323851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
323951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return result;
324051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
324151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
324251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
324351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns <tt>true</tt> if the two specified arrays are <i>deeply
324451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
324551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method, this method is appropriate for use with nested arrays of
324651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arbitrary depth.
324751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
324851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Two array references are considered deeply equal if both
324951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are <tt>null</tt>, or if they refer to arrays that contain the same
325051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * number of elements and all corresponding pairs of elements in the two
325151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays are deeply equal.
325251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
325351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
325451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * deeply equal if any of the following conditions hold:
325551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <ul>
325651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
325751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
325851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
325951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         type, and the appropriate overloading of
326051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>Arrays.equals(e1, e2)</tt> would return true.
326151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1 == e2</tt>
326251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *    <li> <tt>e1.equals(e2)</tt> would return true.
326351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * </ul>
326451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Note that this definition permits <tt>null</tt> elements at any depth.
326551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
326651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If either of the specified arrays contain themselves as elements
326751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * either directly or indirectly through one or more levels of arrays,
326851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the behavior of this method is undefined.
326951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
327051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a1 one array to be tested for equality
327151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a2 the other array to be tested for equality
327251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if the two arrays are equal
327351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #equals(Object[],Object[])
327451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see Objects#deepEquals(Object, Object)
327551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
327651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
327751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static boolean deepEquals(Object[] a1, Object[] a2) {
327851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a1 == a2)
327951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return true;
328051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a1 == null || a2==null)
328151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
328251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int length = a1.length;
328351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a2.length != length)
328451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;
328551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
328651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; i < length; i++) {
328751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object e1 = a1[i];
328851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object e2 = a2[i];
328951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
329051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (e1 == e2)
329151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                continue;
3292669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            if (e1 == null || e2 == null)
329351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
329451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
329551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            // Figure out whether the two elements are equal
329651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            boolean eq = deepEquals0(e1, e2);
329751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
329851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (!eq)
329951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return false;
330051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
330151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
330251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
330351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
330451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    static boolean deepEquals0(Object e1, Object e2) {
3305669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        Class<?> cl1 = e1.getClass().getComponentType();
3306669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        Class<?> cl2 = e2.getClass().getComponentType();
3307669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak
3308669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        if (cl1 != cl2) {
3309669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return false;
3310669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        }
3311669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        if (e1 instanceof Object[])
3312669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return deepEquals ((Object[]) e1, (Object[]) e2);
3313669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == byte.class)
3314669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((byte[]) e1, (byte[]) e2);
3315669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == short.class)
3316669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((short[]) e1, (short[]) e2);
3317669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == int.class)
3318669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((int[]) e1, (int[]) e2);
3319669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == long.class)
3320669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((long[]) e1, (long[]) e2);
3321669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == char.class)
3322669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((char[]) e1, (char[]) e2);
3323669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == float.class)
3324669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((float[]) e1, (float[]) e2);
3325669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == double.class)
3326669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((double[]) e1, (double[]) e2);
3327669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak        else if (cl1 == boolean.class)
3328669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return equals((boolean[]) e1, (boolean[]) e2);
332951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        else
3330669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak            return e1.equals(e2);
333151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
333251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
333351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
333451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
333551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
333651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
333751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
333851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
333951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(long)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
334051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
334151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
334251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
334351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
334451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
334551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
334651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(long[] a) {
334751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
334851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
334951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
335051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
335151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
335251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
335351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
335451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
335551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
335651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
335751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
335851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
335951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
336051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
336151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
336251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
336351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
336451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
336551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
336651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
336751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
336851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
336951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
337051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>null</tt>.
337151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
337251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
337351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
337451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
337551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
337651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(int[] a) {
337751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
337851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
337951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
338051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
338151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
338251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
338351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
338451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
338551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
338651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
338751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
338851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
338951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
339051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
339151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
339251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
339351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
339451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
339551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
339651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
339751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
339851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
339951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(short)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
340051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
340151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
340251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
340351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
340451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
340551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
340651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(short[] a) {
340751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
340851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
340951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
341051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
341151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
341251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
341351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
341451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
341551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
341651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
341751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
341851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
341951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
342051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
342151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
342251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
342351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
342451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
342551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
342651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
342751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
342851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
342951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(char)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
343051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
343151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
343251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
343351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
343451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
343551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
343651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(char[] a) {
343751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
343851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
343951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
344051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
344151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
344251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
344351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
344451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
344551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
344651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
344751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
344851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
344951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
345051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
345151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
345251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
345351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
345451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
345551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
345651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements
345751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * are separated by the characters <tt>", "</tt> (a comma followed
345851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by a space).  Elements are converted to strings as by
345951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(byte)</tt>.  Returns <tt>"null"</tt> if
346051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>a</tt> is <tt>null</tt>.
346151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
346251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
346351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
346451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
346551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
346651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(byte[] a) {
346751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
346851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
346951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
347051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
347151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
347251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
347351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
347451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
347551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
347651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
347751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
347851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
347951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
348051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
348151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
348251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
348351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
348451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
348551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
348651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
348751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
348851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
348951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(boolean)</tt>.  Returns <tt>"null"</tt> if
349051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>a</tt> is <tt>null</tt>.
349151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
349251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
349351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
349451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
349551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
349651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(boolean[] a) {
349751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
349851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
349951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
350051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
350151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
350251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
350351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
350451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
350551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
350651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
350751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
350851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
350951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
351051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
351151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
351251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
351351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
351451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
351551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
351651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
351751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
351851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
351951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(float)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
352051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
352151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
352251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
352351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
352451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
352551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
352651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(float[] a) {
352751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
352851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
352951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
353051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
353151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
353251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
353351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
353451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
353551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
353651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
353751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
353851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
353951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
354051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
354151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
354251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
354351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
354451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
354551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
354651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * The string representation consists of a list of the array's elements,
354751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
354851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * separated by the characters <tt>", "</tt> (a comma followed by a
354951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * space).  Elements are converted to strings as by
355051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(double)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
355151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
355251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
355351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
355451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
355551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
355651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
355751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(double[] a) {
355851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
355951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
356051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
356151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
356251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
356351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
356451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
356551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
356651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
356751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(a[i]);
356851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
356951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
357051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
357151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
357251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
357351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
357451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
357551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the contents of the specified array.
357651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * If the array contains other arrays as elements, they are converted to
357751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * strings by the {@link Object#toString} method inherited from
357851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Object</tt>, which describes their <i>identities</i> rather than
357951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * their contents.
358051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
358151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The value returned by this method is equal to the value that would
358251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
358351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
358451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
358551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
358651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
358751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #deepToString(Object[])
358851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
358951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
359051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String toString(Object[] a) {
359151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
359251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
359351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
359451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
359551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1)
359651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "[]";
359751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
359851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder b = new StringBuilder();
359951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        b.append('[');
360051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
360151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(String.valueOf(a[i]));
360251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
360351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                return b.append(']').toString();
360451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            b.append(", ");
360551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
360651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
360751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
360851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
360951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string representation of the "deep contents" of the specified
361051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * array.  If the array contains other arrays as elements, the string
361151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * representation contains their contents and so on.  This method is
361251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * designed for converting multidimensional arrays to strings.
361351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
361451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>The string representation consists of a list of the array's
361551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent
361651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * elements are separated by the characters <tt>", "</tt> (a comma
361751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * followed by a space).  Elements are converted to strings as by
361851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>String.valueOf(Object)</tt>, unless they are themselves
361951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * arrays.
362051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
362151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>If an element <tt>e</tt> is an array of a primitive type, it is
362251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * converted to a string as by invoking the appropriate overloading of
362351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>Arrays.toString(e)</tt>.  If an element <tt>e</tt> is an array of a
362451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * reference type, it is converted to a string as by invoking
362551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * this method recursively.
362651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
362751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>To avoid infinite recursion, if the specified array contains itself
362851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * as an element, or contains an indirect reference to itself through one
362951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * or more levels of arrays, the self-reference is converted to the string
363051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <tt>"[...]"</tt>.  For example, an array containing only a reference
363151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to itself would be rendered as <tt>"[[...]]"</tt>.
363251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
363351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p>This method returns <tt>"null"</tt> if the specified array
363451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is <tt>null</tt>.
363551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
363651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param a the array whose string representation to return
363751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return a string representation of <tt>a</tt>
363851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @see #toString(Object[])
363951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.5
364051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
364151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public static String deepToString(Object[] a) {
364251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null)
364351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return "null";
364451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
364551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int bufLen = 20 * a.length;
364651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a.length != 0 && bufLen <= 0)
364751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            bufLen = Integer.MAX_VALUE;
364851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        StringBuilder buf = new StringBuilder(bufLen);
364951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        deepToString(a, buf, new HashSet<Object[]>());
365051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return buf.toString();
365151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
365251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
365351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private static void deepToString(Object[] a, StringBuilder buf,
365451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                                     Set<Object[]> dejaVu) {
365551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (a == null) {
365651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append("null");
365751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
365851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
365951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        int iMax = a.length - 1;
366051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (iMax == -1) {
366151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append("[]");
366251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return;
366351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
366451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
366551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        dejaVu.add(a);
366651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        buf.append('[');
366751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        for (int i = 0; ; i++) {
366851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
366951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            Object element = a[i];
367051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (element == null) {
367151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                buf.append("null");
367251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            } else {
367351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                Class eClass = element.getClass();
367451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
367551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                if (eClass.isArray()) {
367651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    if (eClass == byte[].class)
367751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((byte[]) element));
367851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == short[].class)
367951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((short[]) element));
368051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == int[].class)
368151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((int[]) element));
368251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == long[].class)
368351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((long[]) element));
368451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == char[].class)
368551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((char[]) element));
368651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == float[].class)
368751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((float[]) element));
368851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == double[].class)
368951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((double[]) element));
369051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else if (eClass == boolean[].class)
369151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        buf.append(toString((boolean[]) element));
369251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    else { // element is an array of object references
369351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        if (dejaVu.contains(element))
369451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            buf.append("[...]");
369551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                        else
369651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                            deepToString((Object[])element, buf, dejaVu);
369751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    }
369851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                } else {  // element is non-null and not an array
369951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                    buf.append(element.toString());
370051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                }
370151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            }
370251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            if (i == iMax)
370351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                break;
370451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            buf.append(", ");
370551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        }
370651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        buf.append(']');
370751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        dejaVu.remove(a);
370851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
3709983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski
3710983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski    /**
3711983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * Checks that the range described by {@code offset} and {@code count} doesn't exceed
3712983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * {@code arrayLength}.
3713983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     *
3714983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * Android changed.
3715983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     * @hide
3716983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski     */
3717983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski    public static void checkOffsetAndCount(int arrayLength, int offset, int count) {
3718983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski        if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) {
3719983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski            throw new ArrayIndexOutOfBoundsException(arrayLength, offset,
3720983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski                    count);
3721983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski        }
3722983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski    }
37234c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
37244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
37254c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
37264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator} covering all of the specified array.
37274c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37284c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
37294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
37304c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
37314c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37324c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param <T> type of elements
37334c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
37344c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
37354c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
37364c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
37374c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static <T> Spliterator<T> spliterator(T[] array) {
37384c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
37394c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
37404c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
37414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
37424c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
37434c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator} covering the specified range of the
37444c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * specified array.
37454c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37464c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
37474c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
37484c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
37494c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37504c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param <T> type of elements
37514c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
37524c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
37534c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
37544c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
37554c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
37564c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
37574c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
37584c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
37594c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
37604c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
37614c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
37624c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
37634c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
37644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
37654c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
37664c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
37674c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfInt} covering all of the specified array.
37684c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37694c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
37704c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
37714c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
37724c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37734c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
37744c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
37754c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
37764c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
37774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfInt spliterator(int[] array) {
37784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
37794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
37804c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
37814c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
37824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
37834c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfInt} covering the specified range of the
37844c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * specified array.
37854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37864c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
37874c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
37884c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
37894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
37904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
37914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
37924c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
37934c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
37944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
37954c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
37964c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
37974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
37984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
37994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
38004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
38014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
38024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
38034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
38044c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
38054c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
38064c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfLong} covering all of the specified array.
38074c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38084c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
38094c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
38104c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
38114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
38134c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return the spliterator for the array elements
38144c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
38154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
38164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfLong spliterator(long[] array) {
38174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
38184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
38194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
38204c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
38214c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
38224c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfLong} covering the specified range of the
38234c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * specified array.
38244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38254c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
38264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
38274c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
38284c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
38304c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
38314c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
38324c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
38334c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
38344c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
38354c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
38364c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
38374c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
38384c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
38394c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
38404c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
38414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
38424c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
38434c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
38444c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
38454c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfDouble} covering all of the specified
38464c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * array.
38474c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38484c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
38494c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
38504c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
38514c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38524c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
38534c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
38544c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
38554c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
38564c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfDouble spliterator(double[] array) {
38574c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array,
38584c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
38594c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
38604c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath
38614c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    /**
38624c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * Returns a {@link Spliterator.OfDouble} covering the specified range of
38634c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * the specified array.
38644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38654c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * <p>The spliterator reports {@link Spliterator#SIZED},
38664c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
38674c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * {@link Spliterator#IMMUTABLE}.
38684c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *
38694c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param array the array, assumed to be unmodified during use
38704c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param startInclusive the first index to cover, inclusive
38714c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @param endExclusive index immediately past the last index to cover
38724c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @return a spliterator for the array elements
38734c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
38744c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         negative, {@code endExclusive} is less than
38754c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         {@code startInclusive}, or {@code endExclusive} is greater than
38764c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     *         the array size
38774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     * @since 1.8
38784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath     */
38794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
38804c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath        return Spliterators.spliterator(array, startInclusive, endExclusive,
38814c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath                                        Spliterator.ORDERED | Spliterator.IMMUTABLE);
38824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath    }
388351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
3884