9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import jdk.internal.util.ArraysSupport;
29 import jdk.internal.vm.annotation.IntrinsicCandidate;
30
31 import java.io.Serializable;
32 import java.lang.reflect.Array;
33 import java.util.concurrent.ForkJoinPool;
34 import java.util.function.BinaryOperator;
35 import java.util.function.Consumer;
36 import java.util.function.DoubleBinaryOperator;
37 import java.util.function.IntBinaryOperator;
38 import java.util.function.IntFunction;
39 import java.util.function.IntToDoubleFunction;
40 import java.util.function.IntToLongFunction;
41 import java.util.function.IntUnaryOperator;
42 import java.util.function.LongBinaryOperator;
43 import java.util.function.UnaryOperator;
44 import java.util.stream.DoubleStream;
45 import java.util.stream.IntStream;
46 import java.util.stream.LongStream;
47 import java.util.stream.Stream;
48 import java.util.stream.StreamSupport;
3487 * Such indices will exist if and only if the specified length
3488 * is greater than that of the original array.
3489 * The resulting array is of the class {@code newType}.
3490 *
3491 * @param <T> the class of the objects in the returned array
3492 * @param <U> the class of the objects in the original array
3493 * @param original the array to be copied
3494 * @param newLength the length of the copy to be returned
3495 * @param newType the class of the copy to be returned
3496 * @return a copy of the original array, truncated or padded with nulls
3497 * to obtain the specified length
3498 * @throws NegativeArraySizeException if {@code newLength} is negative
3499 * @throws NullPointerException if {@code original} is null
3500 * @throws ArrayStoreException if an element copied from
3501 * {@code original} is not of a runtime type that can be stored in
3502 * an array of class {@code newType}
3503 * @since 1.6
3504 */
3505 @IntrinsicCandidate
3506 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
3507 @SuppressWarnings("unchecked")
3508 T[] copy = ((Object)newType == (Object)Object[].class)
3509 ? (T[]) new Object[newLength]
3510 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3511 System.arraycopy(original, 0, copy, 0,
3512 Math.min(original.length, newLength));
3513 return copy;
3514 }
3515
3516 /**
3517 * Copies the specified array, truncating or padding with zeros (if necessary)
3518 * so the copy has the specified length. For all indices that are
3519 * valid in both the original array and the copy, the two arrays will
3520 * contain identical values. For any indices that are valid in the
3521 * copy but not the original, the copy will contain {@code (byte)0}.
3522 * Such indices will exist if and only if the specified length
3523 * is greater than that of the original array.
3524 *
3525 * @param original the array to be copied
3526 * @param newLength the length of the copy to be returned
3527 * @return a copy of the original array, truncated or padded with zeros
3528 * to obtain the specified length
3529 * @throws NegativeArraySizeException if {@code newLength} is negative
3530 * @throws NullPointerException if {@code original} is null
3531 * @since 1.6
3532 */
3786 * @param to the final index of the range to be copied, exclusive.
3787 * (This index may lie outside the array.)
3788 * @param newType the class of the copy to be returned
3789 * @return a new array containing the specified range from the original array,
3790 * truncated or padded with nulls to obtain the required length
3791 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3792 * or {@code from > original.length}
3793 * @throws IllegalArgumentException if {@code from > to}
3794 * @throws NullPointerException if {@code original} is null
3795 * @throws ArrayStoreException if an element copied from
3796 * {@code original} is not of a runtime type that can be stored in
3797 * an array of class {@code newType}.
3798 * @since 1.6
3799 */
3800 @IntrinsicCandidate
3801 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3802 int newLength = to - from;
3803 if (newLength < 0) {
3804 throw new IllegalArgumentException(from + " > " + to);
3805 }
3806 @SuppressWarnings("unchecked")
3807 T[] copy = ((Object)newType == (Object)Object[].class)
3808 ? (T[]) new Object[newLength]
3809 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3810 System.arraycopy(original, from, copy, 0,
3811 Math.min(original.length - from, newLength));
3812 return copy;
3813 }
3814
3815 /**
3816 * Copies the specified range of the specified array into a new array.
3817 * The initial index of the range ({@code from}) must lie between zero
3818 * and {@code original.length}, inclusive. The value at
3819 * {@code original[from]} is placed into the initial element of the copy
3820 * (unless {@code from == original.length} or {@code from == to}).
3821 * Values from subsequent elements in the original array are placed into
3822 * subsequent elements in the copy. The final index of the range
3823 * ({@code to}), which must be greater than or equal to {@code from},
3824 * may be greater than {@code original.length}, in which case
3825 * {@code (byte)0} is placed in all elements of the copy whose index is
3826 * greater than or equal to {@code original.length - from}. The length
3827 * of the returned array will be {@code to - from}.
3828 *
3829 * @param original the array from which a range is to be copied
3830 * @param from the initial index of the range to be copied, inclusive
3831 * @param to the final index of the range to be copied, exclusive.
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import jdk.internal.util.ArraysSupport;
29 import jdk.internal.value.ValueClass;
30 import jdk.internal.vm.annotation.IntrinsicCandidate;
31
32 import java.io.Serializable;
33 import java.lang.reflect.Array;
34 import java.util.concurrent.ForkJoinPool;
35 import java.util.function.BinaryOperator;
36 import java.util.function.Consumer;
37 import java.util.function.DoubleBinaryOperator;
38 import java.util.function.IntBinaryOperator;
39 import java.util.function.IntFunction;
40 import java.util.function.IntToDoubleFunction;
41 import java.util.function.IntToLongFunction;
42 import java.util.function.IntUnaryOperator;
43 import java.util.function.LongBinaryOperator;
44 import java.util.function.UnaryOperator;
45 import java.util.stream.DoubleStream;
46 import java.util.stream.IntStream;
47 import java.util.stream.LongStream;
48 import java.util.stream.Stream;
49 import java.util.stream.StreamSupport;
3488 * Such indices will exist if and only if the specified length
3489 * is greater than that of the original array.
3490 * The resulting array is of the class {@code newType}.
3491 *
3492 * @param <T> the class of the objects in the returned array
3493 * @param <U> the class of the objects in the original array
3494 * @param original the array to be copied
3495 * @param newLength the length of the copy to be returned
3496 * @param newType the class of the copy to be returned
3497 * @return a copy of the original array, truncated or padded with nulls
3498 * to obtain the specified length
3499 * @throws NegativeArraySizeException if {@code newLength} is negative
3500 * @throws NullPointerException if {@code original} is null
3501 * @throws ArrayStoreException if an element copied from
3502 * {@code original} is not of a runtime type that can be stored in
3503 * an array of class {@code newType}
3504 * @since 1.6
3505 */
3506 @IntrinsicCandidate
3507 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
3508 Class<?> componentType = newType.getComponentType();
3509 Object tmp = null;
3510 if (original.getClass() == newType && componentType.isValue()) {
3511 tmp = ValueClass.copyOfSpecialArray((Object[])original, 0, newLength);
3512 } else {
3513 tmp = ((Object)newType == (Object)Object[].class)
3514 ? new Object[newLength]
3515 : Array.newInstance(componentType, newLength);
3516 System.arraycopy(original, 0, tmp, 0,
3517 Math.min(original.length, newLength));
3518 }
3519 @SuppressWarnings("unchecked")
3520 T[] copy = (T[])tmp;
3521 return copy;
3522 }
3523
3524 /**
3525 * Copies the specified array, truncating or padding with zeros (if necessary)
3526 * so the copy has the specified length. For all indices that are
3527 * valid in both the original array and the copy, the two arrays will
3528 * contain identical values. For any indices that are valid in the
3529 * copy but not the original, the copy will contain {@code (byte)0}.
3530 * Such indices will exist if and only if the specified length
3531 * is greater than that of the original array.
3532 *
3533 * @param original the array to be copied
3534 * @param newLength the length of the copy to be returned
3535 * @return a copy of the original array, truncated or padded with zeros
3536 * to obtain the specified length
3537 * @throws NegativeArraySizeException if {@code newLength} is negative
3538 * @throws NullPointerException if {@code original} is null
3539 * @since 1.6
3540 */
3794 * @param to the final index of the range to be copied, exclusive.
3795 * (This index may lie outside the array.)
3796 * @param newType the class of the copy to be returned
3797 * @return a new array containing the specified range from the original array,
3798 * truncated or padded with nulls to obtain the required length
3799 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3800 * or {@code from > original.length}
3801 * @throws IllegalArgumentException if {@code from > to}
3802 * @throws NullPointerException if {@code original} is null
3803 * @throws ArrayStoreException if an element copied from
3804 * {@code original} is not of a runtime type that can be stored in
3805 * an array of class {@code newType}.
3806 * @since 1.6
3807 */
3808 @IntrinsicCandidate
3809 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3810 int newLength = to - from;
3811 if (newLength < 0) {
3812 throw new IllegalArgumentException(from + " > " + to);
3813 }
3814 Class<?> componentType = newType.getComponentType();
3815 Object tmp = null;
3816 if (original.getClass() == newType && componentType.isValue()) {
3817 tmp = ValueClass.copyOfSpecialArray((Object[])original, from, to);
3818 } else {
3819 tmp = ((Object)newType == (Object)Object[].class)
3820 ? new Object[newLength]
3821 : Array.newInstance(componentType, newLength);
3822 System.arraycopy(original, from, tmp, 0,
3823 Math.min(original.length - from, newLength));
3824 }
3825 @SuppressWarnings("unchecked")
3826 T[] copy = (T[])tmp;
3827 return copy;
3828 }
3829
3830 /**
3831 * Copies the specified range of the specified array into a new array.
3832 * The initial index of the range ({@code from}) must lie between zero
3833 * and {@code original.length}, inclusive. The value at
3834 * {@code original[from]} is placed into the initial element of the copy
3835 * (unless {@code from == original.length} or {@code from == to}).
3836 * Values from subsequent elements in the original array are placed into
3837 * subsequent elements in the copy. The final index of the range
3838 * ({@code to}), which must be greater than or equal to {@code from},
3839 * may be greater than {@code original.length}, in which case
3840 * {@code (byte)0} is placed in all elements of the copy whose index is
3841 * greater than or equal to {@code original.length - from}. The length
3842 * of the returned array will be {@code to - from}.
3843 *
3844 * @param original the array from which a range is to be copied
3845 * @param from the initial index of the range to be copied, inclusive
3846 * @param to the final index of the range to be copied, exclusive.
|