java.lang.Object
ushiosan.jvm_utilities.lang.Obj

public final class Obj extends Object
Class containing functionality for general object manipulation, recasting, and data checking.
  • Method Details

    • toObject

      @NotNull public static @NotNull Object toObject(@NotNull @NotNull Object obj)
      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

      @NotNull public static @NotNull String toString(@Nullable @Nullable Object obj)
      Object string representation
      Parameters:
      obj - object string representation
      Returns:
      object string representation
      See Also:
    • toObjString

      @NotNull public static @NotNull String toObjString(@Nullable @Nullable Object obj)
      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

      @NotNull public static @NotNull String toInstanceString(@NotNull @NotNull Object instance)
      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 as PrintOpts and PrintExclude.
      Parameters:
      instance - the object to transform
      Returns:
      object string representation
    • toDetailString

      @NotNull public static @NotNull String toDetailString(@Nullable @Nullable Object obj)
      Object string representation.

      This method displays more detailed information about the object.

      Parameters:
      obj - object string representation
      Returns:
      object string representation
      See Also:
    • isNull

      public static boolean isNull(@Nullable @Nullable Object obj)
      Checks if an object or reference is null. This method also checks if an object of type Optional is empty (this means that the reference is null).
      Parameters:
      obj - the object to inspect
      Returns:
      true if reference is null or false otherwise
      See Also:
    • isNotNull

      public static boolean isNotNull(@Nullable @Nullable Object obj)
      Checks if an object or reference is not null. This method also checks if an object of type Optional is empty (this means that the reference is null).
      Parameters:
      obj - the object to inspect
      Returns:
      true if reference is not null or false otherwise
      See Also:
    • notNull

      @NotNull public static <T> T notNull(@Nullable T obj, @NotNull T defaultVal)
      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 inspect
      defaultVal - default value used, when reference is null
      Returns:
      the original reference if not null or the default value otherwise
    • notNull

      public static <T> void notNull(@Nullable T obj, @NotNull Apply.Empty<T> action)
      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 inspect
      action - the action that is executed when the reference is not null
    • cast

      public static <T> T cast(@Nullable @Nullable Object obj)
      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 convert
      clazz - target class to convert
      Returns:
      the transformed object
      Throws:
      ClassCastException - error if object is not compatible type
    • pairCast

      public static <T> T pairCast(@NotNull @NotNull Pair<?,Class<T>> pair)
      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 convert
      clazz - target class to convert
      action - 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 convert
      clazz - target class to convert
      Returns:
      the transformed object or Optional.empty() if object is not compatible type
    • canCast

      public static <T> boolean canCast(@Nullable @Nullable Object obj, @NotNull @NotNull Class<T> clazz)
      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 check
      clazz - target class to convert
      Returns:
      return true if the object can be converted to the desired type or false otherwise
    • 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 check
      clazz - target class to convert
      Returns:
      return true if the object can be converted to the desired type or false otherwise
    • isAnyTypeOf

      public static boolean isAnyTypeOf(@NotNull @NotNull Object obj, Class<?> @NotNull ... classes)
      Checks if the object is one of the specified types.
      Parameters:
      obj - the object to check
      classes - all the classes you want to analyze
      Returns:
      returns true if the object matches some data type or false otherwise
    • 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 configure
      action - 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 configure
      action - the action to execute
      Returns:
      the same object but with the configuration already applied
    • apply

      @NotNull public static <T, V> V apply(@NotNull 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 type
      V - result type
      Parameters:
      obj - the base object to configure
      action - 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 type
      V - result type
      Parameters:
      obj - the base object to configure
      action - the action to execute
      Returns:
      a different object depending on the applied configuration.