Package org.openl.rules.util
Class Arrays
- java.lang.Object
-
- org.openl.rules.util.Arrays
-
public final class Arrays extends Object
A set of util methods to work with arrays. Note: For OpenL rules only! Don't use it in Java code.- Author:
- Yury Molchan
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T[]addElement(T[] array, int index, T element)static <T> T[]addElement(T[] array, int index, T... elements)Inserts the specified element at the specified position in the array.static <T> booleanisEmpty(T[] array)Checks if an array is empty or null.static <T> booleanisNotEmpty(T[] array)Checks if an array is NOT empty or null.static <T> intlength(T[] array)static <T> booleannoNulls(T... values)Returnstrueif the input elements exist, and they do not containnullelements.static <T,E extends T>
T[]removeElement(T[] array, E... elements)Removes the first occurrence of the specified element from the specified array.static <T> T[]removeNulls(T... elements)Return a new array without null elementsstatic <T> T[]slice(T[] values, int startIndexInclusive)static <T> T[]slice(T[] values, int startIndexInclusive, int endIndexExclusive)static <T extends Comparable<T>>
T[]sort(T... values)Returns a copy of the specified array into ascending order, according to the natural ordering of its elements.
-
-
-
Method Detail
-
isEmpty
public static <T> boolean isEmpty(T[] array)
Checks if an array is empty or null.- Parameters:
array- the array to test- Returns:
- true if the array is empty or null
-
isNotEmpty
public static <T> boolean isNotEmpty(T[] array)
Checks if an array is NOT empty or null.- Parameters:
array- the array to test- Returns:
- true if the array is NOT empty or null
-
length
public static <T> int length(T[] array)
-
addElement
public static <T> T[] addElement(T[] array, int index, T... elements)Inserts the specified element at the specified position in the array. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
This method returns a new array with the same elements of the input array plus the given element on the specified position. The component type of the returned array is always the same as that of the input array.
If the input array is
null, a new one element array is returned whose component type is the same as the element.Arrays.add(null, 0, null) = [null] Arrays.add(null, 0, "a") = ["a"] Arrays.add(["a"], 1, null) = ["a", null] Arrays.add(["a"], 1, "b") = ["a", "b"] Arrays.add(["a", "b"], 3, "c") = ["a", "b", "c"]
- Parameters:
array- the array to add the element to, may benullindex- the position of the new objectelements- the objects to add- Returns:
- A new array containing the existing elements and the new element
- Throws:
IndexOutOfBoundsException- if the index is out of range (index < 0 || index > array.length).
-
addElement
public static <T> T[] addElement(T[] array, int index, T element)
-
slice
public static <T> T[] slice(T[] values, int startIndexInclusive)
-
slice
public static <T> T[] slice(T[] values, int startIndexInclusive, int endIndexExclusive)
-
removeElement
public static <T,E extends T> T[] removeElement(T[] array, E... elements)Removes the first occurrence of the specified element from the specified array. All subsequent elements are shifted to the left (substracts one from their indices). If the array does not contains such an element, no elements are removed from the array.
This method returns a new array with the same elements of the input array except the first occurrence of the specified element. The component type of the returned array is always the same as that of the input array.
ArrayUtils.removeElement(null, "a") = null
ArrayUtils.removeElement([], "a") = []
ArrayUtils.removeElement(["a"], "b") = ["a"]
ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
- Parameters:
array- the array to remove the element from, may be nullelements-- Returns:
- the element to be removed
-
removeNulls
public static <T> T[] removeNulls(T... elements)
Return a new array without null elements- Parameters:
elements- whose null elements should be removed- Returns:
- array without null elements
-
noNulls
public static <T> boolean noNulls(T... values)
Returnstrueif the input elements exist, and they do not containnullelements.
-
sort
public static <T extends Comparable<T>> T[] sort(T... values)
Returns a copy of the specified array into ascending order, according to the natural ordering of its elements. All elements in the array must implement theComparableinterface. Furthermore, all elements in the array must be mutually comparable (that is,e1.compareTo(e2)must not throw aClassCastExceptionfor any elementse1ande2in the array).nullvalues are kept in the tail. This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.- Parameters:
values- the array to be sorted- Returns:
- a sorted array
-
-