Class ClassInstanceUtil
java.lang.Object
io.github.xrickastley.sevenelements.util.ClassInstanceUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tstatic <T> TcastOrNull(Object instance, Class<T> castClass) Casts the providedinstanceinto an instance ofTif it's an instance ofT.static <T,R> void ifPresentMapped(T instance, Function<T, R> mapper, Consumer<R> ifNonNull) Ifinstanceisn't null andmapper.apply(instance)isn'tnull, calls theifNonNullconsumer with the mapped value obtained frommapper.apply(instance).static <T,R> R Maps the provided instance intoRif it isn'tnull.
-
Constructor Details
-
ClassInstanceUtil
public ClassInstanceUtil()
-
-
Method Details
-
cast
-
castOrNull
Casts the providedinstanceinto an instance ofTif it's an instance ofT.- Type Parameters:
T- The type of the instance.- Parameters:
instance- The object to cast into an instance ofTcastClass- The target class of the cast.- Returns:
instanceas an instance ofT,nullotherwise.
-
mapOrNull
Maps the provided instance intoRif it isn'tnull.- Type Parameters:
T- The type of the instance.R- The type of the result.- Parameters:
instance- The instance ofTto map if it isn't null.mapper- The mapper to apply ifinstanceisn't null- Returns:
Rifinstanceisn'tnull,nullotherwise.
-
ifPresentMapped
public static <T,R> void ifPresentMapped(@Nullable T instance, Function<T, R> mapper, Consumer<R> ifNonNull) Ifinstanceisn't null andmapper.apply(instance)isn'tnull, calls theifNonNullconsumer with the mapped value obtained frommapper.apply(instance).- Type Parameters:
T- The type of the instance.R- The type of the result.- Parameters:
instance- The instance ofTto map if it isn't null.mapper- The mapper to apply ifinstanceisn't nullifNonNull- The consumer to call if the result of mapping the instance withmapperisn'tnull.
-