java.lang.Object
com.github.ushiosan23.jvm.base.Obj

public final class Obj extends Object
  • Method Details

    • toString

      @NotNull public static @NotNull String toString(@Nullable @Nullable Object data)
      Get object string representation
      Parameters:
      data - The data to convert
      Returns:
      Returns an object string representation
    • toInfoString

      @NotNull public static @NotNull String toInfoString(@Nullable @Nullable Object data)
      Get object info representation
      Parameters:
      data - The data to convert
      Returns:
      Returns an object info string representation
    • notNull

      public static <T> T notNull(@Nullable T obj, T defValue)
      Get not null value
      Type Parameters:
      T - Generic object type
      Parameters:
      obj - Object to check if it's null
      defValue - Default value if obj is null
      Returns:
      Return obj if it's not null or defValue otherwise
    • notNull

      public static <T> void notNull(@Nullable T obj, @NotNull IApply.EmptyResult<T> apply)
      Execute action only if object is not null
      Type Parameters:
      T - Generic object type
      Parameters:
      obj - The object to evaluate
      apply - Action to execute
    • apply

      public static <T> T apply(T obj, @NotNull IApply.WithResult<T,T> action)
      Apply configuration to the object and return it as a result
      Type Parameters:
      T - Generic object type
      Parameters:
      obj - Object to which the action applies
      action - Apply action to execute
      Returns:
      Returns the same object is returned but with the changes made
    • applyTransform

      public static <T, V> V applyTransform(T obj, @NotNull IApply.WithResult<T,V> action)
      Apply configuration to the object and return another object result

      Object to which the action applies

      Type Parameters:
      T - Generic object type
      V - Generic return type
      Parameters:
      obj - The object to transform
      action - Apply action to execute
      Returns:
      Returns a transformation object result
    • castTo

      @Contract(value="_, _ -> param1", pure=true) public static <T> T castTo(@Nullable @Nullable Object obj, @NotNull @NotNull Class<T> tClass)
      Recast the object towards the assigned destination. This method must be sure that the object is of the desired type, or it will generate an error.
      Type Parameters:
      T - Generic destination type
      Parameters:
      obj - Object to analyze
      tClass - Destination class
      Returns:
      Returns the transformed object
      Throws:
      ClassCastException - Error if object is not compatible with type
    • castTo

      public static <T> T castTo(@Nullable @Nullable Object obj)
      Recast the object towards the assigned destination. This method must be sure that the object is of the desired type, or it will generate an error.

      Note: This method is based on the context in which it is called.

      Type Parameters:
      T - Generic destination type
      Parameters:
      obj - Object to analyze
      Returns:
      Returns the transformed object
      Throws:
      ClassCastException - Error if object is not compatible with type
    • tryCast

      public static <T> void tryCast(@NotNull @NotNull Object obj, @NotNull @NotNull Class<T> tClass, @NotNull IApply.EmptyResult<T> action)
      It 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.

      Example:

      
           Object unknownType = "Hello, World!";
      
           Obj.tryCast(
              unknownType, // Object to be evaluated
              String.class, // Destination class
              object -> System.out.println(object) // Action that is only executed if the recast is valid
           );
       
      Type Parameters:
      T - Generic destination type
      Parameters:
      obj - Object to analyze
      tClass - Destination class
      action - action that is executed when the recast is valid.
    • tryCast

      @NotNull public static <T> @NotNull Optional<T> tryCast(@NotNull @NotNull Object obj, @NotNull @NotNull Class<T> tClass)
      It tries to recast the object towards the assigned destination, but does not generate an error.
      Type Parameters:
      T - Generic destination type
      Parameters:
      obj - Object to analyze
      tClass - Destination class
      Returns:
      Returns a recast object or Optional.empty() if the cast fails
      See Also: