Class 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 class Arrays. Partially this is influenced by Collections.
    Version:
    1.0
    Author:
    Ernst Reissner
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  ArraysExt.ArrayComparator<O>
      Comparator class which implements a kind of lexical ordering on arrays based on the ordering of the components defined by atomic.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Object[] EMPTY
      The class double[][].
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ArraysExt()  
    • Field Detail

      • EMPTY

        public static final Object[] EMPTY
        The class double[][].
    • Constructor Detail

      • ArraysExt

        private ArraysExt()
    • 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 list of objects satisfying list.get(i) == array[i] if array[i] is not an Object[]; list.get(i) == recAsList(array[i]) otherwise. Note that list may be a nested list.
        See Also:
        CollectionsExt.recToArray(List)
      • recAsList

        public static Object recAsList​(Object source,
                                       Class<?> cls)
        Converts source which is typically an array, recursively into an nested List with the given atomic entry type using the specified caster. Note that if source is not an array, it must be compatible with cls up to equivalence of basic classes and their wrappers and up to subclassing.
        Parameters:
        source - an arbitrary Object, but typically an array as e.g. Object[][] or double[].
        cls - Up to compatibility defined by caster, the type of the entries of the return value. Recursive conversion from arrays to lists stops if an entry of type compatible with cls is found.

        Note that this may also be a primitive type such as int or an array type Double[][][] or List[][][] or even int[][][].

        Returns:
        • if cls and source are assignment compatible up to equivalence of basic classes and their wrappers, source is returned.
        • if cls and source are not compatible, source must be an array; otherwise an exception is thrown.

          In the former case, a list list of objects is returned satisfying list.size() == Array.getLength(source) and list.get(i) == recAsList(Array.get(source,i),cls) for all valid indices i.

          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 type int[] for instance.

        Throws:
        IllegalArgumentException - if source is neither an array, nor assignment compatible with cls up to equivalence between basic types and their wrappers. Also if source is 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)
        Converts source which is typically an array, recursively into an nested List with the given atomic entry type using the specified caster. Note that if source is not an array, it must be compatible with cls with respect to caster.
        Parameters:
        source - an arbitrary Object, but typically an array as e.g. Object[][] or double[].
        cls - Up to compatibility defined by caster, the type of the entries of the return value. Recursive conversion from arrays to lists stops if an entry of type compatible with cls is found.

        Note that this may also be a primitive type such as int or an array type Double[][][] or List[][][] or even int[][][].

        caster - performs the conversion of the top-level elements of the source.
        Returns:
        • if cls and source are compatible (see Caster.areCompatible(java.lang.Class<?>, java.lang.Object)), the cast of source is 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 cls and source are not compatible, source must be an array; otherwise an exception is thrown.

          In the former case, a list list of objects is returned satisfying list.size() == Array.getLength(source) and list.get(i) == recAsList(Array.get(source,i),.. ,caster) for all valid indices i.

          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 type int[] for instance.

        Throws:
        IllegalArgumentException - if source is neither an array, nor compatible with cls with respect to caster. Also if source is 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 like elemArray with basic type replaced by its wrapper.
        Parameters:
        elemArray - a non-null array with elementary entry type such as double[][][].
        Returns:
        an empty array with type like elemArray with basic type replaced by its wrapper. For example createWrappedEmptyArray(new int[][][] {....}) yields new Integer[][][] {}.
        Throws:
        IllegalArgumentException - if elemArray is not an array or if its entry type is not elementary as e.g. for new Integer(0) or for new Integer[][] {}.
        NullPointerException - if elemArray is null
      • createUnWrappedEmptyArray

        private static Object createUnWrappedEmptyArray​(Object[] wrappedArray)
        Returns an empty array with type like wrappedArray with basic type replaced by its wrapper.
        Parameters:
        wrappedArray - a non-null array with wrapper entry type such as Double[][][].
        Returns:
        an empty array with type like wrapperArray with basic type replaced by its wrapper. For example createUnWrappedEmptyArray(new Integer[][][] {....}) yields new int[][][] {}.
        Throws:
        IllegalArgumentException - if the entry type of wrappedArray is no wrapper type as e.g. for new 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 be null.
        Returns:
        null if elemArray == null. an array with the same length as elemArray. The type of the components is the wrapper type of the type of the components of elemArray. Also the entries are those of elemArray just wrapped.
        Throws:
        IllegalArgumentException - if elemArray is not an array or if its entry type is not elementary as e.g. for new Integer(0) or for new Integer[][] {}.
      • toPrimitive

        public static double[] toPrimitive​(Double[] arr)
        Converts an array of Doubles into an array of according doubles.
      • toPrimitive

        public static float[] toPrimitive​(Float[] arr)
        Converts an array of Floats into an array of according floats.
      • toPrimitive

        public static long[] toPrimitive​(Long[] arr)
        Converts an array of Longs into an array of according longs.
      • toPrimitive

        public static int[] toPrimitive​(Integer[] arr)
        Converts an array of Integers into an array of according ints.
      • toPrimitive

        public static short[] toPrimitive​(Short[] arr)
        Converts an array of Shorts into an array of according shorts.
      • toPrimitive

        public static byte[] toPrimitive​(Byte[] arr)
        Converts an array of Bytes into an array of according bytes.
      • toPrimitive

        public static boolean[] toPrimitive​(Boolean[] arr)
        Converts an array of Booleans into an array of according booleans.
      • toPrimitive

        public static char[] toPrimitive​(Character[] arr)
        Converts an array of Characters into an array of according chars.
      • 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 be null.
        Returns:
        null if wrappedArray == null. an array with the same length as wrappedArray. The type of the components is the wrapped elementary type of the type of the components of wrappedArray. Also the entries are those of wrappedArray just unwrapped.
        Throws:
        IllegalArgumentException - if the entry type of wrappedArray is no wrapper as e.g. for new int[][] {} and for new Object[][] {}.
      • getComparator

        public static Comparator<Object[]> getComparator​(Comparator<Object> atomic)
        Returns a comparator of the class ArraysExt.ArrayComparator which implements a kind of lexical ordering on arrays based on the ordering of the components defined by atomic.
        Parameters:
        atomic - a Comparator for the components of arrays.
        Returns:
        a Comparator for arrays.