Class ArrayUtils

java.lang.Object
org.esa.snap.core.util.ArrayUtils

public class ArrayUtils extends Object
A utility class providing a set of static functions frequently used when working with basic Java arrays.

All functions have been implemented with extreme caution in order to provide a maximum performance.

Version:
$Revision$ $Date$
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int[]
    addArrays(int[] firstArray, int[] secondArray)
    Gives a new int array who contains both, all ints form all the given arrays.
    static int[]
    addToArray(int[] array, int value)
    Gives a new int array who contains both, all ints form the given array and the given new value.
    static int[]
    createIntArray(int min, int max)
    Creates an int array which containes all values between the given min and the given max.
    static boolean
    equalArrays(double[] array1, double[] array2, double eps)
    Indicates whether the given arrays arrays are "equal to" each other.
    static boolean
    equalArrays(float[] array1, float[] array2, float eps)
    Indicates whether the given arrays arrays are "equal to" each other.
    static boolean
    equalArrays(Object[] array1, Object[] array2)
    Indicates whether the given objects arrays are "equal to" each other.
    static int
    getElementIndex(Object element, Object[] array)
    Returns the index of the specified object within the given object array.
    static boolean
    isMemberOf(Object element, Object[] array)
    Checks if the given object is member of the specified array.
    static double[]
    recycleOrCreateArray(double[] array, int length)
    Recycles or creates a new double array of the given length.
    static float[]
    recycleOrCreateArray(float[] array, int length)
    Recycles or creates a new float array of the given length.
    static int[]
    recycleOrCreateArray(int[] array, int length)
    Recycles or creates a new int array of the given length.
    static void
    swapArray(byte[] array)
    Swaps the content of the given array of bytes.
    static void
    swapArray(char[] array)
    Swaps the content of the given array of chars.
    static void
    swapArray(double[] array)
    Swaps the content of the given array of doubles.
    static void
    swapArray(float[] array)
    Swaps the content of the given array of floats.
    static void
    swapArray(int[] array)
    Swaps the content of the given array of ints.
    static void
    swapArray(long[] array)
    Swaps the content of the given array of longs.
    static void
    swapArray(short[] array)
    Swaps the content of the given array of shorts.
    static void
    Swaps the content of the given array.
    static void
    swapArray(Object[] array)
    Swaps the content of the given array of Objects.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ArrayUtils

      public ArrayUtils()
  • Method Details

    • equalArrays

      public static boolean equalArrays(float[] array1, float[] array2, float eps)
      Indicates whether the given arrays arrays are "equal to" each other.

      Parameters:
      array1 - the first array, can be null
      array2 - the second array, can also be null
      eps - the maximum allowed absolute difference between the elements in both arrays
      Returns:
      true if each element in the first object array equals each element in the second, in fact in the same order; false otherwise.
    • equalArrays

      public static boolean equalArrays(double[] array1, double[] array2, double eps)
      Indicates whether the given arrays arrays are "equal to" each other.

      Parameters:
      array1 - the first array, can be null
      array2 - the second array, can also be null
      eps - the maximum allowed absolute difference between the elements in both arrays
      Returns:
      true if each element in the first object array equals each element in the second, in fact in the same order; false otherwise.
    • equalArrays

      public static boolean equalArrays(Object[] array1, Object[] array2)
      Indicates whether the given objects arrays are "equal to" each other.

      This method should be used in place of the Object.equals if one ore both arguments can be null and if an element-by-element comparision shall be performed, since this is what this method does: for each element pair the ObjectUtils.equalObjects method is called.

      Parameters:
      array1 - the first object array, can be null
      array2 - the second object array, can also be null
      Returns:
      true if each element in the first object array equals each element in the second, in fact in the same order; false otherwise.
    • getElementIndex

      public static int getElementIndex(Object element, Object[] array)
      Returns the index of the specified object within the given object array.

      The method calls the ObjectUtils.equalObjects with the specified element on each of the array's elements. If both are equal, the index is immediately returned.

      Parameters:
      element - the element to be searched
      array - the array in which to search the element, must not be null
      Returns:
      the array index in the range 0 to array.length - 1 if the element was found, -1 otherwise
    • isMemberOf

      public static boolean isMemberOf(Object element, Object[] array)
      Checks if the given object is member of the specified array.

      The method simply returns getElementIndex(element, array) >= 0.

      Parameters:
      element - the element to be searched
      array - the array in which to search the element, must not be null
      Returns:
      true if the given object is member of the specified array
    • swapArray

      public static void swapArray(byte[] array)
      Swaps the content of the given array of bytes.
      Parameters:
      array - the array of bytes
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(char[] array)
      Swaps the content of the given array of chars.
      Parameters:
      array - the array of chars
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(short[] array)
      Swaps the content of the given array of shorts.
      Parameters:
      array - the array of shorts
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(int[] array)
      Swaps the content of the given array of ints.
      Parameters:
      array - the array of ints
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(long[] array)
      Swaps the content of the given array of longs.
      Parameters:
      array - the array of longs
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(float[] array)
      Swaps the content of the given array of floats.
      Parameters:
      array - the array of floats
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(double[] array)
      Swaps the content of the given array of doubles.
      Parameters:
      array - the array of doubles
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(Object[] array)
      Swaps the content of the given array of Objects.
      Parameters:
      array - the array of Objects
      Throws:
      IllegalArgumentException - if the given array is null
    • swapArray

      public static void swapArray(Object array)
      Swaps the content of the given array.
      Parameters:
      array - the array, must be an instance of a native array type such as float[]
      Throws:
      IllegalArgumentException - if the given array is null or does not have a native array type
    • recycleOrCreateArray

      public static int[] recycleOrCreateArray(int[] array, int length)
      Recycles or creates a new int array of the given length.
      Parameters:
      array - an array which can possibly be recycled
      length - the requested array length
      Returns:
      if the given array is not null and has exactly the requested length the given array is returned, otherwise a new array is created
    • recycleOrCreateArray

      public static float[] recycleOrCreateArray(float[] array, int length)
      Recycles or creates a new float array of the given length.
      Parameters:
      array - an array which can possibly be recycled
      length - the requested array length
      Returns:
      if the given array is not null and has exactly the requested length the given array is returned, otherwise a new array is created
    • recycleOrCreateArray

      public static double[] recycleOrCreateArray(double[] array, int length)
      Recycles or creates a new double array of the given length.
      Parameters:
      array - an array which can possibly be recycled
      length - the requested array length
      Returns:
      if the given array is not null and has exactly the requested length the given array is returned, otherwise a new array is created
    • addToArray

      public static int[] addToArray(int[] array, int value) throws IllegalArgumentException
      Gives a new int array who contains both, all ints form the given array and the given new value. The given value was added to the end of array
      Returns:
      new int[] with all ints and the new value
      Throws:
      IllegalArgumentException - if the given array is null
    • addArrays

      public static int[] addArrays(int[] firstArray, int[] secondArray) throws IllegalArgumentException
      Gives a new int array who contains both, all ints form all the given arrays. The given second array was added to the end of th first array.
      Returns:
      new int[] with all ints of both arrays
      Throws:
      IllegalArgumentException - if any of the given arrays are null
    • createIntArray

      public static int[] createIntArray(int min, int max)
      Creates an int array which containes all values between the given min and the given max. If min and max are equal an int[] which only contains one value was returned.
      Parameters:
      min - the given minimum
      max - the given maximum
      Returns:
      an int array which containes all values between the given min and the given max.