Package io.toro.martini
Class ArrayMethods
- java.lang.Object
-
- io.toro.martini.ArrayMethods
-
public final class ArrayMethods extends Object
Contains methods for manipulating iterables.
-
-
Constructor Summary
Constructors Constructor Description ArrayMethods()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addToList(Object list, Object element)
Addelement
tolist
if it is notnull
.static <T> boolean
allElementsSatisfyCondition(Collection<T> list, Closure<Boolean> closure)
Goes through the items in a list and checks if a certain condition is true for all items.static <T> T
find(Iterable<T> list, Closure<Boolean> findClosure)
Find the first value in the iterable matching the closure condition.static <T> List<T>
findAll(Collection<T> list, Closure<Boolean> findClosure)
Find all the values in the list matching the closure condition.static <T> boolean
hasElementSatisfyingCondition(Collection<T> list, Closure<Boolean> closure)
Goes through the items in a list and checks if a certain condition is true for at least one item.static String
joinElementsIntoString(Collection<?> list, String separator)
Joins together the textual representations (toString()
) of items in a list using the specified separator.static <T> List<T>
removeAtIndex(List<T> list, int index)
Remove an element from a list by specifying its index.static <T> boolean
removeElement(Collection<T> list, Object value)
Remove the specified element from a list.static <T> List<T>
setAtIndex(List<T> list, int index, T value)
Set the value of an element in a list by specifying its index.static <T> List<T>
sort(Iterable<T> list, Closure<Integer> sortClosure)
Sort the provided iterable using the given closure to determine the correct ordering.static Object
sum(Collection<? extends Number> list)
Adds up the values in a list.static <T> List<T>
transformElements(Collection<Object> list, Closure<T> transformClosure)
Maps each item in the list to a new value using the given closure and returns the list of new values.
-
-
-
Method Detail
-
addToList
public static void addToList(@GloopParameter(allowNull=false) Object list, @GloopParameter(allowNull=false) Object element)
Addelement
tolist
if it is notnull
.- Parameters:
list
- the list to add toelement
- the element to add to the list- Since:
- 1.0
-
setAtIndex
public static <T> List<T> setAtIndex(@GloopParameter(allowNull=false) List<T> list, @GloopParameter(allowNull=false) int index, T value)
Set the value of an element in a list by specifying its index.- Parameters:
list
- the list to be modifiedindex
- the index of the element you want to modifyvalue
- the new value to put at the given index
-
removeAtIndex
public static <T> List<T> removeAtIndex(@GloopParameter(allowNull=false) List<T> list, @GloopParameter(allowNull=false) int index)
Remove an element from a list by specifying its index.- Parameters:
list
- the list to be modifiedindex
- the index of the element you want to remove
-
removeElement
public static <T> boolean removeElement(@GloopParameter(allowNull=false) Collection<T> list, @GloopParameter(allowNull=false) Object value)
Remove the specified element from a list.- Parameters:
list
- the list to be modifiedvalue
- the value to be removed from the list, if present
-
sort
public static <T> List<T> sort(Iterable<T> list, @GloopParameter(defaultValue="{ a, b ->\n a <=> b\n}") Closure<Integer> sortClosure)
Sort the provided iterable using the given closure to determine the correct ordering.
If the iterable is a
List
, it is sorted in place and returned. Otherwise, the elements are first placed into a new list which is then sorted and returned - leaving the original iterable unchanged.If the closure has two parameters, it is used like a traditional
Comparator
. It should compare its two parameters for order, returning a negative integer, zero, or a positive integer when the first parameter is less than, equal to, or greater than the second respectively.Otherwise, the closure is assumed to take a single parameter and return a
Comparable
(typically an integer) which is then used for further comparison.assert ["hi","hey","hello"] == ["hello","hi","hey"].sort { it.length() } assert ["hi","hey","hello"] == ["hello","hi","hey"].sort { a, b -> a.length() <=> * b.length() }
- Parameters:
list
- the iterable to be sortedsortClosure
- a one or two-argument closure for determining the correct ordering of the iterable- Returns:
- a newly created, sorted list
- Since:
- 1.0
-
find
public static <T> T find(Iterable<T> list, @GloopParameter(defaultValue="{ a ->\n a != null\n}") Closure<Boolean> findClosure)
Find the first value in the iterable matching the closure condition.- Parameters:
list
- the iterable to searchfindClosure
- the condition to match- Returns:
- the first object found which matches the condition;
null
if none was found - Since:
- 1.0
-
findAll
public static <T> List<T> findAll(Collection<T> list, @GloopParameter(defaultValue="{ a ->\n a != null\n}") Closure<Boolean> findClosure)
Find all the values in the list matching the closure condition.- Parameters:
list
- the list to searchfindClosure
- the condition to match- Returns:
- the list of objects found which match the condition;
null
if none was found
-
hasElementSatisfyingCondition
public static <T> boolean hasElementSatisfyingCondition(@GloopParameter(allowNull=false) Collection<T> list, @GloopParameter(defaultValue="{ a ->\n a != null\n}") Closure<Boolean> closure)
Goes through the items in a list and checks if a certain condition is true for at least one item.- Parameters:
list
- the iterable to searchclosure
- the condition to match- Returns:
true
if at least one item satisfies the condition,false
otherwise
-
allElementsSatisfyCondition
public static <T> boolean allElementsSatisfyCondition(@GloopParameter(allowNull=false) Collection<T> list, @GloopParameter(defaultValue="{ a ->\n a != null\n}") Closure<Boolean> closure)
Goes through the items in a list and checks if a certain condition is true for all items.- Parameters:
list
- the iterable to searchclosure
- the condition to match- Returns:
true
if all items satisfy the condition,false
otherwise
-
transformElements
public static <T> List<T> transformElements(@GloopParameter(allowNull=false) Collection<Object> list, @GloopParameter(allowNull=false) Closure<T> transformClosure)
Maps each item in the list to a new value using the given closure and returns the list of new values.- Parameters:
list
- the iterable to searchtransformClosure
- the condition to match- Returns:
- a new list containing the transformed items
-
joinElementsIntoString
public static String joinElementsIntoString(@GloopParameter Collection<?> list, String separator)
Joins together the textual representations (toString()
) of items in a list using the specified separator.- Parameters:
list
- the list whose elements will be joined to a single stringseparator
- the character or string that will serve as a delimiter or separator in the output string- Returns:
- the joined String
-
sum
public static Object sum(@GloopParameter(allowNull=false) Collection<? extends Number> list)
Adds up the values in a list.- Parameters:
list
- the list from which to get the sum- Returns:
- the sum of values in the list
-
-