Package org.projecthusky.common.utils
Class OptionalUtils
java.lang.Object
org.projecthusky.common.utils.OptionalUtils
Helper utilities related to
Optionals.- Author:
- Quentin Ligier
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @Nullable TcastOrNull(Object object, Class<T> type) Cast the given object to the required class or returnnullto filter the value in theOptional.static <T> @Nullable TgetListElementAtIndex(List<T> list, int index) Safe accessor toLists that returns null instead of throwingIndexOutOfBoundsException.static <T> @Nullable TgetListFirstElement(List<T> list) Safe accessor toLists that returns null instead of throwingIndexOutOfBoundsException.static <T> @Nullable TgetListOnlyElement(List<T> list) Safe accessor toLists that returns null if the list contains no or multiple elements.
-
Method Details
-
getListElementAtIndex
Safe accessor toLists that returns null instead of throwingIndexOutOfBoundsException.- 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
Tornull.
-
getListFirstElement
Safe accessor toLists that returns null instead of throwingIndexOutOfBoundsException.- Type Parameters:
T- The type of the list content.- Parameters:
list- The list.- Returns:
- the first value of the list as type
Tornull.
-
getListOnlyElement
Safe accessor toLists 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
Tornull.
-
castOrNull
Cast the given object to the required class or returnnullto filter the value in theOptional.- Type Parameters:
T- The required type.- Parameters:
object- The instance to cast or filter.type- The required type.- Returns:
- the casted object or
nullif it wasn't an instance of the required type.
-