Class ListUtils


  • public class ListUtils
    extends Object
    Utility class for List(s)
    Author:
    Sebastien Briquet - sebfz1
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> List<T> contains​(String search, List<T> list)
      Returns a sub list of items of type T having their textual representation (toString()) containing the search criteria
      The max size of the sub list is MAX
      static <T> List<T> contains​(String search, List<T> list, int max)
      Returns a sub list of items of type T having their textual representation (toString()) containing the search criteria
      static void empty​(List<?> list)
      Empty a List
      static List<String> exclude​(List<String> list, String... items)
      Excludes items from a List and return a new List
      static List<String> exclude​(List<String> list, List<String> items)
      Excludes items from a List and return a new List
      static <T> T fromHash​(int hash, List<T> list)
      Gets the list-item matching the given hash against the list
      static int indexOf​(int hash, List<?> list)
      Gets the index of the item occurrence matching the specified hashcode.
      static boolean isEmpty​(List<StringValue> list)
      Indicates whether the list is null or empty
      static <T> void move​(T item, int index, List<T> list)
      Utility method to move a list-item at a new position in the specified list
      This method will use the hashcode of the list-item to retrieve it against the list.
      static int random​(int bound)
      Returns a pseudo-random int value between 0 (inclusive) and the specified bound (exclusive)
      static double random​(int min, int max)
      Returns a pseudo-random double value between the minimum (inclusive) and the maximum bound (exclusive)
      static <T> T random​(List<T> list)
      Gets a random object from the supplied List
      static <T> T random​(T[] array)
      Gets a random object from the supplied array
      static <T> List<T> startsWith​(String search, List<T> list)
      Returns a sub list of items of type T having their textual representation (toString()) starting with the search criteria
      The max size of the sub list is MAX
      static <T> List<T> startsWith​(String search, List<T> list, int max)
      Returns a sub list of items of type T having their textual representation (toString()) starting with the search criteria
      static List<String> toStringList​(List<StringValue> values)
      Converts a list of StringValues to a list of String
      static List<String> toStringList​(StringValue values)
      Converts a StringValue to a list of String
    • Method Detail

      • random

        public static int random​(int bound)
        Returns a pseudo-random int value between 0 (inclusive) and the specified bound (exclusive)
        Parameters:
        bound - the maximum bound (exclusive)
        Returns:
        a pseudo-random int
      • random

        public static <T> T random​(T[] array)
        Gets a random object from the supplied array
        Type Parameters:
        T - the object type
        Parameters:
        array - the array
        Returns:
        null if the supplied array is empty
      • random

        public static <T> T random​(List<T> list)
        Gets a random object from the supplied List
        Type Parameters:
        T - the object type
        Parameters:
        list - the List
        Returns:
        null if the supplied list is empty
      • random

        public static double random​(int min,
                                    int max)
        Returns a pseudo-random double value between the minimum (inclusive) and the maximum bound (exclusive)
        Parameters:
        min - the minimum bound (inclusive)
        max - the maximum bound (exclusive)
        Returns:
        a pseudo-random double
      • empty

        public static void empty​(List<?> list)
        Empty a List
        Parameters:
        list - the list to empty
      • fromHash

        public static <T> T fromHash​(int hash,
                                     List<T> list)
        Gets the list-item matching the given hash against the list
        Type Parameters:
        T - the object type
        Parameters:
        hash - the hashcode to match
        list - the List to search in
        Returns:
        the list-item or null if not found
      • indexOf

        public static int indexOf​(int hash,
                                  List<?> list)
        Gets the index of the item occurrence matching the specified hashcode.
        Parameters:
        list - the List
        hash - the hashcode to match
        Returns:
        the index of the item matching the hashcode or -1 if not found
      • move

        public static <T> void move​(T item,
                                    int index,
                                    List<T> list)
        Utility method to move a list-item at a new position in the specified list
        This method will use the hashcode of the list-item to retrieve it against the list.
        Type Parameters:
        T - the object type
        Parameters:
        list - the List
        item - the item
        index - the position to move to
      • contains

        public static <T> List<T> contains​(String search,
                                           List<T> list)
        Returns a sub list of items of type T having their textual representation (toString()) containing the search criteria
        The max size of the sub list is MAX
        Type Parameters:
        T - the type
        Parameters:
        search - search criteria
        list - reference list
        Returns:
        the sub list
      • contains

        public static <T> List<T> contains​(String search,
                                           List<T> list,
                                           int max)
        Returns a sub list of items of type T having their textual representation (toString()) containing the search criteria
        Type Parameters:
        T - the type
        Parameters:
        search - search criteria
        list - reference list
        max - max size of the sub list to be returned
        Returns:
        the sub list
      • startsWith

        public static <T> List<T> startsWith​(String search,
                                             List<T> list)
        Returns a sub list of items of type T having their textual representation (toString()) starting with the search criteria
        The max size of the sub list is MAX
        Type Parameters:
        T - the type
        Parameters:
        search - search criteria
        list - reference list
        Returns:
        the sub list
      • startsWith

        public static <T> List<T> startsWith​(String search,
                                             List<T> list,
                                             int max)
        Returns a sub list of items of type T having their textual representation (toString()) starting with the search criteria
        Type Parameters:
        T - the type
        Parameters:
        search - search criteria
        list - reference list
        max - max size of the sub list to be returned
        Returns:
        the sub list
      • exclude

        public static List<String> exclude​(List<String> list,
                                           String... items)
        Excludes items from a List and return a new List
        Parameters:
        list - the original List
        items - items to remove
        Returns:
        a new List without excluded items
      • exclude

        public static List<String> exclude​(List<String> list,
                                           List<String> items)
        Excludes items from a List and return a new List
        Parameters:
        list - the original List
        items - items to remove
        Returns:
        a new List without excluded items
      • isEmpty

        public static boolean isEmpty​(List<StringValue> list)
        Indicates whether the list is null or empty
        Parameters:
        list -
        Returns:
        true if the list is null or empty