java.lang.Object
ushiosan.jvm_utilities.lang.Obj
Class containing functionality for general object manipulation, recasting, and data checking.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Talso(T obj, @NotNull Apply.Empty<T> action) Applies configuration to an object based on a local context.static <T,E extends Throwable>
TalsoErr(T obj, @NotNull Apply.EmptyError<T, E> action) Applies configuration to an object based on a local context.static <T,E extends Throwable>
TalsoErrSafe(T obj, @NotNull Apply.EmptyErrorSafe<T, E> action) Applies configuration to an object based on a local context.alsoNotNull(T obj, @NotNull Apply.Empty<T> action) Applies configuration to an object based on a local context.static <T,V> V apply(T obj, @NotNull Apply.Result<T, V> action) Applies configuration to an object based on a local context.static <T,V, E extends Throwable>
VapplyErr(T obj, @NotNull Apply.ResultError<T, V, E> action) Applies configuration to an object based on a local context.applyErrNotNull(T obj, @NotNull Apply.ResultError<T, V, E> action) Applies configuration to an object based on a local context.applyErrSafe(T obj, @NotNull Apply.ResultErrorSafe<T, V, E> action) Applies configuration to an object based on a local context.applyErrSafeNotNull(T obj, @NotNull Apply.ResultErrorSafe<T, V, E> action) Applies configuration to an object based on a local context.applyNotNull(T obj, @NotNull Apply.Result<T, V> action) Applies configuration to an object based on a local context.static <T> booleanCheck if one object can be cast to another type.static <T> booleancanCastNotNull(@Nullable Object obj, @NotNull Class<T> clazz) Check if one object can be cast to another type.static <T> TRecast the object towards the assigned destination.static <T> TRecast the object towards the assigned destination.static booleanisAnyTypeOf(@NotNull Object obj, Class<?> @NotNull ... classes) Checks if the object is one of the specified types.static booleanChecks if an object or reference is not null.static booleanChecks if an object or reference is null.static Loggerlogger()Returns the default global loggerstatic <T> TnotNull(T obj, T defaultVal) Method used to prevent null references and avoid errors for this type of objects.static <T> voidnotNull(T obj, Apply.Empty<T> action) Method used to prevent null references and avoid errors for this type of objects.static <T> TRecast the object towards the assigned destination.Object string representation.toInstanceString(@NotNull Object instance) Loop through the entire object and create a representation of the object in a text string.converts any object to a generic object.toObjString(@Nullable Object obj) Object string representation.Object string representationstatic <T> Optional<T>Tries to recast the object towards the assigned destination, but does not generate an error.static <T> voidTries to recast the object towards the assigned destination, but does not generate an error.
-
Method Details
-
logger
Returns the default global logger- Returns:
- the logger instance
-
toObject
converts any object to a generic object. Even for primitive types.- Parameters:
obj- the object to convert- Returns:
- the same object but with different type. For primitive types, these are wrapped in their wrapper classes, and it is possible to perform the operations of a normal object.
-
toString
Object string representation- Parameters:
obj- object string representation- Returns:
- object string representation
- See Also:
-
toObjString
Object string representation.The same behavior as the
toString(Object)method but with a different name to be able to do the following:import static ushiosan.jvm_utilities.lang.Obj.toObjString; // File content... public void myMethod() { // Some code ... System.out.println(toObjString(myVar)); }- Parameters:
obj- object string representation- Returns:
- object string representation
- See Also:
-
toInstanceString
Loop through the entire object and create a representation of the object in a text string. This behavior can be configured using class annotations such asPrintOptsandPrintExclude.- Parameters:
instance- the object to transform- Returns:
- object string representation
-
toDetailString
Object string representation.This method displays more detailed information about the object.
- Parameters:
obj- object string representation- Returns:
- object string representation
- See Also:
-
isNull
Checks if an object or reference is null. This method also checks if an object of typeOptionalis empty (this means that the reference is null).- Parameters:
obj- the object to inspect- Returns:
trueif reference isnullorfalseotherwise- See Also:
-
isNotNull
Checks if an object or reference is not null. This method also checks if an object of typeOptionalis empty (this means that the reference is null).- Parameters:
obj- the object to inspect- Returns:
trueif reference is notnullorfalseotherwise- See Also:
-
notNull
Method used to prevent null references and avoid errors for this type of objects.- Type Parameters:
T- generic object type- Parameters:
obj- the object to inspectdefaultVal- default value used, when reference is null- Returns:
- the original reference if not
nullor the default value otherwise
-
notNull
Method used to prevent null references and avoid errors for this type of objects.- Type Parameters:
T- generic object type- Parameters:
obj- the object to inspectaction- the action that is executed when the reference is not null
-
cast
Recast the object towards the assigned destination.Note: this method returns the result depending on the context
Be careful: this method must be sure that the object is of the desired type, or it will generate an error.
- Type Parameters:
T- class cast type- Parameters:
obj- the object to convert- Returns:
- the transformed object
- Throws:
ClassCastException- error if object is not compatible type
-
cast
@Contract(value="_, _ -> param1", pure=true) public static <T> T cast(@Nullable @Nullable Object obj, @NotNull @NotNull Class<T> clazz) Recast the object towards the assigned destination.Be careful: this method must be sure that the object is of the desired type, or it will generate an error.
- Type Parameters:
T- class cast type- Parameters:
obj- the object to convertclazz- target class to convert- Returns:
- the transformed object
- Throws:
ClassCastException- error if object is not compatible type
-
pairCast
Recast the object towards the assigned destination.Be careful: this method must be sure that the object is of the desired type, or it will generate an error.
- Type Parameters:
T- class cast type- Parameters:
pair- the entry elements to convert- Returns:
- the transformed object
- Throws:
ClassCastException- error if object is not compatible type
-
tryCast
public static <T> void tryCast(@Nullable @Nullable Object obj, @NotNull @NotNull Class<T> clazz, Apply.Empty<T> action) Tries to recast the object towards the assigned destination, but does not generate an error. Instead, the action passed as the third parameter is executed, as long as the condition is true.- Type Parameters:
T- class cast type- Parameters:
obj- the object to convertclazz- target class to convertaction- the action to execute if casting is valid
-
tryCast
public static <T> Optional<T> tryCast(@Nullable @Nullable Object obj, @NotNull @NotNull Class<T> clazz) Tries to recast the object towards the assigned destination, but does not generate an error. Instead, the action passed as the third parameter is executed, as long as the condition is true.- Type Parameters:
T- class cast type- Parameters:
obj- the object to convertclazz- target class to convert- Returns:
- the transformed object or
Optional.empty()if object is not compatible type
-
canCast
Check if one object can be cast to another type.Be careful: Remember that null values are valid candidates to change to any data type (except primitive types).
- Type Parameters:
T- class type- Parameters:
obj- the object to checkclazz- target class to convert- Returns:
- return
trueif the object can be converted to the desired type orfalseotherwise
-
canCastNotNull
public static <T> boolean canCastNotNull(@Nullable @Nullable Object obj, @NotNull @NotNull Class<T> clazz) Check if one object can be cast to another type.- Type Parameters:
T- class type- Parameters:
obj- the object to checkclazz- target class to convert- Returns:
- return
trueif the object can be converted to the desired type orfalseotherwise
-
isAnyTypeOf
Checks if the object is one of the specified types.- Parameters:
obj- the object to checkclasses- all the classes you want to analyze- Returns:
- returns
trueif the object matches some data type orfalseotherwise
-
also
@Contract("_, _ -> param1") @NotNull public static <T> T also(@NotNull T obj, @NotNull Apply.Empty<T> action) Applies configuration to an object based on a local context. Returns the same object but with the configuration already applied.- Type Parameters:
T- object type- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- the same object but with the configuration already applied
-
alsoErr
@NotNull public static <T,E extends Throwable> T alsoErr(@NotNull T obj, @NotNull Apply.EmptyError<T, E> action) throws EApplies configuration to an object based on a local context. Returns the same object but with the configuration already applied.- Type Parameters:
T- object typeE- the type of error that can occur- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- the same object but with the configuration already applied
- Throws:
E- error if something goes wrong
-
alsoErrSafe
@NotNull public static <T,E extends Throwable> T alsoErrSafe(@NotNull T obj, @NotNull Apply.EmptyErrorSafe<T, E> action) Applies configuration to an object based on a local context. Returns the same object but with the configuration already applied.- Type Parameters:
T- object typeE- the type of error that can occur- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- the same object but with the configuration already applied
-
alsoNotNull
@NotNull public static <T> @NotNull Optional<T> alsoNotNull(@Nullable T obj, @NotNull Apply.Empty<T> action) Applies configuration to an object based on a local context. Returns the same object but with the configuration already applied.- Type Parameters:
T- object type- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- the same object but with the configuration already applied
-
apply
Applies configuration to an object based on a local context. Returns a different object depending on the applied configuration.- Type Parameters:
T- object typeV- result type- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- a different object depending on the applied configuration.
-
applyErr
@NotNull public static <T,V, V applyErrE extends Throwable> (@NotNull T obj, @NotNull Apply.ResultError<T, V, throws EE> action) Applies configuration to an object based on a local context. Returns a different object depending on the applied configuration.- Type Parameters:
T- object typeV- result typeE- the type of error that can occur- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- a different object depending on the applied configuration.
- Throws:
E- error if something goes wrong
-
applyErrSafe
@NotNull public static <T,V, @NotNull Optional<V> applyErrSafeE extends Throwable> (@NotNull T obj, @NotNull Apply.ResultErrorSafe<T, V, E> action) Applies configuration to an object based on a local context. Returns a different object depending on the applied configuration.- Type Parameters:
T- object typeV- result typeE- the type of error that can occur- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- a different object depending on the applied configuration.
-
applyNotNull
@NotNull public static <T,V> @NotNull Optional<V> applyNotNull(@Nullable T obj, @NotNull Apply.Result<T, V> action) Applies configuration to an object based on a local context. Returns a different object depending on the applied configuration.- Type Parameters:
T- object typeV- result type- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- a different object depending on the applied configuration.
-
applyErrNotNull
@NotNull public static <T,V, @NotNull Optional<V> applyErrNotNullE extends Throwable> (@Nullable T obj, @NotNull Apply.ResultError<T, V, throws EE> action) Applies configuration to an object based on a local context. Returns a different object depending on the applied configuration.- Type Parameters:
T- object typeV- result typeE- the type of error that can occur- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- a different object depending on the applied configuration.
- Throws:
E- error if something goes wrong
-
applyErrSafeNotNull
@NotNull public static <T,V, @NotNull Optional<V> applyErrSafeNotNullE extends Throwable> (@Nullable T obj, @NotNull Apply.ResultErrorSafe<T, V, E> action) Applies configuration to an object based on a local context. Returns a different object depending on the applied configuration.- Type Parameters:
T- object typeV- result typeE- the type of error that can occur- Parameters:
obj- the base object to configureaction- the action to execute- Returns:
- a different object depending on the applied configuration.
-