Class ClassInstanceUtil

java.lang.Object
io.github.xrickastley.sevenelements.util.ClassInstanceUtil

public final class ClassInstanceUtil extends Object
  • Constructor Details

    • ClassInstanceUtil

      public ClassInstanceUtil()
  • Method Details

    • cast

      public static <T> T cast(Object instance)
    • castOrNull

      @Nullable public static <T> T castOrNull(Object instance, Class<T> castClass)
      Casts the provided instance into an instance of T if it's an instance of T.
      Type Parameters:
      T - The type of the instance.
      Parameters:
      instance - The object to cast into an instance of T
      castClass - The target class of the cast.
      Returns:
      instance as an instance of T, null otherwise.
    • mapOrNull

      @Nullable public static <T, R> R mapOrNull(@Nullable T instance, Function<T,R> mapper)
      Maps the provided instance into R if it isn't null.
      Type Parameters:
      T - The type of the instance.
      R - The type of the result.
      Parameters:
      instance - The instance of T to map if it isn't null.
      mapper - The mapper to apply if instance isn't null
      Returns:
      R if instance isn't null, null otherwise.
    • ifPresentMapped

      public static <T, R> void ifPresentMapped(@Nullable T instance, Function<T,R> mapper, Consumer<R> ifNonNull)
      If instance isn't null and mapper.apply(instance) isn't null, calls the ifNonNull consumer with the mapped value obtained from mapper.apply(instance).
      Type Parameters:
      T - The type of the instance.
      R - The type of the result.
      Parameters:
      instance - The instance of T to map if it isn't null.
      mapper - The mapper to apply if instance isn't null
      ifNonNull - The consumer to call if the result of mapping the instance with mapper isn't null.