Class OptionalUtils

java.lang.Object
org.projecthusky.common.utils.OptionalUtils

public class OptionalUtils extends Object
Helper utilities related to Optionals.
Author:
Quentin Ligier
  • Method Details

    • getListElementAtIndex

      public static <T> @Nullable T getListElementAtIndex(List<T> list, int index)
      Safe accessor to Lists that returns null instead of throwing IndexOutOfBoundsException.
      Type Parameters:
      T - The type of the list content.
      Parameters:
      list - The list.
      index - The index to access, positive or null.
      Returns:
      the value of the list at the requested index as type T or null.
    • getListFirstElement

      public static <T> @Nullable T getListFirstElement(List<T> list)
      Safe accessor to Lists that returns null instead of throwing IndexOutOfBoundsException.
      Type Parameters:
      T - The type of the list content.
      Parameters:
      list - The list.
      Returns:
      the first value of the list as type T or null.
    • getListOnlyElement

      public static <T> @Nullable T getListOnlyElement(List<T> list)
      Safe accessor to Lists that returns null if the list contains no or multiple elements.
      Type Parameters:
      T - The type of the list content.
      Parameters:
      list - The list.
      Returns:
      the only value of the list as type T or null.
    • castOrNull

      public static <T> @Nullable T castOrNull(Object object, Class<T> type)
      Cast the given object to the required class or return null to filter the value in the Optional.
      Type Parameters:
      T - The required type.
      Parameters:
      object - The instance to cast or filter.
      type - The required type.
      Returns:
      the casted object or null if it wasn't an instance of the required type.