Package nom.tam.util

Class ArrayFuncs

java.lang.Object
nom.tam.util.ArrayFuncs

public final class ArrayFuncs extends Object
This is a package of static functions which perform computations on arrays. Generally these routines attempt to complete without throwing errors by ignoring data they cannot understand.
  • Method Details

    • arrayDescription

      public static String arrayDescription(Object o)
      Parameters:
      o - The array to be described.
      Returns:
      a description of an array (presumed rectangular).
    • computeLSize

      public static long computeLSize(Object o)
    • computeSize

      @Deprecated public static int computeSize(Object o)
      Deprecated.
      May silently underestimate the size if the size > 2 GB.
      Parameters:
      o - The object whose size is desired.
      Returns:
      Compute the size of an object. Note that this only handles arrays or scalars of the primitive objects and Strings. It returns 0 for any object array element it does not understand.
    • convertArray

      public static Object convertArray(Object array, Class<?> newType)
      Parameters:
      array - A possibly multidimensional array to be converted.
      newType - The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.
      Returns:
      Convert an array to a specified type. This method supports conversions only among the primitive numeric types.
    • convertArray

      public static Object convertArray(Object array, Class<?> newType, boolean reuse)
      Parameters:
      array - A possibly multidimensional array to be converted.
      newType - The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.
      reuse - If set, and the requested type is the same as the original, then the original is returned.
      Returns:
      Convert an array to a specified type. This method supports conversions only among the primitive numeric types.
    • copyArray

      public static void copyArray(Object original, Object copy)
      Copy one array into another. This function copies the contents of one array into a previously allocated array. The arrays must agree in type and size.
      Parameters:
      original - The array to be copied.
      copy - The array to be copied into. This array must already be fully allocated.
    • copyInto

      public static void copyInto(Object array, Object mimic)
      Copy an array into an array of a different type. The dimensions and dimensionalities of the two arrays should be the same.
      Parameters:
      array - The original array.
      mimic - The array mimicking the original.
    • curl

      public static Object curl(Object input, int[] dimens)
      Curl an input array up into a multi-dimensional array.
      Parameters:
      input - The one dimensional array to be curled.
      dimens - The desired dimensions
      Returns:
      The curled array.
    • deepClone

      public static Object deepClone(Object o)
      Parameters:
      o - The object to be copied.
      Returns:
      a deep clone of an Array or a standard clone of a scalar. The object may comprise arrays of any primitive type or any Object type which implements Cloneable. However, if the Object is some kind of collection, e.g., a Vector then only a shallow copy of that object is made. I.e., deep refers only to arrays.
    • flatten

      public static Object flatten(Object input)
      Given an array of arbitrary dimensionality .
      Parameters:
      input - The input array.
      Returns:
      the array flattened into a single dimension.
    • genericClone

      public static Object genericClone(Object o)
      Clone an Object if possible. This method returns an Object which is a clone of the input object. It checks if the method implements the Cloneable interface and then uses reflection to invoke the clone method. This can't be done directly since as far as the compiler is concerned the clone method for Object is protected and someone could implement Cloneable but leave the clone method protected. The cloning can fail in a variety of ways which are trapped so that it returns null instead. This method will generally create a shallow clone. If you wish a deep copy of an array the method deepClone should be used.
      Parameters:
      o - The object to be cloned.
      Returns:
      the clone
    • getBaseArray

      public static Object getBaseArray(Object o)
      This routine returns the base array of a multi-dimensional array. I.e., a one-d array of whatever the array is composed of. Note that arrays are not guaranteed to be rectangular, so this returns o[0][0]....
      Parameters:
      o - the multi-dimensional array
      Returns:
      base array of a multi-dimensional array.
    • getBaseClass

      public static Class<?> getBaseClass(Object o)
      This routine returns the base class of an object. This is just the class of the object for non-arrays.
      Parameters:
      o - array to get the base class from
      Returns:
      the base class of an array
    • getBaseLength

      public static int getBaseLength(Object o)
      This routine returns the size of the base element of an array.
      Parameters:
      o - The array object whose base length is desired.
      Returns:
      the size of the object in bytes, 0 if null, or -1 if not a primitive array.
    • getDimensions

      public static int[] getDimensions(Object o)
      Find the dimensions of an object. This method returns an integer array with the dimensions of the object o which should usually be an array. It returns an array of dimension 0 for scalar objects and it returns -1 for dimension which have not been allocated, e.g., int[][][] x = new int[100][][]; should return [100,-1,-1].
      Parameters:
      o - The object to get the dimensions of.
      Returns:
      the dimensions of an object
    • mimicArray

      public static Object mimicArray(Object array, Class<?> newType)
      Create an array of a type given by new type with the dimensionality given in array.
      Parameters:
      array - A possibly multidimensional array to be converted.
      newType - The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.
      Returns:
      the new array with same dimensions
    • nElements

      @Deprecated public static int nElements(Object o)
      Deprecated.
      May silently underestimate size if number is > 2 G.
      Parameters:
      o - the array to count the elements
      Returns:
      Count the number of elements in an array.
    • newInstance

      public static Object newInstance(Class<?> cl, int dim)
      Allocate an array dynamically. The Array.newInstance method does not throw an error when there is insufficient memory and silently returns a null.throws an OutOfMemoryError if insufficient space is available.
      Parameters:
      cl - The class of the array.
      dim - The dimension of the array.
      Returns:
      The allocated array.
    • newInstance

      public static Object newInstance(Class<?> cl, int[] dims)
      Allocate an array dynamically. The Array.newInstance method does not throw an error and silently returns a null.throws an OutOfMemoryError if insufficient space is available.
      Parameters:
      cl - The class of the array.
      dims - The dimensions of the array.
      Returns:
      The allocated array.
    • nLElements

      @Deprecated public static long nLElements(Object o)
      Deprecated.
      May silently underestimate size if number is > 2 G.
      Parameters:
      o - the array to count elements in
      Returns:
      Count the number of elements in an array.
    • reverseIndices

      public static int[] reverseIndices(int[] indices)
      Reverse an integer array. This can be especially useful when dealing with an array of indices in FITS order that you wish to have in Java order.
      Parameters:
      indices - the array to reverse
      Returns:
      the reversed array.