/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.integralblue.httpresponsecache.compat.java.util; import java.lang.reflect.Array; /** * {@code Arrays} contains static methods which operate on arrays. * * @since 1.2 */ public class Arrays { /** * Checks that the range described by {@code offset} and {@code count} doesn't exceed * {@code arrayLength}. * * @hide */ public static void checkOffsetAndCount(int arrayLength, int offset, int count) { if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) { throw new com.integralblue.httpresponsecache.compat.java.lang.ArrayIndexOutOfBoundsException(arrayLength, offset, count); } } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code false}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static boolean[] copyOfRange(boolean[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); boolean[] result = new boolean[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code (byte) 0}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static byte[] copyOfRange(byte[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); byte[] result = new byte[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code '\\u0000'}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static char[] copyOfRange(char[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); char[] result = new char[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code 0.0d}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static double[] copyOfRange(double[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); double[] result = new double[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code 0.0f}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static float[] copyOfRange(float[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); float[] result = new float[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code 0}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static int[] copyOfRange(int[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); int[] result = new int[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code 0L}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static long[] copyOfRange(long[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); long[] result = new long[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code (short) 0}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ public static short[] copyOfRange(short[] original, int start, int end) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); short[] result = new short[resultLength]; System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code null}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @since 1.6 */ @SuppressWarnings("unchecked") public static <T> T[] copyOfRange(T[] original, int start, int end) { int originalLength = original.length; // For exception priority compatibility. if (start > end) { throw new IllegalArgumentException(); } if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); T[] result = (T[]) Array.newInstance(original.getClass().getComponentType(), resultLength); System.arraycopy(original, start, result, 0, copyLength); return result; } /** * Copies elements from {@code original} into a new array, from indexes start (inclusive) to * end (exclusive). The original order of elements is preserved. * If {@code end} is greater than {@code original.length}, the result is padded * with the value {@code null}. * * @param original the original array * @param start the start index, inclusive * @param end the end index, exclusive * @return the new array * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} * @throws IllegalArgumentException if {@code start > end} * @throws NullPointerException if {@code original == null} * @throws ArrayStoreException if a value in {@code original} is incompatible with T * @since 1.6 */ @SuppressWarnings("unchecked") public static <T, U> T[] copyOfRange(U[] original, int start, int end, Class<? extends T[]> newType) { if (start > end) { throw new IllegalArgumentException(); } int originalLength = original.length; if (start < 0 || start > originalLength) { throw new ArrayIndexOutOfBoundsException(); } int resultLength = end - start; int copyLength = Math.min(resultLength, originalLength - start); T[] result = (T[]) Array.newInstance(newType.getComponentType(), resultLength); System.arraycopy(original, start, result, 0, copyLength); return result; } }