public class ArrayMethods extends Object
Constructor and Description |
---|
ArrayMethods() |
Modifier and Type | Method and Description |
---|---|
static void |
addToList(Object list,
Object element)
Add a non-null object to a list, if the value is null, it will not be added
|
static Object |
find(Iterable list,
Closure findClosure)
Finds the first value matching the closure condition
|
static List |
sort(Iterable list,
Closure sortClosure)
Sorts this Iterable using the given Closure to determine the correct ordering.
|
public static void addToList(@GloopParameter(allowNull=false) Object list, @GloopParameter(allowNull=false) Object element)
list
- the list to add toelement
- the element to add to the listpublic static List sort(Iterable list, @GloopParameter(defaultValue="{ a, b ->\n a <=> b\n}") Closure sortClosure)
If the Closure has two parameters it is used like a traditional Comparator. I.e. 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() }
list
- the Iterable to be sortedsortClosure
- a 1 or 2 arg Closure used to determine the correct orderingpublic static Object find(Iterable list, @GloopParameter(defaultValue="{ a ->\n a != null\n}") Closure findClosure)
list
- an IterablefindClosure
- a closure conditionCopyright © 2019. All rights reserved.