Package org.esa.snap.core.util
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 ArrayUtils()
-
Method Summary
All Methods Static Methods Concrete Methods 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 newdouble
array of the given length.static float[]
recycleOrCreateArray(float[] array, int length)
Recycles or creates a newfloat
array of the given length.static int[]
recycleOrCreateArray(int[] array, int length)
Recycles or creates a newint
array of the given length.static void
swapArray(byte[] array)
Swaps the content of the given array ofbyte
s.static void
swapArray(char[] array)
Swaps the content of the given array ofchar
s.static void
swapArray(double[] array)
Swaps the content of the given array ofdouble
s.static void
swapArray(float[] array)
Swaps the content of the given array offloat
s.static void
swapArray(int[] array)
Swaps the content of the given array ofint
s.static void
swapArray(long[] array)
Swaps the content of the given array oflong
s.static void
swapArray(short[] array)
Swaps the content of the given array ofshort
s.static void
swapArray(Object array)
Swaps the content of the given array.static void
swapArray(Object[] array)
Swaps the content of the given array ofObject
s.
-
-
-
Method Detail
-
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 benull
array2
- the second array, can also benull
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 benull
array2
- the second array, can also benull
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 benull
and if an element-by-element comparision shall be performed, since this is what this method does: for each element pair theObjectUtils.equalObjects
method is called.- Parameters:
array1
- the first object array, can benull
array2
- the second object array, can also benull
- 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 searchedarray
- the array in which to search the element, must not benull
- Returns:
- the array index in the range
0
toarray.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 searchedarray
- the array in which to search the element, must not benull
- 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 ofbyte
s.- Parameters:
array
- the array ofbyte
s- Throws:
IllegalArgumentException
- if the given array isnull
-
swapArray
public static void swapArray(char[] array)
Swaps the content of the given array ofchar
s.- Parameters:
array
- the array ofchar
s- Throws:
IllegalArgumentException
- if the given array isnull
-
swapArray
public static void swapArray(short[] array)
Swaps the content of the given array ofshort
s.- Parameters:
array
- the array ofshort
s- Throws:
IllegalArgumentException
- if the given array isnull
-
swapArray
public static void swapArray(int[] array)
Swaps the content of the given array ofint
s.- Parameters:
array
- the array ofint
s- Throws:
IllegalArgumentException
- if the given array isnull
-
swapArray
public static void swapArray(long[] array)
Swaps the content of the given array oflong
s.- Parameters:
array
- the array oflong
s- Throws:
IllegalArgumentException
- if the given array isnull
-
swapArray
public static void swapArray(float[] array)
Swaps the content of the given array offloat
s.- Parameters:
array
- the array offloat
s- Throws:
IllegalArgumentException
- if the given array isnull
-
swapArray
public static void swapArray(double[] array)
Swaps the content of the given array ofdouble
s.- Parameters:
array
- the array ofdouble
s- Throws:
IllegalArgumentException
- if the given array isnull
-
swapArray
public static void swapArray(Object[] array)
Swaps the content of the given array ofObject
s.- Parameters:
array
- the array ofObject
s- Throws:
IllegalArgumentException
- if the given array isnull
-
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 asfloat[]
- Throws:
IllegalArgumentException
- if the given array isnull
or does not have a native array type
-
recycleOrCreateArray
public static int[] recycleOrCreateArray(int[] array, int length)
Recycles or creates a newint
array 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 newfloat
array 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 newdouble
array 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
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 isnull
-
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 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.
-
-