public final class ArrayMethods extends Object
Constructor and Description |
---|
ArrayMethods() |
Modifier and Type | Method and Description |
---|---|
static void |
addToList(Object list,
Object element)
Add
element to list if it is not null . |
static Object |
find(Iterable list,
groovy.lang.Closure findClosure)
Find the first value in the iterable matching the closure condition.
|
static List |
sort(Iterable list,
groovy.lang.Closure sortClosure)
Sort the provided iterable using the given closure to determine the correct ordering.
|
public static void addToList(@GloopParameter(allowNull=false) Object list, @GloopParameter(allowNull=false) Object element)
element
to list
if it is not null
.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}") groovy.lang.Closure 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() }
list
- the iterable to be sortedsortClosure
- a one or two-argument closure for determining the correct ordering of the iterablepublic static Object find(Iterable list, @GloopParameter(defaultValue="{ a ->\n a != null\n}") groovy.lang.Closure findClosure)
list
- the iterable to searchfindClosure
- the condition to matchnull
if none was foundCopyright © 2021. All rights reserved.