Package org.projecthusky.common.utils
Class OptionalUtils
java.lang.Object
org.projecthusky.common.utils.OptionalUtils
Helper utilities related to
Optional
s.- Author:
- Quentin Ligier
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @Nullable T
castOrNull
(Object object, Class<T> type) Cast the given object to the required class or returnnull
to filter the value in theOptional
.static <T> @Nullable T
getListElementAtIndex
(List<T> list, int index) Safe accessor toList
s that returns null instead of throwingIndexOutOfBoundsException
.static <T> @Nullable T
getListFirstElement
(List<T> list) Safe accessor toList
s that returns null instead of throwingIndexOutOfBoundsException
.static <T> @Nullable T
getListOnlyElement
(List<T> list) Safe accessor toList
s that returns null if the list contains no or multiple elements.
-
Method Details
-
getListElementAtIndex
Safe accessor toList
s 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
T
ornull
.
-
getListFirstElement
Safe accessor toList
s 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
T
ornull
.
-
getListOnlyElement
Safe accessor toList
s 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
ornull
.
-
castOrNull
Cast the given object to the required class or returnnull
to 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
null
if it wasn't an instance of the required type.
-