commit 3fcefd6e595e1e96c59d16f81466bd3104904563
Author: Claudio Maggioni
This class contains various utility methods that make working with + * annotations simpler.
+ * + *{@link Annotation} instances are always proxy objects; unfortunately + * dynamic proxies cannot be depended upon to know how to implement certain + * methods in the same manner as would be done by "natural" {@link Annotation}s. + * The methods presented in this class can be used to avoid that possibility. It + * is of course also possible for dynamic proxies to actually delegate their + * e.g. {@link Annotation#equals(Object)}/{@link Annotation#hashCode()}/ + * {@link Annotation#toString()} implementations to {@link AnnotationUtils}.
+ * + *#ThreadSafe#
+ * + * @since 3.0 + */ +public class AnnotationUtils { + + /** + * A style that prints annotations as recommended. + */ + private static final ToStringStyle TO_STRING_STYLE = new ToStringStyle() { + /** Serialization version */ + private static final long serialVersionUID = 1L; + + { + setDefaultFullDetail(true); + setArrayContentDetail(true); + setUseClassName(true); + setUseShortClassName(true); + setUseIdentityHashCode(false); + setContentStart("("); + setContentEnd(")"); + setFieldSeparator(", "); + setArrayStart("["); + setArrayEnd("]"); + } + + /** + * {@inheritDoc} + */ + @Override + protected String getShortClassName(final Class> cls) { + for (final Class> iface : ClassUtils.getAllInterfaces(cls)) { + if (Annotation.class.isAssignableFrom(iface)) { + return "@" + iface.getName(); + } + } + return StringUtils.EMPTY; + } + + /** + * {@inheritDoc} + */ + @Override + protected void appendDetail(final StringBuffer buffer, final String fieldName, Object value) { + if (value instanceof Annotation) { + value = AnnotationUtils.toString((Annotation) value); + } + super.appendDetail(buffer, fieldName, value); + } + + }; + + /** + *{@code AnnotationUtils} instances should NOT be constructed in + * standard programming. Instead, the class should be used statically.
+ * + *This constructor is public to permit tools that require a JavaBean + * instance to operate.
+ */ + public AnnotationUtils() { + } + + //----------------------------------------------------------------------- + /** + *Checks if two annotations are equal using the criteria for equality + * presented in the {@link Annotation#equals(Object)} API docs.
+ * + * @param a1 the first Annotation to compare, {@code null} returns + * {@code false} unless both are {@code null} + * @param a2 the second Annotation to compare, {@code null} returns + * {@code false} unless both are {@code null} + * @return {@code true} if the two annotations are {@code equal} or both + * {@code null} + */ + public static boolean equals(final Annotation a1, final Annotation a2) { + if (a1 == a2) { + return true; + } + if (a1 == null || a2 == null) { + return false; + } + final Class extends Annotation> type1 = a1.annotationType(); + final Class extends Annotation> type2 = a2.annotationType(); + Validate.notNull(type1, "Annotation %s with null annotationType()", a1); + Validate.notNull(type2, "Annotation %s with null annotationType()", a2); + if (!type1.equals(type2)) { + return false; + } + try { + for (final Method m : type1.getDeclaredMethods()) { + if (m.getParameterTypes().length == 0 + && isValidAnnotationMemberType(m.getReturnType())) { + final Object v1 = m.invoke(a1); + final Object v2 = m.invoke(a2); + if (!memberEquals(m.getReturnType(), v1, v2)) { + return false; + } + } + } + } catch (final IllegalAccessException | InvocationTargetException ex) { + return false; + } + return true; + } + + /** + *Generate a hash code for the given annotation using the algorithm + * presented in the {@link Annotation#hashCode()} API docs.
+ * + * @param a the Annotation for a hash code calculation is desired, not + * {@code null} + * @return the calculated hash code + * @throws RuntimeException if an {@code Exception} is encountered during + * annotation member access + * @throws IllegalStateException if an annotation method invocation returns + * {@code null} + */ + public static int hashCode(final Annotation a) { + int result = 0; + final Class extends Annotation> type = a.annotationType(); + for (final Method m : type.getDeclaredMethods()) { + try { + final Object value = m.invoke(a); + if (value == null) { + throw new IllegalStateException( + String.format("Annotation method %s returned null", m)); + } + result += hashMember(m.getName(), value); + } catch (final RuntimeException ex) { + throw ex; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + return result; + } + + /** + *Generate a string representation of an Annotation, as suggested by + * {@link Annotation#toString()}.
+ * + * @param a the annotation of which a string representation is desired + * @return the standard string representation of an annotation, not + * {@code null} + */ + public static String toString(final Annotation a) { + final ToStringBuilder builder = new ToStringBuilder(a, TO_STRING_STYLE); + for (final Method m : a.annotationType().getDeclaredMethods()) { + if (m.getParameterTypes().length > 0) { + continue; //wtf? + } + try { + builder.append(m.getName(), m.invoke(a)); + } catch (final RuntimeException ex) { + throw ex; + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + return builder.build(); + } + + /** + *Checks if the specified type is permitted as an annotation member.
+ * + *The Java language specification only permits certain types to be used + * in annotations. These include {@link String}, {@link Class}, primitive + * types, {@link Annotation}, {@link Enum}, and single-dimensional arrays of + * these types.
+ * + * @param type the type to check, {@code null} + * @return {@code true} if the type is a valid type to use in an annotation + */ + public static boolean isValidAnnotationMemberType(Class> type) { + if (type == null) { + return false; + } + if (type.isArray()) { + type = type.getComponentType(); + } + return type.isPrimitive() || type.isEnum() || type.isAnnotation() + || String.class.equals(type) || Class.class.equals(type); + } + + //besides modularity, this has the advantage of autoboxing primitives: + /** + * Helper method for generating a hash code for a member of an annotation. + * + * @param name the name of the member + * @param value the value of the member + * @return a hash code for this member + */ + private static int hashMember(final String name, final Object value) { + final int part1 = name.hashCode() * 127; + if (value.getClass().isArray()) { + return part1 ^ arrayMemberHash(value.getClass().getComponentType(), value); + } + if (value instanceof Annotation) { + return part1 ^ hashCode((Annotation) value); + } + return part1 ^ value.hashCode(); + } + + /** + * Helper method for checking whether two objects of the given type are + * equal. This method is used to compare the parameters of two annotation + * instances. + * + * @param type the type of the objects to be compared + * @param o1 the first object + * @param o2 the second object + * @return a flag whether these objects are equal + */ + private static boolean memberEquals(final Class> type, final Object o1, final Object o2) { + if (o1 == o2) { + return true; + } + if (o1 == null || o2 == null) { + return false; + } + if (type.isArray()) { + return arrayMemberEquals(type.getComponentType(), o1, o2); + } + if (type.isAnnotation()) { + return equals((Annotation) o1, (Annotation) o2); + } + return o1.equals(o2); + } + + /** + * Helper method for comparing two objects of an array type. + * + * @param componentType the component type of the array + * @param o1 the first object + * @param o2 the second object + * @return a flag whether these objects are equal + */ + private static boolean arrayMemberEquals(final Class> componentType, final Object o1, final Object o2) { + if (componentType.isAnnotation()) { + return annotationArrayMemberEquals((Annotation[]) o1, (Annotation[]) o2); + } + if (componentType.equals(Byte.TYPE)) { + return Arrays.equals((byte[]) o1, (byte[]) o2); + } + if (componentType.equals(Short.TYPE)) { + return Arrays.equals((short[]) o1, (short[]) o2); + } + if (componentType.equals(Integer.TYPE)) { + return Arrays.equals((int[]) o1, (int[]) o2); + } + if (componentType.equals(Character.TYPE)) { + return Arrays.equals((char[]) o1, (char[]) o2); + } + if (componentType.equals(Long.TYPE)) { + return Arrays.equals((long[]) o1, (long[]) o2); + } + if (componentType.equals(Float.TYPE)) { + return Arrays.equals((float[]) o1, (float[]) o2); + } + if (componentType.equals(Double.TYPE)) { + return Arrays.equals((double[]) o1, (double[]) o2); + } + if (componentType.equals(Boolean.TYPE)) { + return Arrays.equals((boolean[]) o1, (boolean[]) o2); + } + return Arrays.equals((Object[]) o1, (Object[]) o2); + } + + /** + * Helper method for comparing two arrays of annotations. + * + * @param a1 the first array + * @param a2 the second array + * @return a flag whether these arrays are equal + */ + private static boolean annotationArrayMemberEquals(final Annotation[] a1, final Annotation[] a2) { + if (a1.length != a2.length) { + return false; + } + for (int i = 0; i < a1.length; i++) { + if (!equals(a1[i], a2[i])) { + return false; + } + } + return true; + } + + /** + * Helper method for generating a hash code for an array. + * + * @param componentType the component type of the array + * @param o the array + * @return a hash code for the specified array + */ + private static int arrayMemberHash(final Class> componentType, final Object o) { + if (componentType.equals(Byte.TYPE)) { + return Arrays.hashCode((byte[]) o); + } + if (componentType.equals(Short.TYPE)) { + return Arrays.hashCode((short[]) o); + } + if (componentType.equals(Integer.TYPE)) { + return Arrays.hashCode((int[]) o); + } + if (componentType.equals(Character.TYPE)) { + return Arrays.hashCode((char[]) o); + } + if (componentType.equals(Long.TYPE)) { + return Arrays.hashCode((long[]) o); + } + if (componentType.equals(Float.TYPE)) { + return Arrays.hashCode((float[]) o); + } + if (componentType.equals(Double.TYPE)) { + return Arrays.hashCode((double[]) o); + } + if (componentType.equals(Boolean.TYPE)) { + return Arrays.hashCode((boolean[]) o); + } + return Arrays.hashCode((Object[]) o); + } +} diff --git a/before/src/main/java/org/apache/commons/lang3/ArchUtils.java b/before/src/main/java/org/apache/commons/lang3/ArchUtils.java new file mode 100644 index 0000000..20ba2fb --- /dev/null +++ b/before/src/main/java/org/apache/commons/lang3/ArchUtils.java @@ -0,0 +1,132 @@ +/* + * 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 org.apache.commons.lang3; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; + +import org.apache.commons.lang3.arch.Processor; + +/** + * An utility class for the os.arch System Property. The class defines methods for + * identifying the architecture of the current JVM. + *+ * Important: The os.arch System Property returns the architecture used by the JVM + * not of the operating system. + *
+ * @since 3.6 + */ +public class ArchUtils { + + private static final Map+ * Important: The os.arch System Property returns the architecture used by the JVM + * not of the operating system. + *
+ * + * @return A {@link Processor} when supported, else {@code null}. + */ + public static Processor getProcessor() { + return getProcessor(SystemUtils.OS_ARCH); + } + + /** + * Returns a {@link Processor} object the given value {@link String}. The {@link String} must be + * like a value returned by the os.arch System Property. + * + * @param value A {@link String} like a value returned by the os.arch System Property. + * @return A {@link Processor} when it exists, else {@code null}. + */ + public static Processor getProcessor(final String value) { + return ARCH_TO_PROCESSOR.get(value); + } + +} diff --git a/before/src/main/java/org/apache/commons/lang3/ArraySorter.java b/before/src/main/java/org/apache/commons/lang3/ArraySorter.java new file mode 100644 index 0000000..2208bd9 --- /dev/null +++ b/before/src/main/java/org/apache/commons/lang3/ArraySorter.java @@ -0,0 +1,141 @@ +/* + * 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 org.apache.commons.lang3; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * Sorts and returns arrays in the fluent style. + * + * @since 3.12.0 + */ +public class ArraySorter { + + /** + * Sorts and returns the given array. + * + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(byte[]) + */ + public static byte[] sort(final byte[] array) { + Arrays.sort(array); + return array; + } + + /** + * Sorts and returns the given array. + * + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(char[]) + */ + public static char[] sort(final char[] array) { + Arrays.sort(array); + return array; + } + + /** + * Sorts and returns the given array. + * + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(double[]) + */ + public static double[] sort(final double[] array) { + Arrays.sort(array); + return array; + } + + /** + * Sorts and returns the given array. + * + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(float[]) + */ + public static float[] sort(final float[] array) { + Arrays.sort(array); + return array; + } + + /** + * Sorts and returns the given array. + * + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(int[]) + */ + public static int[] sort(final int[] array) { + Arrays.sort(array); + return array; + } + + /** + * Sorts and returns the given array. + * + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(long[]) + */ + public static long[] sort(final long[] array) { + Arrays.sort(array); + return array; + } + + /** + * Sorts and returns the given array. + * + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(short[]) + */ + public static short[] sort(final short[] array) { + Arrays.sort(array); + return array; + } + + /** + * Sorts and returns the given array. + * + * @paramOperations on arrays, primitive arrays (like {@code int[]}) and + * primitive wrapper arrays (like {@code Integer[]}). + * + *
This class tries to handle {@code null} input gracefully. + * An exception will not be thrown for a {@code null} + * array input. However, an Object array that contains a {@code null} + * element may throw an exception. Each method documents its behavior. + * + *
#ThreadSafe# + * @since 2.0 + */ +public class ArrayUtils { + + /** + * An empty immutable {@code boolean} array. + */ + public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; + + /** + * An empty immutable {@code Boolean} array. + */ + public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0]; + + /** + * An empty immutable {@code byte} array. + */ + public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + + /** + * An empty immutable {@code Byte} array. + */ + public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0]; + + /** + * An empty immutable {@code char} array. + */ + public static final char[] EMPTY_CHAR_ARRAY = new char[0]; + + /** + * An empty immutable {@code Character} array. + */ + public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0]; + + /** + * An empty immutable {@code Class} array. + */ + public static final Class>[] EMPTY_CLASS_ARRAY = new Class[0]; + + /** + * An empty immutable {@code double} array. + */ + public static final double[] EMPTY_DOUBLE_ARRAY = new double[0]; + + /** + * An empty immutable {@code Double} array. + */ + public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0]; + + /** + * An empty immutable {@code Field} array. + * + * @since 3.10 + */ + public static final Field[] EMPTY_FIELD_ARRAY = new Field[0]; + + /** + * An empty immutable {@code float} array. + */ + public static final float[] EMPTY_FLOAT_ARRAY = new float[0]; + + /** + * An empty immutable {@code Float} array. + */ + public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0]; + + /** + * An empty immutable {@code int} array. + */ + public static final int[] EMPTY_INT_ARRAY = new int[0]; + + /** + * An empty immutable {@code Integer} array. + */ + public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0]; + + /** + * An empty immutable {@code long} array. + */ + public static final long[] EMPTY_LONG_ARRAY = new long[0]; + + /** + * An empty immutable {@code Long} array. + */ + public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0]; + + /** + * An empty immutable {@code Method} array. + * + * @since 3.10 + */ + public static final Method[] EMPTY_METHOD_ARRAY = new Method[0]; + + /** + * An empty immutable {@code Object} array. + */ + public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + + /** + * An empty immutable {@code short} array. + */ + public static final short[] EMPTY_SHORT_ARRAY = new short[0]; + + /** + * An empty immutable {@code Short} array. + */ + public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0]; + + /** + * An empty immutable {@code String} array. + */ + public static final String[] EMPTY_STRING_ARRAY = new String[0]; + + /** + * An empty immutable {@code Throwable} array. + * + * @since 3.10 + */ + public static final Throwable[] EMPTY_THROWABLE_ARRAY = new Throwable[0]; + + /** + * An empty immutable {@code Type} array. + * + * @since 3.10 + */ + public static final Type[] EMPTY_TYPE_ARRAY = new Type[0]; + + /** + * The index value when an element is not found in a list or array: {@code -1}. + * This value is returned by methods in this class and can also be used in comparisons with values returned by + * various method from {@link java.util.List}. + */ + public static final int INDEX_NOT_FOUND = -1; + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, true) = [true] + * ArrayUtils.add([true], false) = [true, false] + * ArrayUtils.add([true, false], true) = [true, false, true] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static boolean[] add(final boolean[] array, final boolean element) { + final boolean[] newArray = (boolean[]) copyArrayGrow1(array, Boolean.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0, true) = [true] + * ArrayUtils.add([true], 0, false) = [false, true] + * ArrayUtils.add([false], 1, true) = [false, true] + * ArrayUtils.add([true, false], 1, true) = [true, true, false] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, boolean[], boolean...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static boolean[] add(final boolean[] array, final int index, final boolean element) { + return (boolean[]) add(array, index, Boolean.valueOf(element), Boolean.TYPE); + } + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0) = [0] + * ArrayUtils.add([1], 0) = [1, 0] + * ArrayUtils.add([1, 0], 1) = [1, 0, 1] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static byte[] add(final byte[] array, final byte element) { + final byte[] newArray = (byte[]) copyArrayGrow1(array, Byte.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add([1], 0, 2) = [2, 1] + * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3] + * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6] + * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, byte[], byte...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static byte[] add(final byte[] array, final int index, final byte element) { + return (byte[]) add(array, index, Byte.valueOf(element), Byte.TYPE); + } + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, '0') = ['0'] + * ArrayUtils.add(['1'], '0') = ['1', '0'] + * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1'] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static char[] add(final char[] array, final char element) { + final char[] newArray = (char[]) copyArrayGrow1(array, Character.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0, 'a') = ['a'] + * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a'] + * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b'] + * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b'] + * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c'] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, char[], char...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static char[] add(final char[] array, final int index, final char element) { + return (char[]) add(array, index, Character.valueOf(element), Character.TYPE); + } + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0) = [0] + * ArrayUtils.add([1], 0) = [1, 0] + * ArrayUtils.add([1, 0], 1) = [1, 0, 1] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static double[] add(final double[] array, final double element) { + final double[] newArray = (double[]) copyArrayGrow1(array, Double.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1] + * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5] + * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7] + * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, double[], double...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static double[] add(final double[] array, final int index, final double element) { + return (double[]) add(array, index, Double.valueOf(element), Double.TYPE); + } + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0) = [0] + * ArrayUtils.add([1], 0) = [1, 0] + * ArrayUtils.add([1, 0], 1) = [1, 0, 1] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static float[] add(final float[] array, final float element) { + final float[] newArray = (float[]) copyArrayGrow1(array, Float.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f] + * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f] + * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f] + * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f, 0.3f] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, float[], float...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static float[] add(final float[] array, final int index, final float element) { + return (float[]) add(array, index, Float.valueOf(element), Float.TYPE); + } + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0) = [0] + * ArrayUtils.add([1], 0) = [1, 0] + * ArrayUtils.add([1, 0], 1) = [1, 0, 1] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static int[] add(final int[] array, final int element) { + final int[] newArray = (int[]) copyArrayGrow1(array, Integer.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add([1], 0, 2) = [2, 1] + * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10] + * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6] + * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, int[], int...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static int[] add(final int[] array, final int index, final int element) { + return (int[]) add(array, index, Integer.valueOf(element), Integer.TYPE); + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add([1L], 0, 2L) = [2L, 1L] + * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L] + * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L] + * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, long[], long...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static long[] add(final long[] array, final int index, final long element) { + return (long[]) add(array, index, Long.valueOf(element), Long.TYPE); + } + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0) = [0] + * ArrayUtils.add([1], 0) = [1, 0] + * ArrayUtils.add([1, 0], 1) = [1, 0, 1] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static long[] add(final long[] array, final long element) { + final long[] newArray = (long[]) copyArrayGrow1(array, Long.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + /** + * Underlying implementation of add(array, index, element) methods. + * The last parameter is the class, which may not equal element.getClass + * for primitives. + * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @param clss the type of the element being added + * @return A new array containing the existing elements and the new element + */ + private static Object add(final Object array, final int index, final Object element, final Class> clss) { + if (array == null) { + if (index != 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0"); + } + final Object joinedArray = Array.newInstance(clss, 1); + Array.set(joinedArray, 0, element); + return joinedArray; + } + final int length = Array.getLength(array); + if (index > length || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); + } + final Object result = Array.newInstance(clss, length + 1); + System.arraycopy(array, 0, result, 0, index); + Array.set(result, index, element); + if (index < length) { + System.arraycopy(array, index, result, index + 1, length - index); + } + return result; + } + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add([1], 0, 2) = [2, 1] + * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10] + * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6] + * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3] + *+ * + * @param array the array to add the element to, may be {@code null} + * @param index the position of the new object + * @param element the object to add + * @return A new array containing the existing elements and the new element + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index > array.length). + * @deprecated this method has been superseded by {@link #insert(int, short[], short...)} and + * may be removed in a future release. Please note the handling of {@code null} input arrays differs + * in the new method: inserting {@code X} into a {@code null} array results in {@code null} not {@code X}. + */ + @Deprecated + public static short[] add(final short[] array, final int index, final short element) { + return (short[]) add(array, index, Short.valueOf(element), Short.TYPE); + } + + /** + *
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0) = [0] + * ArrayUtils.add([1], 0) = [1, 0] + * ArrayUtils.add([1, 0], 1) = [1, 0, 1] + *+ * + * @param array the array to copy and add the element to, may be {@code null} + * @param element the object to add at the last index of the new array + * @return A new array containing the existing elements plus the new element + * @since 2.1 + */ + public static short[] add(final short[] array, final short element) { + final short[] newArray = (short[]) copyArrayGrow1(array, Short.TYPE); + newArray[newArray.length - 1] = element; + return newArray; + } + + + /** + *
Inserts the specified element at the specified position in the array. + * Shifts the element currently at that position (if any) and any subsequent + * elements to the right (adds one to their indices). + * + *
This method returns a new array with the same elements of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input + * array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element. + * + *
+ * ArrayUtils.add(null, 0, null) = IllegalArgumentException + * ArrayUtils.add(null, 0, "a") = ["a"] + * ArrayUtils.add(["a"], 1, null) = ["a", null] + * ArrayUtils.add(["a"], 1, "b") = ["a", "b"] + * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"] + *+ * + * @param
Copies the given array and adds the given element at the end of the new array. + * + *
The new array contains the same elements of the input + * array plus the given element in the last position. The component type of + * the new array is the same as that of the input array. + * + *
If the input array is {@code null}, a new one element array is returned + * whose component type is the same as the element, unless the element itself is null, + * in which case the return type is Object[] + * + *
+ * ArrayUtils.add(null, null) = IllegalArgumentException + * ArrayUtils.add(null, "a") = ["a"] + * ArrayUtils.add(["a"], null) = ["a", null] + * ArrayUtils.add(["a"], "b") = ["a", "b"] + * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"] + *+ * + * @param
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new boolean[] array. + * @since 2.1 + */ + public static boolean[] addAll(final boolean[] array1, final boolean... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final boolean[] joinedArray = new boolean[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new byte[] array. + * @since 2.1 + */ + public static byte[] addAll(final byte[] array1, final byte... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final byte[] joinedArray = new byte[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new char[] array. + * @since 2.1 + */ + public static char[] addAll(final char[] array1, final char... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final char[] joinedArray = new char[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new double[] array. + * @since 2.1 + */ + public static double[] addAll(final double[] array1, final double... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final double[] joinedArray = new double[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new float[] array. + * @since 2.1 + */ + public static float[] addAll(final float[] array1, final float... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final float[] joinedArray = new float[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new int[] array. + * @since 2.1 + */ + public static int[] addAll(final int[] array1, final int... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final int[] joinedArray = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new long[] array. + * @since 2.1 + */ + public static long[] addAll(final long[] array1, final long... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final long[] joinedArray = new long[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + *+ * + * @param array1 the first array whose elements are added to the new array. + * @param array2 the second array whose elements are added to the new array. + * @return The new short[] array. + * @since 2.1 + */ + public static short[] addAll(final short[] array1, final short... array2) { + if (array1 == null) { + return clone(array2); + } else if (array2 == null) { + return clone(array1); + } + final short[] joinedArray = new short[array1.length + array2.length]; + System.arraycopy(array1, 0, joinedArray, 0, array1.length); + System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); + return joinedArray; + } + + /** + *
Adds all the elements of the given arrays into a new array. + *
The new array contains all of the element of {@code array1} followed + * by all of the elements {@code array2}. When an array is returned, it is always + * a new array. + * + *
+ * ArrayUtils.addAll(null, null) = null + * ArrayUtils.addAll(array1, null) = cloned copy of array1 + * ArrayUtils.addAll(null, array2) = cloned copy of array2 + * ArrayUtils.addAll([], []) = [] + * ArrayUtils.addAll([null], [null]) = [null, null] + * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"] + *+ * + * @param
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, true) = [true] + * ArrayUtils.addFirst([true], false) = [false, true] + * ArrayUtils.addFirst([true, false], true) = [true, true, false] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static boolean[] addFirst(final boolean[] array, final boolean element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, 1) = [1] + * ArrayUtils.addFirst([1], 0) = [0, 1] + * ArrayUtils.addFirst([1, 0], 1) = [1, 1, 0] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static byte[] addFirst(final byte[] array, final byte element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, '1') = ['1'] + * ArrayUtils.addFirst(['1'], '0') = ['0', '1'] + * ArrayUtils.addFirst(['1', '0'], '1') = ['1', '1', '0'] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static char[] addFirst(final char[] array, final char element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, 1) = [1] + * ArrayUtils.addFirst([1], 0) = [0, 1] + * ArrayUtils.addFirst([1, 0], 1) = [1, 1, 0] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static double[] addFirst(final double[] array, final double element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, 1) = [1] + * ArrayUtils.addFirst([1], 0) = [0, 1] + * ArrayUtils.addFirst([1, 0], 1) = [1, 1, 0] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static float[] addFirst(final float[] array, final float element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, 1) = [1] + * ArrayUtils.addFirst([1], 0) = [0, 1] + * ArrayUtils.addFirst([1, 0], 1) = [1, 1, 0] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static int[] addFirst(final int[] array, final int element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, 1) = [1] + * ArrayUtils.addFirst([1], 0) = [0, 1] + * ArrayUtils.addFirst([1, 0], 1) = [1, 1, 0] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static long[] addFirst(final long[] array, final long element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first position. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element. + *
+ * + *+ * ArrayUtils.addFirst(null, 1) = [1] + * ArrayUtils.addFirst([1], 0) = [0, 1] + * ArrayUtils.addFirst([1, 0], 1) = [1, 1, 0] + *+ * + * @param array the array to "add" the element to, may be {@code null}. + * @param element the object to add. + * @return A new array containing the existing elements plus the new element The returned array type will be that of + * the input array (unless null), in which case it will have the same type as the element. + * @since 3.10 + */ + public static short[] addFirst(final short[] array, final short element) { + return array == null ? add(array, element) : insert(0, array, element); + } + + /** + * Copies the given array and adds the given element at the beginning of the new array. + * + *
+ * The new array contains the same elements of the input array plus the given element in the first positioaddFirstaddFirstaddFirstn. The + * component type of the new array is the same as that of the input array. + *
+ * + *+ * If the input array is {@code null}, a new one element array is returned whose component type is the same as the + * element, unless the element itself is null, in which case the return type is Object[] + *
+ * + *+ * ArrayUtils.addFirst(null, null) = IllegalArgumentException + * ArrayUtils.addFirst(null, "a") = ["a"] + * ArrayUtils.addFirst(["a"], null) = [null, "a"] + * ArrayUtils.addFirst(["a"], "b") = ["b", "a"] + * ArrayUtils.addFirst(["a", "b"], "c") = ["c", "a", "b"] + *+ * + * @param
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static boolean[] clone(final boolean[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + /** + *
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static byte[] clone(final byte[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + /** + *
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static char[] clone(final char[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + /** + *
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static double[] clone(final double[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + /** + *
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static float[] clone(final float[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + /** + *
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static int[] clone(final int[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + /** + *
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static long[] clone(final long[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + /** + *
Clones an array returning a typecast result and handling + * {@code null}. + * + *
This method returns {@code null} for a {@code null} input array. + * + * @param array the array to clone, may be {@code null} + * @return the cloned array, {@code null} if {@code null} input + */ + public static short[] clone(final short[] array) { + if (array == null) { + return null; + } + return array.clone(); + } + + // Clone + //----------------------------------------------------------------------- + /** + *
Shallow clones an array returning a typecast result and handling + * {@code null}. + * + *
The objects in the array are not cloned, thus there is no special + * handling for multi-dimensional arrays. + * + *
This method returns {@code null} for a {@code null} input array.
+ *
+ * @param Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final boolean[] array, final boolean valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final byte[] array, final byte valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ * @since 2.1
+ */
+ public static boolean contains(final char[] array, final char valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final double[] array, final double valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if a value falling within the given tolerance is in the
+ * given array. If the array contains a value within the inclusive range
+ * defined by (value - tolerance) to (value + tolerance).
+ *
+ * The method returns {@code false} if a {@code null} array
+ * is passed in.
+ *
+ * @param array the array to search
+ * @param valueToFind the value to find
+ * @param tolerance the array contains the tolerance of the search
+ * @return true if value falling within tolerance is in array
+ */
+ public static boolean contains(final double[] array, final double valueToFind, final double tolerance) {
+ return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final float[] array, final float valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final int[] array, final int valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final long[] array, final long valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the object is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param objectToFind the object to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final Object[] array, final Object objectToFind) {
+ return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ *
+ * The method returns {@code false} if a {@code null} array is passed in.
+ *
+ * @param array the array to search through
+ * @param valueToFind the value to find
+ * @return {@code true} if the array contains the object
+ */
+ public static boolean contains(final short[] array, final short valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Returns a copy of the given array of size 1 greater than the argument.
+ * The last value of the array is left to the default value.
+ *
+ * @param array The array to copy, must not be {@code null}.
+ * @param newArrayComponentType If {@code array} is {@code null}, create a
+ * size 1 array of this type.
+ * @return A new copy of the array of size 1 greater than the input.
+ */
+ private static Object copyArrayGrow1(final Object array, final Class> newArrayComponentType) {
+ if (array != null) {
+ final int arrayLength = Array.getLength(array);
+ final Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
+ System.arraycopy(array, 0, newArray, 0, arrayLength);
+ return newArray;
+ }
+ return Array.newInstance(newArrayComponentType, 1);
+ }
+
+ /**
+ * Gets the nTh element of an array or null if the index is out of bounds or the array is null.
+ *
+ * @param Returns the length of the specified array.
+ * This method can deal with {@code Object} arrays and with primitive arrays.
+ *
+ * If the input array is {@code null}, {@code 0} is returned.
+ *
+ * Get a hash code for an array handling multi-dimensional arrays correctly.
+ *
+ * Multi-dimensional primitive arrays are also handled correctly by this method.
+ *
+ * @param array the array to get a hash code for, {@code null} returns zero
+ * @return a hash code for the array
+ */
+ public static int hashCode(final Object array) {
+ return new HashCodeBuilder().append(array).toHashCode();
+ }
+
+ /**
+ * Finds the indices of the given value in the array.
+ *
+ * This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet ({@code -1}). This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet. This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet. This method returns empty BitSet for a {@code null} input array.
+ * This method will return all the indices of the value which fall between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance, each time between the nearest integers.
+ * This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet.
+ * This method will return the indices of the values which fall between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance, between the nearest integers.
+ * This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet. This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return empty BitSet. This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet. This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet. This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet. This method returns an empty BitSet for a {@code null} input array. This method returns an empty BitSet for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return an empty BitSet. Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final boolean[] array, final boolean valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null}
+ * array input
+ */
+ public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ // byte IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final byte[] array, final byte valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final byte[] array, final byte valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ // char IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
+ */
+ public static int indexOf(final char[] array, final char valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
+ */
+ public static int indexOf(final char[] array, final char valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ // double IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final double[] array, final double valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value within a given tolerance in the array.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final double[] array, final double valueToFind, final double tolerance) {
+ return indexOf(array, valueToFind, 0, tolerance);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final double[] array, final double valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ final boolean searchNaN = Double.isNaN(valueToFind);
+ for (int i = startIndex; i < array.length; i++) {
+ final double element = array[i];
+ if (valueToFind == element || (searchNaN && Double.isNaN(element))) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ final double min = valueToFind - tolerance;
+ final double max = valueToFind + tolerance;
+ for (int i = startIndex; i < array.length; i++) {
+ if (array[i] >= min && array[i] <= max) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ // float IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final float[] array, final float valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final float[] array, final float valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ final boolean searchNaN = Float.isNaN(valueToFind);
+ for (int i = startIndex; i < array.length; i++) {
+ final float element = array[i];
+ if (valueToFind == element || (searchNaN && Float.isNaN(element))) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ // int IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final int[] array, final int valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+public static int indexOf(final int[] array, final int valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+}
+
+ // long IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final long[] array, final long valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final long[] array, final long valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ // Object IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given object in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @return the index of the object within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final Object[] array, final Object objectToFind) {
+ return indexOf(array, objectToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given object in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @param startIndex the index to start searching at
+ * @return the index of the object within the array starting at the index,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ if (objectToFind == null) {
+ for (int i = startIndex; i < array.length; i++) {
+ if (array[i] == null) {
+ return i;
+ }
+ }
+ } else {
+ for (int i = startIndex; i < array.length; i++) {
+ if (objectToFind.equals(array[i])) {
+ return i;
+ }
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ // short IndexOf
+ //-----------------------------------------------------------------------
+ /**
+ * Finds the index of the given value in the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final short[] array, final short valueToFind) {
+ return indexOf(array, valueToFind, 0);
+ }
+
+ /**
+ * Finds the index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the index to start searching at
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int indexOf(final short[] array, final short valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ startIndex = 0;
+ }
+ for (int i = startIndex; i < array.length; i++) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Inserts elements into an array at the given index (starting from zero). When an array is returned, it is always a new array. Checks if an array of primitive booleans is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final boolean[] array) {
+ return getLength(array) == 0;
+ }
+
+ /**
+ * Checks if an array of primitive bytes is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final byte[] array) {
+ return getLength(array) == 0;
+ }
+
+ // IndexOf search
+ // ----------------------------------------------------------------------
+
+ /**
+ * Checks if an array of primitive chars is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final char[] array) {
+ return getLength(array) == 0;
+ }
+
+ /**
+ * Checks if an array of primitive doubles is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final double[] array) {
+ return getLength(array) == 0;
+ }
+
+ /**
+ * Checks if an array of primitive floats is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final float[] array) {
+ return getLength(array) == 0;
+ }
+
+ /**
+ * Checks if an array of primitive ints is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final int[] array) {
+ return getLength(array) == 0;
+ }
+
+
+
+ /**
+ * Checks if an array of primitive longs is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final long[] array) {
+ return getLength(array) == 0;
+ }
+
+ // ----------------------------------------------------------------------
+ /**
+ * Checks if an array of Objects is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final Object[] array) {
+ return getLength(array) == 0;
+ }
+
+ /**
+ * Checks if an array of primitive shorts is empty or {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is empty or {@code null}
+ * @since 2.1
+ */
+ public static boolean isEmpty(final short[] array) {
+ return getLength(array) == 0;
+ }
+
+ /**
+ * Compares two arrays, using equals(), handling multi-dimensional arrays
+ * correctly.
+ *
+ * Multi-dimensional primitive arrays are also handled correctly by this method.
+ *
+ * @param array1 the left hand array to compare, may be {@code null}
+ * @param array2 the right hand array to compare, may be {@code null}
+ * @return {@code true} if the arrays are equal
+ * @deprecated this method has been replaced by {@code java.util.Objects.deepEquals(Object, Object)} and will be
+ * removed from future releases.
+ */
+ @Deprecated
+ public static boolean isEquals(final Object array1, final Object array2) {
+ return new EqualsBuilder().append(array1, array2).isEquals();
+ }
+
+ /**
+ * Checks if an array of primitive booleans is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final boolean[] array) {
+ return !isEmpty(array);
+ }
+
+ /**
+ * Checks if an array of primitive bytes is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final byte[] array) {
+ return !isEmpty(array);
+ }
+
+ /**
+ * Checks if an array of primitive chars is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final char[] array) {
+ return !isEmpty(array);
+ }
+
+ /**
+ * Checks if an array of primitive doubles is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final double[] array) {
+ return !isEmpty(array);
+ }
+
+ /**
+ * Checks if an array of primitive floats is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final float[] array) {
+ return !isEmpty(array);
+ }
+
+ /**
+ * Checks if an array of primitive ints is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final int[] array) {
+ return !isEmpty(array);
+ }
+
+ /**
+ * Checks if an array of primitive longs is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final long[] array) {
+ return !isEmpty(array);
+ }
+
+ /**
+ * Checks if an array of primitive shorts is not empty and not {@code null}.
+ *
+ * @param array the array to test
+ * @return {@code true} if the array is not empty and not {@code null}
+ * @since 2.5
+ */
+ public static boolean isNotEmpty(final short[] array) {
+ return !isEmpty(array);
+ }
+
+ // ----------------------------------------------------------------------
+ /**
+ * Checks if an array of Objects is not empty and not {@code null}.
+ *
+ * @param Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final boolean[] array1, final boolean[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final byte[] array1, final byte[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final char[] array1, final char[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final double[] array1, final double[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final float[] array1, final float[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final int[] array1, final int[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final long[] array1, final long[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * Any multi-dimensional aspects of the arrays are ignored.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ * @since 3.11
+ */
+ public static boolean isSameLength(final Object array1, final Object array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * Any multi-dimensional aspects of the arrays are ignored.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final Object[] array1, final Object[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same length, treating
+ * {@code null} arrays as length {@code 0}.
+ *
+ * @param array1 the first array, may be {@code null}
+ * @param array2 the second array, may be {@code null}
+ * @return {@code true} if length of arrays matches, treating
+ * {@code null} as an empty array
+ */
+ public static boolean isSameLength(final short[] array1, final short[] array2) {
+ return getLength(array1) == getLength(array2);
+ }
+
+ /**
+ * Checks whether two arrays are the same type taking into account
+ * multi-dimensional arrays.
+ *
+ * @param array1 the first array, must not be {@code null}
+ * @param array2 the second array, must not be {@code null}
+ * @return {@code true} if type of arrays matches
+ * @throws IllegalArgumentException if either array is {@code null}
+ */
+ public static boolean isSameType(final Object array1, final Object array2) {
+ if (array1 == null || array2 == null) {
+ throw new IllegalArgumentException("The Array must not be null");
+ }
+ return array1.getClass().getName().equals(array2.getClass().getName());
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering
+ * ({@code false} before {@code true}).
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final boolean[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ boolean previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final boolean current = array[i];
+ if (BooleanUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final byte[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ byte previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final byte current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final char[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ char previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final char current = array[i];
+ if (CharUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final double[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ double previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final double current = array[i];
+ if (Double.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final float[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ float previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final float current = array[i];
+ if (Float.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final int[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ int previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final int current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final long[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ long previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final long current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to natural ordering.
+ *
+ * @param array the array to check
+ * @return whether the array is sorted according to natural ordering
+ * @since 3.4
+ */
+ public static boolean isSorted(final short[] array) {
+ if (array == null || array.length < 2) {
+ return true;
+ }
+
+ short previous = array[0];
+ final int n = array.length;
+ for (int i = 1; i < n; i++) {
+ final short current = array[i];
+ if (NumberUtils.compare(previous, current) > 0) {
+ return false;
+ }
+
+ previous = current;
+ }
+ return true;
+ }
+
+ /**
+ * This method checks whether the provided array is sorted according to the class's
+ * {@code compareTo} method.
+ *
+ * @param array the array to check
+ * @param This method checks whether the provided array is sorted according to the provided {@code Comparator}.
+ *
+ * @param array the array to check
+ * @param comparator the {@code Comparator} to compare over
+ * @param Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) if
+ * {@code null} array input.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final boolean[] array, final boolean valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
+ * the array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final byte[] array, final byte valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
+ */
+ public static int lastIndexOf(final char[] array, final char valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ * @since 2.1
+ */
+ public static int lastIndexOf(final char[] array, final char valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final double[] array, final double valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given value within a given tolerance in the array.
+ * This method will return the index of the last value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to search through for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param tolerance tolerance of the search
+ * @return the index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final double[] array, final double valueToFind, final double tolerance) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance);
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ * This method will return the index of the last value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @param tolerance search for value within plus/minus this amount
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ final double min = valueToFind - tolerance;
+ final double max = valueToFind + tolerance;
+ for (int i = startIndex; i >= 0; i--) {
+ if (array[i] >= min && array[i] <= max) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final float[] array, final float valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) {
+ if (isEmpty(array)) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final int[] array, final int valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final long[] array, final long valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given object within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @return the last index of the object within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final Object[] array, final Object objectToFind) {
+ return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given object in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
+ * the array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param objectToFind the object to find, may be {@code null}
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the object within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ if (objectToFind == null) {
+ for (int i = startIndex; i >= 0; i--) {
+ if (array[i] == null) {
+ return i;
+ }
+ }
+ } else if (array.getClass().getComponentType().isInstance(objectToFind)) {
+ for (int i = startIndex; i >= 0; i--) {
+ if (objectToFind.equals(array[i])) {
+ return i;
+ }
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Finds the last index of the given value within the array.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * @param array the array to traverse backwards looking for the object, may be {@code null}
+ * @param valueToFind the object to find
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final short[] array, final short valueToFind) {
+ return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Finds the last index of the given value in the array starting at the given index.
+ *
+ * This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ *
+ * A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array.
+ *
+ * @param array the array to traverse for looking for the object, may be {@code null}
+ * @param valueToFind the value to find
+ * @param startIndex the start index to traverse backwards from
+ * @return the last index of the value within the array,
+ * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input
+ */
+ public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) {
+ if (array == null) {
+ return INDEX_NOT_FOUND;
+ }
+ if (startIndex < 0) {
+ return INDEX_NOT_FOUND;
+ } else if (startIndex >= array.length) {
+ startIndex = array.length - 1;
+ }
+ for (int i = startIndex; i >= 0; i--) {
+ if (valueToFind == array[i]) {
+ return i;
+ }
+ }
+ return INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static boolean[] nullToEmpty(final boolean[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BOOLEAN_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Boolean[] nullToEmpty(final Boolean[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BOOLEAN_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static byte[] nullToEmpty(final byte[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BYTE_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Byte[] nullToEmpty(final Byte[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_BYTE_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static char[] nullToEmpty(final char[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_CHAR_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Character[] nullToEmpty(final Character[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_CHARACTER_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 3.2
+ */
+ public static Class>[] nullToEmpty(final Class>[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_CLASS_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static double[] nullToEmpty(final double[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_DOUBLE_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Double[] nullToEmpty(final Double[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_DOUBLE_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static float[] nullToEmpty(final float[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_FLOAT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Float[] nullToEmpty(final Float[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_FLOAT_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static int[] nullToEmpty(final int[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_INT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Integer[] nullToEmpty(final Integer[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_INTEGER_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ // Primitive/Object array converters
+ // ----------------------------------------------------------------------
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static long[] nullToEmpty(final long[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_LONG_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Long[] nullToEmpty(final Long[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_LONG_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Object[] nullToEmpty(final Object[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static short[] nullToEmpty(final short[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_SHORT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static Short[] nullToEmpty(final Short[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_SHORT_OBJECT_ARRAY;
+ }
+ return array;
+ }
+
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * As a memory optimizing technique an empty array passed in will be overridden with
+ * the empty {@code public static} references in this class.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @return the same array, {@code public static} empty array if {@code null} or empty input
+ * @since 2.5
+ */
+ public static String[] nullToEmpty(final String[] array) {
+ if (isEmpty(array)) {
+ return EMPTY_STRING_ARRAY;
+ }
+ return array;
+ }
+
+ // nullToEmpty
+ //-----------------------------------------------------------------------
+ /**
+ * Defensive programming technique to change a {@code null}
+ * reference to an empty one.
+ *
+ * This method returns an empty array for a {@code null} input array.
+ *
+ * @param array the array to check for {@code null} or empty
+ * @param type the class representation of the desired array
+ * @param Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ private static Object remove(final Object array, final int index) {
+ final int length = getLength(array);
+ if (index < 0 || index >= length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
+ }
+
+ final Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
+ System.arraycopy(array, 0, result, 0, index);
+ if (index < length - 1) {
+ System.arraycopy(array, index + 1, result, index, length - index - 1);
+ }
+
+ return result;
+ }
+
+ /**
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the element at the specified position from the specified array.
+ * All subsequent elements are shifted to the left (subtracts one from
+ * their indices).
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the element on the specified position. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ * Removes the elements at the specified positions from the specified array.
+ * All remaining elements are shifted to the left.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except those at the specified positions. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * If the input array is {@code null}, an IndexOutOfBoundsException
+ * will be thrown, because in that case no valid index can be specified.
+ *
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ *
+ * All subsequent elements are shifted to the left (subtracts one from their indices).
+ * If the array doesn't contains such an element, no elements are removed from the array.
+ * {@code null} will be returned if the input array is {@code null}.
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes the first occurrence of the specified element from the
+ * specified array. All subsequent elements are shifted to the left
+ * (subtracts one from their indices). If the array doesn't contains
+ * such an element, no elements are removed from the array.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except the first occurrence of the specified element. The component
+ * type of the returned array is always the same as that of the input
+ * array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Removes occurrences of specified elements, in specified quantities,
+ * from the specified array. All subsequent elements are shifted left.
+ * For any element-to-be-removed specified in greater quantities than
+ * contained in the original array, no change occurs beyond the
+ * removal of the existing matching items.
+ *
+ * This method returns a new array with the same elements of the input
+ * array except for the earliest-encountered occurrences of the specified
+ * elements. The component type of the returned array is always the same
+ * as that of the input array.
+ *
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final boolean[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final boolean[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ boolean tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final byte[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final byte[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ byte tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final char[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final char[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ char tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final double[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final double[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ double tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final float[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final float[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ float tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final int[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final int[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ int tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final long[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final long[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ long tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * There is no special handling for multi-dimensional arrays.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final Object[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Under value (<0) is promoted to 0, over value (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Under value (< start index) results in no
+ * change. Over value (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final Object[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ Object tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Reverses the order of the given array.
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array the array to reverse, may be {@code null}
+ */
+ public static void reverse(final short[] array) {
+ if (array == null) {
+ return;
+ }
+ reverse(array, 0, array.length);
+ }
+
+ /**
+ *
+ * Reverses the order of the given array in the given range.
+ *
+ *
+ * This method does nothing for a {@code null} input array.
+ *
+ * @param array
+ * the array to reverse, may be {@code null}
+ * @param startIndexInclusive
+ * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no
+ * change.
+ * @param endIndexExclusive
+ * elements up to endIndex-1 are reversed in the array. Undervalue (< start index) results in no
+ * change. Overvalue (>array.length) is demoted to array length.
+ * @since 3.2
+ */
+ public static void reverse(final short[] array, final int startIndexInclusive, final int endIndexExclusive) {
+ if (array == null) {
+ return;
+ }
+ int i = Math.max(startIndexInclusive, 0);
+ int j = Math.min(array.length, endIndexExclusive) - 1;
+ short tmp;
+ while (j > i) {
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ j--;
+ i++;
+ }
+ }
+
+ /**
+ * Shifts the order of the given boolean array.
+ *
+ * There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for {@code null} or empty input arrays. Produces a new {@code boolean} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(boolean[], int, int)
+ */
+ public static boolean[] subarray(final boolean[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_BOOLEAN_ARRAY;
+ }
+
+ final boolean[] subarray = new boolean[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * Produces a new {@code byte} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(byte[], int, int)
+ */
+ public static byte[] subarray(final byte[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_BYTE_ARRAY;
+ }
+
+ final byte[] subarray = new byte[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * Produces a new {@code char} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(char[], int, int)
+ */
+ public static char[] subarray(final char[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_CHAR_ARRAY;
+ }
+
+ final char[] subarray = new char[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * Produces a new {@code double} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(double[], int, int)
+ */
+ public static double[] subarray(final double[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_DOUBLE_ARRAY;
+ }
+
+ final double[] subarray = new double[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * Produces a new {@code float} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(float[], int, int)
+ */
+ public static float[] subarray(final float[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_FLOAT_ARRAY;
+ }
+
+ final float[] subarray = new float[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * Produces a new {@code int} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(int[], int, int)
+ */
+ public static int[] subarray(final int[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_INT_ARRAY;
+ }
+
+ final int[] subarray = new int[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * Produces a new {@code long} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(long[], int, int)
+ */
+ public static long[] subarray(final long[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_LONG_ARRAY;
+ }
+
+ final long[] subarray = new long[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ /**
+ * Produces a new {@code short} array containing the elements
+ * between the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * @param array the array
+ * @param startIndexInclusive the starting index. Undervalue (<0)
+ * is promoted to 0, overvalue (>array.length) results
+ * in an empty array.
+ * @param endIndexExclusive elements up to endIndex-1 are present in the
+ * returned subarray. Undervalue (< startIndex) produces
+ * empty array, overvalue (>array.length) is demoted to
+ * array length.
+ * @return a new array containing the elements between
+ * the start and end indices.
+ * @since 2.1
+ * @see Arrays#copyOfRange(short[], int, int)
+ */
+ public static short[] subarray(final short[] array, int startIndexInclusive, int endIndexExclusive) {
+ if (array == null) {
+ return null;
+ }
+ if (startIndexInclusive < 0) {
+ startIndexInclusive = 0;
+ }
+ if (endIndexExclusive > array.length) {
+ endIndexExclusive = array.length;
+ }
+ final int newSize = endIndexExclusive - startIndexInclusive;
+ if (newSize <= 0) {
+ return EMPTY_SHORT_ARRAY;
+ }
+
+ final short[] subarray = new short[newSize];
+ System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
+ return subarray;
+ }
+
+ // Subarrays
+ //-----------------------------------------------------------------------
+ /**
+ * Produces a new array containing the elements between
+ * the start and end indices.
+ *
+ * The start index is inclusive, the end index exclusive.
+ * Null array input produces null output.
+ *
+ * The component type of the subarray is always the same as
+ * that of the input array. Thus, if the input is an array of type
+ * {@code Date}, the following usage is envisaged:
+ *
+ * There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. There is no special handling for multi-dimensional arrays. This method
+ * does nothing for a {@code null} or empty input array or for overflow indices.
+ * Negative indices are promoted to 0(zero). This method does nothing for a {@code null} or empty input array or
+ * for overflow indices. Negative indices are promoted to 0(zero). If any
+ * of the sub-arrays to swap falls outside of the given array, then the
+ * swap is stopped at the end of the array and as many as possible elements
+ * are swapped. Create a type-safe generic array.
+ *
+ * The Java language does not allow an array to be created from a generic type:
+ *
+ * Therefore new arrays of generic types can be created with this method.
+ * For example, an array of Strings can be created:
+ *
+ * The method is typically used in scenarios, where the caller itself uses generic types
+ * that have to be combined into an array.
+ *
+ * Note, this method makes only sense to provide arguments of the same type so that the
+ * compiler can deduce the type of the array itself. While it is possible to select the
+ * type explicitly like in
+ * {@code Number[] array = ArrayUtils.<Number>toArray(Integer.valueOf(42), Double.valueOf(Math.PI))},
+ * there is no real advantage when compared to
+ * {@code new Number[] {Integer.valueOf(42), Double.valueOf(Math.PI)}}.
+ *
+ * @param Converts the given array into a {@link java.util.Map}. Each element of the array
+ * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
+ * elements, where the first element is used as key and the second as
+ * value.
+ *
+ * This method can be used to initialize:
+ * This method returns {@code null} for a {@code null} input array.
+ *
+ * @param array an array whose elements are either a {@link java.util.Map.Entry} or
+ * an Array containing at least two elements, may be {@code null}
+ * @return a {@code Map} that was created from the array
+ * @throws IllegalArgumentException if one element of this Array is
+ * itself an Array containing less then two elements
+ * @throws IllegalArgumentException if the array contains elements other
+ * than {@link java.util.Map.Entry} and an Array
+ */
+ public static Map
+ * Static methods to convert a type into another, with endianness and bit ordering awareness.
+ *
+ * The methods names follow a naming rule:
+ * Source/destination type fields is one of the following:
+ *
+ * Endianness field: little endian is the default, in this case the field is absent. In case of
+ * big endian, the field is "Be".
+ * Example: intBeMsb0ToHex convert an int with big endian byte order and Msb0 bit order into its
+ * hexadecimal string representation
+ *
+ * Most of the methods provide only default encoding for destination, this limits the number of
+ * ways to do one thing. Unless you are dealing with data from/to outside of the JVM platform,
+ * you should not need to use "Be" and "Msb0" methods.
+ *
+ * Development status: work on going, only a part of the little endian, Lsb0 methods implemented
+ * so far.
+ *
+ * Converts a hexadecimal digit into an int using the default (Lsb0) bit ordering.
+ *
+ * '1' is converted to 1
+ *
+ * Converts a hexadecimal digit into an int using the Msb0 bit ordering.
+ *
+ * '1' is converted to 8
+ *
+ * Converts a hexadecimal digit into binary (represented as boolean array) using the default
+ * (Lsb0) bit ordering.
+ *
+ * '1' is converted as follow: (1, 0, 0, 0)
+ *
+ * Converts a hexadecimal digit into binary (represented as boolean array) using the Msb0
+ * bit ordering.
+ *
+ * '1' is converted as follow: (0, 0, 0, 1)
+ *
+ * Converts binary (represented as boolean array) to a hexadecimal digit using the default
+ * (Lsb0) bit ordering.
+ *
+ * (1, 0, 0, 0) is converted as follow: '1'
+ *
+ * Converts binary (represented as boolean array) to a hexadecimal digit using the default
+ * (Lsb0) bit ordering.
+ *
+ * (1, 0, 0, 0) is converted as follow: '1'
+ *
+ * Converts binary (represented as boolean array) to a hexadecimal digit using the Msb0 bit
+ * ordering.
+ *
+ * (1, 0, 0, 0) is converted as follow: '8'
+ *
+ * Converts binary (represented as boolean array) to a hexadecimal digit using the Msb0 bit
+ * ordering.
+ *
+ * (1, 0, 0, 0) is converted as follow: '8' (1, 0, 0, 1, 1, 0, 1, 0) with srcPos = 3 is converted
+ * to 'D'
+ *
+ * Converts the first 4 bits of a binary (represented as boolean array) in big endian Msb0
+ * bit ordering to a hexadecimal digit.
+ *
+ * (1, 0, 0, 0) is converted as follow: '8' (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0) is converted
+ * to '4'
+ *
+ * Converts a binary (represented as boolean array) in big endian Msb0 bit ordering to a
+ * hexadecimal digit.
+ *
+ * (1, 0, 0, 0) with srcPos = 0 is converted as follow: '8' (1, 0, 0, 0, 0, 0, 0, 0,
+ * 0, 0, 0, 1, 0, 1, 0, 0) with srcPos = 2 is converted to '5'
+ *
+ * Converts the 4 lsb of an int to a hexadecimal digit.
+ *
+ * 0 returns '0'
+ *
+ * 1 returns '1'
+ *
+ * 10 returns 'A' and so on...
+ *
+ * Converts the 4 lsb of an int to a hexadecimal digit encoded using the Msb0 bit ordering.
+ *
+ * 0 returns '0'
+ *
+ * 1 returns '8'
+ *
+ * 10 returns '5' and so on...
+ *
+ * Converts an array of int into a long using the default (little endian, Lsb0) byte and bit
+ * ordering.
+ *
+ * Converts an array of short into a long using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an array of short into an int using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an array of byte into a long using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an array of byte into an int using the default (little endian, Lsb0) byte and bit
+ * ordering.
+ *
+ * Converts an array of byte into a short using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an array of Char into a long using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an array of Char into an int using the default (little endian, Lsb0) byte and bit
+ * ordering.
+ *
+ * Converts an array of Char into a short using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an array of Char into a byte using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts binary (represented as boolean array) into a long using the default (little
+ * endian, Lsb0) byte and bit ordering.
+ *
+ * Converts binary (represented as boolean array) into an int using the default (little
+ * endian, Lsb0) byte and bit ordering.
+ *
+ * Converts binary (represented as boolean array) into a short using the default (little
+ * endian, Lsb0) byte and bit ordering.
+ *
+ * Converts binary (represented as boolean array) into a byte using the default (little
+ * endian, Lsb0) byte and bit ordering.
+ *
+ * Converts a long into an array of int using the default (little endian, Lsb0) byte and bit
+ * ordering.
+ *
+ * Converts a long into an array of short using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an int into an array of short using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts a long into an array of byte using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an int into an array of byte using the default (little endian, Lsb0) byte and bit
+ * ordering.
+ *
+ * Converts a short into an array of byte using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts a long into an array of Char using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an int into an array of Char using the default (little endian, Lsb0) byte and bit
+ * ordering.
+ *
+ * Converts a short into an array of Char using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts a byte into an array of Char using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts a long into an array of boolean using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts an int into an array of boolean using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts a short into an array of boolean using the default (little endian, Lsb0) byte
+ * and bit ordering.
+ *
+ * Converts a byte into an array of boolean using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ *
+ * Converts UUID into an array of byte using the default (little endian, Lsb0) byte and bit
+ * ordering.
+ *
+ * Converts bytes from an array into a UUID using the default (little endian, Lsb0) byte and
+ * bit ordering.
+ * Utility library to provide helper methods for Java enums. #ThreadSafe# Creates a long bit vector representation of the given array of Enum values. This generates a value that is usable by {@link EnumUtils#processBitVector}. Do not use this method if you have more than 64 values in your Enum, as this
+ * would create a value greater than a long can hold. Creates a long bit vector representation of the given subset of an Enum. This generates a value that is usable by {@link EnumUtils#processBitVector}. Do not use this method if you have more than 64 values in your Enum, as this
+ * would create a value greater than a long can hold. Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed. This generates a value that is usable by {@link EnumUtils#processBitVectors}. Use this method if you have more than 64 values in your Enum. Creates a bit vector representation of the given subset of an Enum using as many {@code long}s as needed. This generates a value that is usable by {@link EnumUtils#processBitVectors}. Use this method if you have more than 64 values in your Enum. Gets the enum for the class, returning {@code null} if not found. This method differs from {@link Enum#valueOf} in that it does not throw an exception
+ * for an invalid enum name. Gets the enum for the class, returning {@code defaultEnum} if not found. This method differs from {@link Enum#valueOf} in that it does not throw an exception
+ * for an invalid enum name. Gets the enum for the class, returning {@code null} if not found. This method differs from {@link Enum#valueOf} in that it does not throw an exception
+ * for an invalid enum name and performs case insensitive matching of the name. Gets the enum for the class, returning {@code defaultEnum} if not found. This method differs from {@link Enum#valueOf} in that it does not throw an exception
+ * for an invalid enum name and performs case insensitive matching of the name. Gets the {@code List} of enums. This method is useful when you need a list of enums rather than an array. Gets the {@code Map} of enums by name. This method is useful when you need a map of enums by name. Checks if the specified name is a valid enum for the class. This method differs from {@link Enum#valueOf} in that checks if the name is
+ * a valid enum without needing to catch the exception. Checks if the specified name is a valid enum for the class. This method differs from {@link Enum#valueOf} in that checks if the name is
+ * a valid enum without needing to catch the exception
+ * and performs case insensitive matching of the name. Convert a long value created by {@link EnumUtils#generateBitVector} into the set of
+ * enum values that it represents. If you store this value, beware any changes to the enum that would affect ordinal values. Convert a {@code long[]} created by {@link EnumUtils#generateBitVectors} into the set of
+ * enum values that it represents. If you store this value, beware any changes to the enum that would affect ordinal values.
+ * By replacing a {@link java.util.function.Consumer Consumer<O>} with a {@link FailableConsumer
+ * FailableConsumer<O,? extends Throwable>}, this can be written like follows:
+ *
+ * Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than the second
+ * version.
+ * TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function. TODO for 4.0: Move to org.apache.commons.lang3.function.
+ * Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a
+ * {@code RuntimeException} or {@code Error} then the argument will be rethrown without modification. If the
+ * exception is {@code IOException} then it will be wrapped into a {@code UncheckedIOException}. In every other
+ * cases the exception will be wrapped into a {@code
+ * UndeclaredThrowableException}
+ *
+ * Note that there is a declared return type for this method, even though it never returns. The reason for that is
+ * to support the usual pattern:
+ *
+ * instead of just calling the method. This pattern may help the Java compiler to recognize that at that point an
+ * exception will be thrown and the code flow analysis will not demand otherwise mandatory commands that could
+ * follow the method call, like a {@code return} statement from a value returning method.
+ * An enum representing all the versions of the Java specification.
+ * This is intended to mirror available values from the
+ *
+ * ArrayUtils.getLength(null) = 0
+ * ArrayUtils.getLength([]) = 0
+ * ArrayUtils.getLength([null]) = 1
+ * ArrayUtils.getLength([true, false]) = 2
+ * ArrayUtils.getLength([1, 2, 3]) = 3
+ * ArrayUtils.getLength(["a", "b", "c"]) = 3
+ *
+ *
+ * @param array the array to retrieve the length from, may be null
+ * @return The length of the array, or {@code 0} if the array is {@code null}
+ * @throws IllegalArgumentException if the object argument is not an array.
+ * @since 2.1
+ */
+ public static int getLength(final Object array) {
+ if (array == null) {
+ return 0;
+ }
+ return Array.getLength(array);
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static boolean[] insert(final int index, final boolean[] array, final boolean... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final boolean[] result = new boolean[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static byte[] insert(final int index, final byte[] array, final byte... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final byte[] result = new byte[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static char[] insert(final int index, final char[] array, final char... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final char[] result = new char[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static double[] insert(final int index, final double[] array, final double... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final double[] result = new double[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static float[] insert(final int index, final float[] array, final float... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final float[] result = new float[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static int[] insert(final int index, final int[] array, final int... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final int[] result = new int[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static long[] insert(final int index, final long[] array, final long... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final long[] result = new long[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param index the position within {@code array} to insert the new values
+ * @param array the array to insert the values into, may be {@code null}
+ * @param values the new values to insert, may be {@code null}
+ * @return The new array.
+ * @throws IndexOutOfBoundsException if {@code array} is provided
+ * and either {@code index < 0} or {@code index > array.length}
+ * @since 3.6
+ */
+ public static short[] insert(final int index, final short[] array, final short... values) {
+ if (array == null) {
+ return null;
+ }
+ if (ArrayUtils.isEmpty(values)) {
+ return clone(array);
+ }
+ if (index < 0 || index > array.length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + array.length);
+ }
+
+ final short[] result = new short[array.length + values.length];
+
+ System.arraycopy(values, 0, result, index, values.length);
+ if (index > 0) {
+ System.arraycopy(array, 0, result, 0, index);
+ }
+ if (index < array.length) {
+ System.arraycopy(array, index, result, index + values.length, array.length - index);
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.insert(index, null, null) = null
+ * ArrayUtils.insert(index, array, null) = cloned copy of 'array'
+ * ArrayUtils.insert(index, null, values) = null
+ *
+ *
+ * @param
+ * ArrayUtils.isArrayIndexValid(null, 0) = false
+ * ArrayUtils.isArrayIndexValid([], 0) = false
+ * ArrayUtils.isArrayIndexValid(["a"], 0) = true
+ *
+ *
+ * @param
+ * ArrayUtils.remove([true], 0) = []
+ * ArrayUtils.remove([true, false], 0) = [false]
+ * ArrayUtils.remove([true, false], 1) = [true]
+ * ArrayUtils.remove([true, true, false], 1) = [true, false]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static boolean[] remove(final boolean[] array, final int index) {
+ return (boolean[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove([1], 0) = []
+ * ArrayUtils.remove([1, 0], 0) = [0]
+ * ArrayUtils.remove([1, 0], 1) = [1]
+ * ArrayUtils.remove([1, 0, 1], 1) = [1, 1]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static byte[] remove(final byte[] array, final int index) {
+ return (byte[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove(['a'], 0) = []
+ * ArrayUtils.remove(['a', 'b'], 0) = ['b']
+ * ArrayUtils.remove(['a', 'b'], 1) = ['a']
+ * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c']
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static char[] remove(final char[] array, final int index) {
+ return (char[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove([1.1], 0) = []
+ * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
+ * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
+ * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static double[] remove(final double[] array, final int index) {
+ return (double[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove([1.1], 0) = []
+ * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
+ * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
+ * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static float[] remove(final float[] array, final int index) {
+ return (float[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove([1], 0) = []
+ * ArrayUtils.remove([2, 6], 0) = [6]
+ * ArrayUtils.remove([2, 6], 1) = [2]
+ * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static int[] remove(final int[] array, final int index) {
+ return (int[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove([1], 0) = []
+ * ArrayUtils.remove([2, 6], 0) = [6]
+ * ArrayUtils.remove([2, 6], 1) = [2]
+ * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static long[] remove(final long[] array, final int index) {
+ return (long[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove([1], 0) = []
+ * ArrayUtils.remove([2, 6], 0) = [6]
+ * ArrayUtils.remove([2, 6], 1) = [2]
+ * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param index the position of the element to be removed
+ * @return A new array containing the existing elements except the element
+ * at the specified position.
+ * @throws IndexOutOfBoundsException if the index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 2.1
+ */
+ public static short[] remove(final short[] array, final int index) {
+ return (short[]) remove((Object) array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.remove(["a"], 0) = []
+ * ArrayUtils.remove(["a", "b"], 0) = ["b"]
+ * ArrayUtils.remove(["a", "b"], 1) = ["a"]
+ * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
+ *
+ *
+ * @param
+ * ArrayUtils.removeAll([true, false, true], 0, 2) = [false]
+ * ArrayUtils.removeAll([true, false, true], 1, 2) = [true]
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static boolean[] removeAll(final boolean[] array, final int... indices) {
+ return (boolean[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static byte[] removeAll(final byte[] array, final int... indices) {
+ return (byte[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static char[] removeAll(final char[] array, final int... indices) {
+ return (char[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static double[] removeAll(final double[] array, final int... indices) {
+ return (double[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static float[] removeAll(final float[] array, final int... indices) {
+ return (float[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static int[] removeAll(final int[] array, final int... indices) {
+ return (int[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static long[] removeAll(final long[] array, final int... indices) {
+ return (long[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ * Removes multiple array elements specified by indices.
+ *
+ * @param array source
+ * @param indices to remove
+ * @return new array of same type minus elements specified by the set bits in {@code indices}
+ * @since 3.2
+ */
+ // package protected for access by unit tests
+ static Object removeAll(final Object array, final BitSet indices) {
+ if (array == null) {
+ return null;
+ }
+
+ final int srcLength = getLength(array);
+ // No need to check maxIndex here, because method only currently called from removeElements()
+ // which guarantee to generate only valid bit entries.
+// final int maxIndex = indices.length();
+// if (maxIndex > srcLength) {
+// throw new IndexOutOfBoundsException("Index: " + (maxIndex-1) + ", Length: " + srcLength);
+// }
+ final int removals = indices.cardinality(); // true bits are items to remove
+ final Object result = Array.newInstance(array.getClass().getComponentType(), srcLength - removals);
+ int srcIndex = 0;
+ int destIndex = 0;
+ int count;
+ int set;
+ while ((set = indices.nextSetBit(srcIndex)) != -1) {
+ count = set - srcIndex;
+ if (count > 0) {
+ System.arraycopy(array, srcIndex, result, destIndex, count);
+ destIndex += count;
+ }
+ srcIndex = indices.nextClearBit(set);
+ }
+ count = srcLength - srcIndex;
+ if (count > 0) {
+ System.arraycopy(array, srcIndex, result, destIndex, count);
+ }
+ return result;
+ }
+
+ /**
+ * Removes multiple array elements specified by index.
+ * @param array source
+ * @param indices to remove
+ * @return new array of same type minus elements specified by unique values of {@code indices}
+ * @since 3.0.1
+ */
+ // package protected for access by unit tests
+ static Object removeAll(final Object array, final int... indices) {
+ final int length = getLength(array);
+ int diff = 0; // number of distinct indexes, i.e. number of entries that will be removed
+ final int[] clonedIndices = ArraySorter.sort(clone(indices));
+
+ // identify length of result array
+ if (isNotEmpty(clonedIndices)) {
+ int i = clonedIndices.length;
+ int prevIndex = length;
+ while (--i >= 0) {
+ final int index = clonedIndices[i];
+ if (index < 0 || index >= length) {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
+ }
+ if (index >= prevIndex) {
+ continue;
+ }
+ diff++;
+ prevIndex = index;
+ }
+ }
+
+ // create result array
+ final Object result = Array.newInstance(array.getClass().getComponentType(), length - diff);
+ if (diff < length) {
+ int end = length; // index just after last copy
+ int dest = length - diff; // number of entries so far not copied
+ for (int i = clonedIndices.length - 1; i >= 0; i--) {
+ final int index = clonedIndices[i];
+ if (end - index > 1) { // same as (cp > 0)
+ final int cp = end - index - 1;
+ dest -= cp;
+ System.arraycopy(array, index + 1, result, dest, cp);
+ // After this copy, we still have room for dest items.
+ }
+ end = index;
+ }
+ if (end > 0) {
+ System.arraycopy(array, 0, result, 0, end);
+ }
+ }
+ return result;
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll([1], 0) = []
+ * ArrayUtils.removeAll([2, 6], 0) = [6]
+ * ArrayUtils.removeAll([2, 6], 0, 1) = []
+ * ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
+ * ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
+ *
+ *
+ * @param array the array to remove the element from, may not be {@code null}
+ * @param indices the positions of the elements to be removed
+ * @return A new array containing the existing elements except those
+ * at the specified positions.
+ * @throws IndexOutOfBoundsException if any index is out of range
+ * (index < 0 || index >= array.length), or if the array is {@code null}.
+ * @since 3.0.1
+ */
+ public static short[] removeAll(final short[] array, final int... indices) {
+ return (short[]) removeAll((Object) array, indices);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeAll(["a", "b", "c"], 0, 2) = ["b"]
+ * ArrayUtils.removeAll(["a", "b", "c"], 1, 2) = ["a"]
+ *
+ *
+ * @param
+ * ArrayUtils.removeElement(null, true) = null
+ * ArrayUtils.removeElement([], true) = []
+ * ArrayUtils.removeElement([true], false) = [true]
+ * ArrayUtils.removeElement([true, false], false) = [true]
+ * ArrayUtils.removeElement([true, false, true], true) = [false, true]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static boolean[] removeElement(final boolean[] array, final boolean element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, 1) = null
+ * ArrayUtils.removeElement([], 1) = []
+ * ArrayUtils.removeElement([1], 0) = [1]
+ * ArrayUtils.removeElement([1, 0], 0) = [1]
+ * ArrayUtils.removeElement([1, 0, 1], 1) = [0, 1]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static byte[] removeElement(final byte[] array, final byte element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, 'a') = null
+ * ArrayUtils.removeElement([], 'a') = []
+ * ArrayUtils.removeElement(['a'], 'b') = ['a']
+ * ArrayUtils.removeElement(['a', 'b'], 'a') = ['b']
+ * ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static char[] removeElement(final char[] array, final char element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, 1.1) = null
+ * ArrayUtils.removeElement([], 1.1) = []
+ * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
+ * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
+ * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static double[] removeElement(final double[] array, final double element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, 1.1) = null
+ * ArrayUtils.removeElement([], 1.1) = []
+ * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
+ * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
+ * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static float[] removeElement(final float[] array, final float element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, 1) = null
+ * ArrayUtils.removeElement([], 1) = []
+ * ArrayUtils.removeElement([1], 2) = [1]
+ * ArrayUtils.removeElement([1, 3], 1) = [3]
+ * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static int[] removeElement(final int[] array, final int element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, 1) = null
+ * ArrayUtils.removeElement([], 1) = []
+ * ArrayUtils.removeElement([1], 2) = [1]
+ * ArrayUtils.removeElement([1, 3], 1) = [3]
+ * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static long[] removeElement(final long[] array, final long element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, 1) = null
+ * ArrayUtils.removeElement([], 1) = []
+ * ArrayUtils.removeElement([1], 2) = [1]
+ * ArrayUtils.removeElement([1, 3], 1) = [3]
+ * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param element the element to be removed
+ * @return A new array containing the existing elements except the first
+ * occurrence of the specified element.
+ * @since 2.1
+ */
+ public static short[] removeElement(final short[] array, final short element) {
+ final int index = indexOf(array, element);
+ if (index == INDEX_NOT_FOUND) {
+ return clone(array);
+ }
+ return remove(array, index);
+ }
+
+ /**
+ *
+ * ArrayUtils.removeElement(null, "a") = null
+ * ArrayUtils.removeElement([], "a") = []
+ * ArrayUtils.removeElement(["a"], "b") = ["a"]
+ * ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
+ * ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
+ *
+ *
+ * @param
+ * ArrayUtils.removeElements(null, true, false) = null
+ * ArrayUtils.removeElements([], true, false) = []
+ * ArrayUtils.removeElements([true], false, false) = [true]
+ * ArrayUtils.removeElements([true, false], true, true) = [false]
+ * ArrayUtils.removeElements([true, false, true], true) = [false, true]
+ * ArrayUtils.removeElements([true, false, true], true, true) = [false]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static boolean[] removeElements(final boolean[] array, final boolean... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final HashMap
+ * ArrayUtils.removeElements(null, 1, 2) = null
+ * ArrayUtils.removeElements([], 1, 2) = []
+ * ArrayUtils.removeElements([1], 2, 3) = [1]
+ * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
+ * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
+ * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static byte[] removeElements(final byte[] array, final byte... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final Map
+ * ArrayUtils.removeElements(null, 1, 2) = null
+ * ArrayUtils.removeElements([], 1, 2) = []
+ * ArrayUtils.removeElements([1], 2, 3) = [1]
+ * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
+ * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
+ * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static char[] removeElements(final char[] array, final char... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final HashMap
+ * ArrayUtils.removeElements(null, 1, 2) = null
+ * ArrayUtils.removeElements([], 1, 2) = []
+ * ArrayUtils.removeElements([1], 2, 3) = [1]
+ * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
+ * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
+ * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static double[] removeElements(final double[] array, final double... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final HashMap
+ * ArrayUtils.removeElements(null, 1, 2) = null
+ * ArrayUtils.removeElements([], 1, 2) = []
+ * ArrayUtils.removeElements([1], 2, 3) = [1]
+ * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
+ * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
+ * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static float[] removeElements(final float[] array, final float... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final HashMap
+ * ArrayUtils.removeElements(null, 1, 2) = null
+ * ArrayUtils.removeElements([], 1, 2) = []
+ * ArrayUtils.removeElements([1], 2, 3) = [1]
+ * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
+ * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
+ * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static int[] removeElements(final int[] array, final int... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final HashMap
+ * ArrayUtils.removeElements(null, 1, 2) = null
+ * ArrayUtils.removeElements([], 1, 2) = []
+ * ArrayUtils.removeElements([1], 2, 3) = [1]
+ * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
+ * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
+ * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static long[] removeElements(final long[] array, final long... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final HashMap
+ * ArrayUtils.removeElements(null, 1, 2) = null
+ * ArrayUtils.removeElements([], 1, 2) = []
+ * ArrayUtils.removeElements([1], 2, 3) = [1]
+ * ArrayUtils.removeElements([1, 3], 1, 2) = [3]
+ * ArrayUtils.removeElements([1, 3, 1], 1) = [3, 1]
+ * ArrayUtils.removeElements([1, 3, 1], 1, 1) = [3]
+ *
+ *
+ * @param array the array to remove the element from, may be {@code null}
+ * @param values the elements to be removed
+ * @return A new array containing the existing elements except the
+ * earliest-encountered occurrences of the specified elements.
+ * @since 3.0.1
+ */
+ public static short[] removeElements(final short[] array, final short... values) {
+ if (isEmpty(array) || isEmpty(values)) {
+ return clone(array);
+ }
+ final HashMap
+ * ArrayUtils.removeElements(null, "a", "b") = null
+ * ArrayUtils.removeElements([], "a", "b") = []
+ * ArrayUtils.removeElements(["a"], "b", "c") = ["a"]
+ * ArrayUtils.removeElements(["a", "b"], "a", "c") = ["b"]
+ * ArrayUtils.removeElements(["a", "b", "a"], "a") = ["b", "a"]
+ * ArrayUtils.removeElements(["a", "b", "a"], "a", "a") = ["b"]
+ *
+ *
+ * @param
+ * Date[] someDates = (Date[]) ArrayUtils.subarray(allDates, 2, 5);
+ *
+ *
+ * @param
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final boolean[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given boolean array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final boolean[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final boolean aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+
+ /**
+ * Swaps two elements in the given byte array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final byte[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given byte array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final byte[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final byte aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+ /**
+ * Swaps two elements in the given char array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final char[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given char array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final char[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final char aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+ /**
+ * Swaps two elements in the given double array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final double[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given double array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final double[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final double aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+ /**
+ * Swaps two elements in the given float array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final float[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given float array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final float[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final float aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+
+ }
+
+ /**
+ * Swaps two elements in the given int array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final int[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given int array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final int[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final int aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+ /**
+ * Swaps two elements in the given long array.
+ *
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final long[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given long array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final long[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final long aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+ // Swap
+ //-----------------------------------------------------------------------
+ /**
+ * Swaps two elements in the given array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final Object[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final Object[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final Object aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+ /**
+ * Swaps two elements in the given short array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset2 the index of the second element to swap
+ * @since 3.5
+ */
+ public static void swap(final short[] array, final int offset1, final int offset2) {
+ if (isEmpty(array)) {
+ return;
+ }
+ swap(array, offset1, offset2, 1);
+ }
+
+ /**
+ * Swaps a series of elements in the given short array.
+ *
+ *
+ *
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset2 the index of the second element in the series to swap
+ * @param len the number of elements to swap starting with the given indices
+ * @since 3.5
+ */
+ public static void swap(final short[] array, int offset1, int offset2, int len) {
+ if (isEmpty(array) || offset1 >= array.length || offset2 >= array.length) {
+ return;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ if (offset1 == offset2) {
+ return;
+ }
+ len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
+ for (int i = 0; i < len; i++, offset1++, offset2++) {
+ final short aux = array[offset1];
+ array[offset1] = array[offset2];
+ array[offset2] = aux;
+ }
+ }
+
+ // Generic array
+ //-----------------------------------------------------------------------
+ /**
+ *
+ public static <T> T[] createAnArray(int size) {
+ return new T[size]; // compiler error here
+ }
+ public static <T> T[] createAnArray(int size) {
+ return (T[]) new Object[size]; // ClassCastException at runtime
+ }
+ *
+ *
+ *
+ String[] array = ArrayUtils.toArray("1", "2");
+ String[] emptyArray = ArrayUtils.<String>toArray();
+ *
+ *
+ *
+ * // Create a Map mapping colors.
+ * Map colorMap = ArrayUtils.toMap(new String[][] {
+ * {"RED", "#FF0000"},
+ * {"GREEN", "#00FF00"},
+ * {"BLUE", "#0000FF"}});
+ *
+ *
+ *
+ * {@code
+ *
+ *
Bit ordering: Lsb0 is the default, in this case the field
+ * is absent. In case of Msb0, the field is "Msb0".
+ *
+ * {@code
+ * Consumer
+ *
+ *
+ * {@code
+ * Functions.accept((m) -> m.invoke(o,args));
+ * }
+ *
+ *
+ * throw rethrow(myUncheckedException);
+ *
+ *
+ * Functions.stream(collection.stream());
+ *
+ * @param collection The collection, which is being converted into a {@link FailableStream}.
+ * @param
+ * {@code
+ * final FileInputStream fis = new FileInputStream("my.file");
+ * Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
+ * }
+ *
+ * @param action The action to execute. This object will always be invoked.
+ * @param errorHandler An optional error handler, which will be invoked finally, if any error occurred. The error
+ * handler will receive the first error, AKA {@link Throwable}.
+ * @param resources The resource actions to execute. All resource actions will be invoked, in the given
+ * order. A resource action is an instance of {@link FailableRunnable}, which will be executed.
+ * @see #tryWithResources(FailableRunnable, FailableRunnable...)
+ */
+ @SafeVarargs
+ public static void tryWithResources(final FailableRunnable extends Throwable> action,
+ final FailableConsumer
+ * {@code
+ * final FileInputStream fis = new FileInputStream("my.file");
+ * Functions.tryWithResources(useInputStream(fis), () -> fis.close());
+ * }
+ *
+ * @param action The action to execute. This object will always be invoked.
+ * @param resources The resource actions to execute. All resource actions will be invoked, in the given
+ * order. A resource action is an instance of {@link FailableRunnable}, which will be executed.
+ * @see #tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
+ */
+ @SafeVarargs
+ public static void tryWithResources(final FailableRunnable extends Throwable> action,
+ final FailableRunnable extends Throwable>... resources) {
+ tryWithResources(action, null, resources);
+ }
+}
diff --git a/before/src/main/java/org/apache/commons/lang3/JavaVersion.java b/before/src/main/java/org/apache/commons/lang3/JavaVersion.java
new file mode 100644
index 0000000..7b8f729
--- /dev/null
+++ b/before/src/main/java/org/apache/commons/lang3/JavaVersion.java
@@ -0,0 +1,324 @@
+/*
+ * 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 org.apache.commons.lang3;
+
+import org.apache.commons.lang3.math.NumberUtils;
+
+/**
+ *