java.lang.Object
org.litebridgedb.commons.ObjectUtils
Utility class for working with objects.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TgetFieldValue(Object dto, String fieldName, Class<T> fieldType) Retrieves the value of a specified field from the given object.static <T,X extends Throwable>
TrequireNonNull(@Nullable T obj, Supplier<? extends X> exceptionSupplier) Ensures that the provided object is not null.static <X extends Throwable>
voidrequireNull(@Nullable Object obj, Supplier<? extends X> exceptionSupplier) Ensures that the provided object isnull.
-
Method Details
-
requireNonNull
public static <T,X extends Throwable> T requireNonNull(@Nullable T obj, Supplier<? extends X> exceptionSupplier) throws X Ensures that the provided object is not null.If the object is null, an exception provided by the
exceptionSupplieris thrown.- Type Parameters:
T- the type of the object to be validatedX- the type of the exception to be thrown if validation fails- Parameters:
obj- the object to be checked for nullabilityexceptionSupplier- the supplier that provides an exception to be thrown ifobjis null- Returns:
- the non-null object passed as the
objparameter - Throws:
X- ifobjis null
-
requireNull
public static <X extends Throwable> void requireNull(@Nullable Object obj, Supplier<? extends X> exceptionSupplier) throws X Ensures that the provided object isnull.If the object is not null, an exception provided by the
exceptionSupplieris thrown.- Type Parameters:
X- the type of the exception to be thrown- Parameters:
obj- the object to be checked for nullityexceptionSupplier- the supplier that provides an exception to be thrown ifobjis not null- Throws:
X- ifobjis not null
-
getFieldValue
Retrieves the value of a specified field from the given object.The field is accessed reflectively, and its value is returned as an object of the specified type.
- Type Parameters:
T- the expected type of the field's value- Parameters:
dto- the object from which the field value is to be retrievedfieldName- the name of the field to retrievefieldType- the expected class type of the field's value- Returns:
- the value of the specified field, cast to the specified type
- Throws:
IllegalStateException- if the field is inaccessible or retrieval fails
-