ArrayUtils provides static methods for manipulating arrays when using a tool such as java util ArrayList is inc
/* * ArrayUtils.java Created Aug 29, 2005 by Andrew Butler, PSL */ //package prisms.util; import java.lang.reflect.Array; /** * ArrayUtils provides some static methods for manipulating arrays easily when * using a tool such as {@link java.util.ArrayList} is inconvenient. */ final class ArrayUtils { /** * Gets the first element in the given array for which the * {@link EqualsChecker#equals(Object, Object)} returns true, or null if * this never occurs. The equals method is called with the element as the * first argument and the test parameter as the second. * * @param * The type of array to get an element of * @param array * The array to get the element of * @param test * The object to compare the others with. This may be null. * @param checker * The checker to determine which element to get * @return The first element in the array for which the * {@link EqualsChecker#equals(Object, Object)} returns true, or * null if this never occurs */ public static T get(T[] array, Object test, EqualsChecker checker) { for (T el : array) if (checker.equals(el, test)) return el; return null; } /** * Inserts an element into the array--for primitive types * * @param anArray * The array to insert into * @param anElement * The element to insert * @param anIndex * The index for the new element * @return The new array with all elements of anArray, but with * anElement inserted at index anIndex */ public static Object addP(Object anArray, Object anElement, int anIndex) { Object ret; int length; if (anArray == null) { if (anIndex != 0) throw new ArrayIndexOutOfBoundsException("Cannot set " + anIndex + " element in a null array"); ret = Array.newInstance(anElement.getClass(), 1); Array.set(ret, 0, anElement); return ret; } else { length = Array.getLength(anArray); ret = Array.newInstance(anArray.getClass().getComponentType(), length + 1); } System.arraycopy(anArray, 0, ret, 0, anIndex); put(ret, anElement, anIndex); System.arraycopy(anArray, anIndex, ret, anIndex + 1, length - anIndex); return ret; } /** * Inserts an element into the array * * @param * The type of the object array * @param anArray * The array to insert into * @param anElement * The element to insert * @param anIndex * The index for the new element * @return The new array with all elements of anArray, but with * anElement inserted at index anIndex */ public static T[] add(T[] anArray, T anElement, int anIndex) { T[] ret; if (anArray == null) { if (anIndex != 0) throw new ArrayIndexOutOfBoundsException("Cannot set " + anIndex + " element in a null array"); ret = (T[]) Array.newInstance(anElement.getClass(), 1); ret[0] = anElement; return ret; } else ret = (T[]) Array.newInstance( anArray.getClass().getComponentType(), anArray.length + 1); System.arraycopy(anArray, 0, ret, 0, anIndex); put(ret, anElement, anIndex); System.arraycopy(anArray, anIndex, ret, anIndex + 1, anArray.length - anIndex); return ret; } /** * @param anArray * The array to extend * @param anElement * The element to add into anArray * @return A new array of length anArray.length+1 whose * contents are those of anArray followed by * anElement */ public static Object addP(Object anArray, Object anElement) { int len = anArray == null ? 0 : Array.getLength(anArray); return addP(anArray, anElement, len); } /** * @param * The type of the array * @param anArray * The array to extend * @param anElement * The element to add into anArray * @return A new array of length anArray.length+1 whose * contents are those of anArray followed by * anElement */ public static T[] add(T[] anArray, T anElement) { int len = anArray == null ? 0 : Array.getLength(anArray); return add(anArray, anElement, len); } /** * Moves an element in an array from one index to another * * @param * The type of the array * @param anArray * The array to move an element within * @param from * The index of the element to move * @param to * The index to move the element to * @return The original array */ public static T[] move(T[] anArray, int from, int to) { final T element = anArray[from]; for (; from < to; from++) anArray[from] = anArray[from + 1]; for (; from > to; from--) anArray[from] = anArray[from - 1]; anArray[to] = element; return anArray; } /** * Moves an element in a primitive array from one index to another * * @param anArray * The array to move an element within * @param from * The index of the element to move * @param to * The index to move the element to * @return The original array */ public static Object moveP(Object anArray, int from, int to) { if (anArray instanceof Object[]) return move((Object[]) anArray, from, to); final Object element = Array.get(anArray, from); for (; from < to; from++) Array.set(anArray, from, Array.get(anArray, from + 1)); for (; from > to; from--) Array.set(anArray, from, Array.get(anArray, from - 1)); Array.set(anArray, to, element); return anArray; } private static void put(Object array, Object element, int index) { try { if (array instanceof Object[]) { try { ((Object[]) array)[index] = element; } catch (ArrayStoreException e) { throw new IllegalArgumentException(e.getMessage() + ": " + (element == null ? "null" : element.getClass() .getName()) + " into " + array.getClass().getName()); } } else { try { Array.set(array, index, element); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage() + ": " + (element == null ? "null" : element.getClass() .getName()) + " into " + array.getClass().getName(), e); } } } catch (ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException(index + " into " + Array.getLength(array)); } } /** * Merges all elements of a set of arrays into a single array with no * duplicates. For primitive types. * * @param type * The type of the result * @param arrays * The arrays to merge * @return A new array containing all elements of array1 and * all elements of array2 that are not present in * array1 * @throws NullPointerException * If either array is null * @throws ArrayStoreException * If elements in the arrays are incompatible with * type */ public static Object mergeInclusiveP(Class type, Object... arrays) { java.util.LinkedHashSet