Class ArraysExt<E>
- java.lang.Object
-
- eu.simuline.util.ArraysExt<E>
-
- Type Parameters:
E- The type of the entry of the array under consideration.
public final class ArraysExt<E> extends Object
An add on to the classArrays. Partially this is influenced byCollections.- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classArraysExt.ArrayComparator<O>Comparator class which implements a kind of lexical ordering on arrays based on the ordering of the components defined byatomic.
-
Constructor Summary
Constructors Modifier Constructor Description privateArraysExt()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static ObjectcreateUnWrappedEmptyArray(Object[] wrappedArray)Returns an empty array with type likewrappedArraywith basic type replaced by its wrapper.private static Object[]createWrappedEmptyArray(Object elemArray)Returns an empty array with type likeelemArraywith basic type replaced by its wrapper.static voidfill(Object[] array, Object obj)Replaces all of the elements of the specified array with the specified element.static Comparator<Object[]>getComparator(Comparator<Object> atomic)Returns a comparator of the classArraysExt.ArrayComparatorwhich implements a kind of lexical ordering on arrays based on the ordering of the components defined byatomic.static Object[]nCopies(int num, Object obj)Returns an array consisting of n copies of the specified object.static List<Object>recAsList(Object[] array)Turns the given array recursively into a hierarchy of nested lists.static ObjectrecAsList(Object source, Class<?> cls)Convertssourcewhich is typically an array, recursively into an nestedListwith the given atomic entry type using the specified caster.static ObjectrecAsList(Object source, Class<?> cls, Caster caster)Convertssourcewhich is typically an array, recursively into an nestedListwith the given atomic entry type using the specified caster.static voidreverse(Object[] array)Reverses the order of the elements in the specified array.static boolean[]toPrimitive(Boolean[] arr)Converts an array ofBooleans into an array of accordingbooleans.static byte[]toPrimitive(Byte[] arr)Converts an array ofBytes into an array of accordingbytes.static char[]toPrimitive(Character[] arr)Converts an array ofCharacters into an array of accordingchars.static double[]toPrimitive(Double[] arr)Converts an array ofDoubles into an array of accordingdoubles.static float[]toPrimitive(Float[] arr)Converts an array ofFloats into an array of accordingfloats.static int[]toPrimitive(Integer[] arr)Converts an array ofIntegers into an array of accordingints.static long[]toPrimitive(Long[] arr)Converts an array ofLongs into an array of accordinglongs.static short[]toPrimitive(Short[] arr)Converts an array ofShorts into an array of accordingshorts.static ObjectunWrapArray(Object[] wrappedArray)Returns an array which corresponds with the given one except that the entries are unwrapped.static Object[]wrapArray(Object elemArray)Returns an array which corresponds with the given one except that the entries are wrapped.
-
-
-
Field Detail
-
EMPTY
public static final Object[] EMPTY
The classdouble[][].
-
-
Method Detail
-
reverse
public static void reverse(Object[] array)
Reverses the order of the elements in the specified array. This method runs in linear time.- Parameters:
array- the array whose elements are to be reversed.
-
fill
public static void fill(Object[] array, Object obj)
Replaces all of the elements of the specified array with the specified element. This method runs in linear time.- Parameters:
array- the array to be filled with the specified element.obj- The element with which to fill the specified array.
-
nCopies
public static Object[] nCopies(int num, Object obj)
Returns an array consisting of n copies of the specified object. The newly allocated data object is tiny (it contains a single reference to the data object).- Parameters:
num- the number of elements in the returned list.obj- the element to appear repeatedly in the returned list.- Returns:
- an array consisting of n copies of the specified object.
- Throws:
IllegalArgumentException- if n < 0.
-
recAsList
public static List<Object> recAsList(Object[] array)
Turns the given array recursively into a hierarchy of nested lists. Note that unlike Arrays.asList, this is not a simple wrapper!- Parameters:
array- an array of objects which may in turn be arrays of objects.- Returns:
- a list
listof objects satisfyinglist.get(i) == array[i]ifarray[i]is not anObject[];list.get(i) == recAsList(array[i])otherwise. Note thatlistmay be a nested list. - See Also:
CollectionsExt.recToArray(List)
-
recAsList
public static Object recAsList(Object source, Class<?> cls)
Convertssourcewhich is typically an array, recursively into an nestedListwith the given atomic entry type using the specified caster. Note that ifsourceis not an array, it must be compatible withclsup to equivalence of basic classes and their wrappers and up to subclassing.- Parameters:
source- an arbitraryObject, but typically an array as e.g.Object[][]ordouble[].cls- Up to compatibility defined bycaster, the type of the entries of the return value. Recursive conversion from arrays to lists stops if an entry of type compatible withclsis found.Note that this may also be a primitive type such as
intor an array typeDouble[][][]orList[][][]or evenint[][][].- Returns:
-
if
clsandsourceare assignment compatible up to equivalence of basic classes and their wrappers,sourceis returned. -
if
clsandsourceare not compatible,sourcemust be an array; otherwise an exception is thrown.In the former case, a list
listof objects is returned satisfyinglist.size() == Array.getLength(source)andlist.get(i) == recAsList(Array.get(source,i),cls)for all valid indicesi.Note that the return value is either the result of a casting process or a list. In the latter case its entries are either lists themselves or again the result of a casting and so on. Clearly its atomic entries are always
Objects even when starting with sources of typeint[]for instance.
-
if
- Throws:
IllegalArgumentException- ifsourceis neither an array, nor assignment compatible withclsup to equivalence between basic types and their wrappers. Also ifsourceis an array, and this condition holds for some component. This is a recursive definition.
-
recAsList
public static Object recAsList(Object source, Class<?> cls, Caster caster)
Convertssourcewhich is typically an array, recursively into an nestedListwith the given atomic entry type using the specified caster. Note that ifsourceis not an array, it must be compatible withclswith respect tocaster.- Parameters:
source- an arbitraryObject, but typically an array as e.g.Object[][]ordouble[].cls- Up to compatibility defined bycaster, the type of the entries of the return value. Recursive conversion from arrays to lists stops if an entry of type compatible withclsis found.Note that this may also be a primitive type such as
intor an array typeDouble[][][]orList[][][]or evenint[][][].caster- performs the conversion of the top-level elements of thesource.- Returns:
-
if
clsandsourceare compatible (seeCaster.areCompatible(java.lang.Class<?>, java.lang.Object)), thecast ofsourceis returned.Note that compatibility is up to wrapping of elementary types and unwrapping of their wrappers. Arrays are converted to
Lists applying this method recursively to their entries. -
if
clsandsourceare not compatible,sourcemust be an array; otherwise an exception is thrown.In the former case, a list
listof objects is returned satisfyinglist.size() == Array.getLength(source)andlist.get(i) == recAsList(Array.get(source,i),.. ,caster)for all valid indicesi.Note that the return value is either the result of a casting process or a list. In the latter case its entries are either lists themselves or again the result of a casting and so on. Clearly its atomic entries are always
Objects even when starting with sources of typeint[]for instance.
-
if
- Throws:
IllegalArgumentException- ifsourceis neither an array, nor compatible withclswith respect tocaster. Also ifsourceis an array, and this condition holds for some component. This is a recursive definition.
-
createWrappedEmptyArray
private static Object[] createWrappedEmptyArray(Object elemArray)
Returns an empty array with type likeelemArraywith basic type replaced by its wrapper.- Parameters:
elemArray- a non-null array with elementary entry type such asdouble[][][].- Returns:
- an empty array with type like
elemArraywith basic type replaced by its wrapper. For examplecreateWrappedEmptyArray(new int[][][] {....})yieldsnew Integer[][][] {}. - Throws:
IllegalArgumentException- ifelemArrayis not an array or if its entry type is not elementary as e.g. fornew Integer(0)or fornew Integer[][] {}.NullPointerException- ifelemArrayisnull
-
createUnWrappedEmptyArray
private static Object createUnWrappedEmptyArray(Object[] wrappedArray)
Returns an empty array with type likewrappedArraywith basic type replaced by its wrapper.- Parameters:
wrappedArray- a non-null array with wrapper entry type such asDouble[][][].- Returns:
- an empty array with type like
wrapperArraywith basic type replaced by its wrapper. For examplecreateUnWrappedEmptyArray(new Integer[][][] {....})yieldsnew int[][][] {}. - Throws:
IllegalArgumentException- if the entry type ofwrappedArrayis no wrapper type as e.g. fornew int[][] {}.
-
wrapArray
public static Object[] wrapArray(Object elemArray)
Returns an array which corresponds with the given one except that the entries are wrapped.- Parameters:
elemArray- an array with elementary component type as e.g.new int[][] {}. May also benull.- Returns:
nullifelemArray == null. an array with the same length aselemArray. The type of the components is the wrapper type of the type of the components ofelemArray. Also the entries are those ofelemArrayjust wrapped.- Throws:
IllegalArgumentException- ifelemArrayis not an array or if its entry type is not elementary as e.g. fornew Integer(0)or fornew Integer[][] {}.
-
toPrimitive
public static double[] toPrimitive(Double[] arr)
Converts an array ofDoubles into an array of accordingdoubles.
-
toPrimitive
public static float[] toPrimitive(Float[] arr)
Converts an array ofFloats into an array of accordingfloats.
-
toPrimitive
public static long[] toPrimitive(Long[] arr)
Converts an array ofLongs into an array of accordinglongs.
-
toPrimitive
public static int[] toPrimitive(Integer[] arr)
Converts an array ofIntegers into an array of accordingints.
-
toPrimitive
public static short[] toPrimitive(Short[] arr)
Converts an array ofShorts into an array of accordingshorts.
-
toPrimitive
public static byte[] toPrimitive(Byte[] arr)
Converts an array ofBytes into an array of accordingbytes.
-
toPrimitive
public static boolean[] toPrimitive(Boolean[] arr)
Converts an array ofBooleans into an array of accordingbooleans.
-
toPrimitive
public static char[] toPrimitive(Character[] arr)
Converts an array ofCharacters into an array of accordingchars.
-
unWrapArray
public static Object unWrapArray(Object[] wrappedArray)
Returns an array which corresponds with the given one except that the entries are unwrapped.- Parameters:
wrappedArray- an array with a wrapper as component type as e.g.new Integer[][] {}. May also benull.- Returns:
nullifwrappedArray == null. an array with the same length aswrappedArray. The type of the components is the wrapped elementary type of the type of the components ofwrappedArray. Also the entries are those ofwrappedArrayjust unwrapped.- Throws:
IllegalArgumentException- if the entry type ofwrappedArrayis no wrapper as e.g. fornew int[][] {}and fornew Object[][] {}.
-
getComparator
public static Comparator<Object[]> getComparator(Comparator<Object> atomic)
Returns a comparator of the classArraysExt.ArrayComparatorwhich implements a kind of lexical ordering on arrays based on the ordering of the components defined byatomic.- Parameters:
atomic- aComparatorfor the components of arrays.- Returns:
- a
Comparatorfor arrays.
-
-