Class ArrayMethods


  • public final class ArrayMethods
    extends Object
    Contains methods for manipulating iterables.
    • Constructor Detail

      • ArrayMethods

        ArrayMethods()
    • Method Detail

      • 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 modified
        index - the index of the element you want to modify
        value - 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 modified
        index - the index of the element you want to remove
      • 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 sorted
        sortClosure - 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 search
        findClosure - 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 search
        findClosure - 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 search
        closure - 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 search
        closure - 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 search
        transformClosure - 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 string
        separator - the character or string that will serve as a delimiter or separator in the output string
        Returns:
        the joined String