Package org.esa.snap.core.util
Class ArrayUtils
java.lang.Object
org.esa.snap.core.util.ArrayUtils
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 booleanequalArrays(double[] array1, double[] array2, double eps) Indicates whether the given arrays arrays are "equal to" each other.static booleanequalArrays(float[] array1, float[] array2, float eps) Indicates whether the given arrays arrays are "equal to" each other.static booleanequalArrays(Object[] array1, Object[] array2) Indicates whether the given objects arrays are "equal to" each other.static intgetElementIndex(Object element, Object[] array) Returns the index of the specified object within the given object array.static booleanisMemberOf(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 newdoublearray of the given length.static float[]recycleOrCreateArray(float[] array, int length) Recycles or creates a newfloatarray of the given length.static int[]recycleOrCreateArray(int[] array, int length) Recycles or creates a newintarray of the given length.static voidswapArray(byte[] array) Swaps the content of the given array ofbytes.static voidswapArray(char[] array) Swaps the content of the given array ofchars.static voidswapArray(double[] array) Swaps the content of the given array ofdoubles.static voidswapArray(float[] array) Swaps the content of the given array offloats.static voidswapArray(int[] array) Swaps the content of the given array ofints.static voidswapArray(long[] array) Swaps the content of the given array oflongs.static voidswapArray(short[] array) Swaps the content of the given array ofshorts.static voidSwaps the content of the given array.static voidSwaps the content of the given array ofObjects.
-
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 benullarray2- the second array, can also benulleps- the maximum allowed absolute difference between the elements in both arrays- Returns:
trueif each element in the first object array equals each element in the second, in fact in the same order;falseotherwise.
-
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 benullarray2- the second array, can also benulleps- the maximum allowed absolute difference between the elements in both arrays- Returns:
trueif each element in the first object array equals each element in the second, in fact in the same order;falseotherwise.
-
equalArrays
Indicates whether the given objects arrays are "equal to" each other.This method should be used in place of the
Object.equalsif one ore both arguments can benulland if an element-by-element comparision shall be performed, since this is what this method does: for each element pair theObjectUtils.equalObjectsmethod is called.- Parameters:
array1- the first object array, can benullarray2- the second object array, can also benull- Returns:
trueif each element in the first object array equals each element in the second, in fact in the same order;falseotherwise.
-
getElementIndex
Returns the index of the specified object within the given object array.The method calls the
ObjectUtils.equalObjectswith 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 searchedarray- the array in which to search the element, must not benull- Returns:
- the array index in the range
0toarray.length - 1if the element was found,-1otherwise
-
isMemberOf
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 searchedarray- the array in which to search the element, must not benull- Returns:
trueif the given object is member of the specified array
-
swapArray
public static void swapArray(byte[] array) Swaps the content of the given array ofbytes.- Parameters:
array- the array ofbytes- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
public static void swapArray(char[] array) Swaps the content of the given array ofchars.- Parameters:
array- the array ofchars- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
public static void swapArray(short[] array) Swaps the content of the given array ofshorts.- Parameters:
array- the array ofshorts- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
public static void swapArray(int[] array) Swaps the content of the given array ofints.- Parameters:
array- the array ofints- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
public static void swapArray(long[] array) Swaps the content of the given array oflongs.- Parameters:
array- the array oflongs- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
public static void swapArray(float[] array) Swaps the content of the given array offloats.- Parameters:
array- the array offloats- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
public static void swapArray(double[] array) Swaps the content of the given array ofdoubles.- Parameters:
array- the array ofdoubles- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
Swaps the content of the given array ofObjects.- Parameters:
array- the array ofObjects- Throws:
IllegalArgumentException- if the given array isnull
-
swapArray
Swaps the content of the given array.- Parameters:
array- the array, must be an instance of a native array type such asfloat[]- Throws:
IllegalArgumentException- if the given array isnullor does not have a native array type
-
recycleOrCreateArray
public static int[] recycleOrCreateArray(int[] array, int length) Recycles or creates a newintarray of the given length.- Parameters:
array- an array which can possibly be recycledlength- 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 newfloatarray of the given length.- Parameters:
array- an array which can possibly be recycledlength- 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 newdoublearray of the given length.- Parameters:
array- an array which can possibly be recycledlength- 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
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 isnull
-
addArrays
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 arenull
-
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 minimummax- the given maximum- Returns:
- an int array which containes all values between the given min and the given max.
-