Class ObjectUtil

java.lang.Object
ru.progrm_jarvis.javacommons.object.ObjectUtil

public final class ObjectUtil extends Object
Utilities for common object operations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final @NotNull Object @NotNull @Unmodifiable []
    Empty array of objects.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T, R> R
    map(T value, @NonNull Function<T,R> mappingFunction)
    Maps (transforms) the value specified using the mapping function.
    static <T, R> R
    mapNonNull(@NonNull Function<@Nullable T,R> mappingFunction, @NotNull Supplier<@Nullable T> @NonNull ... variants)
    Returns the first nonnull value of specified variants or null if none found mapped using function specified.
    static <T, R> R
    mapNonNull(@NonNull Function<@Nullable T,R> mappingFunction, @Nullable T @NonNull ... variants)
    Returns the first nonnull value of specified variants or null if none found mapped using function specified.
    static <T, R> R
    mapNonNullOrThrow(@NonNull Function<@NotNull T,R> mappingFunction, @NotNull Supplier<@Nullable T> @NonNull ... variants)
    Returns the first nonnull value of specified variants mapped using function specified or throws NullPointerException if none found.
    static <T, R> R
    mapNonNullOrThrow(@NonNull Function<@NotNull T,R> mappingFunction, @Nullable T @NonNull ... variants)
    Returns the first nonnull value of specified variants mapped using function specified or throws NullPointerException if none found.
    static <T, R> R
    mapOnlyNonNull(@NonNull Function<@NotNull T,R> mappingFunction, @NotNull Supplier<@Nullable T> @NonNull ... variants)
    Returns the first nonnull value of specified variants mapped using function specified or null if none found.
    static <T, R> R
    mapOnlyNonNull(@NonNull Function<@NotNull T,R> mappingFunction, @Nullable T @NonNull ... variants)
    Returns the first nonnull value of specified variants mapped using function specified or null if none found.
    static <T> T
    nonNull(@Nullable T @NonNull ... variants)
    Returns the first nonnull value of specified variants or null if none found.
    static <T> T
    nonNull(Supplier<@Nullable T> @NonNull ... variants)
    Returns the first nonnull value of specified variants or null if none found.
    static <T> T
    nonNullOr(@NonNull Supplier<T> nullHandler, @NotNull Supplier<@Nullable T> @NonNull ... variants)
    Returns the first nonnull value of specified variants or throws NullPointerException if none found.
    static <T> T
    nonNullOr(@NonNull Supplier<T> nullHandler, @Nullable T @NonNull ... variants)
    Returns the first nonnull value of specified variants or throws NullPointerException if none found.
    static <T> T
    nonNullOrThrow(@NotNull Supplier<@Nullable T> @NonNull ... variants)
    Returns the first nonnull value of specified variants or throws NullPointerException if none found.
    static <T> T
    nonNullOrThrow(@Nullable T @NonNull ... variants)
    Returns the first nonnull value of specified variants or throws NullPointerException if none found.
    static <T> @NotNull Optional<T>
    optionalNonNull(@NotNull Supplier<@Nullable T> @NonNull ... variants)
    Returns the first nonnull value of specified variants wrapped in Optional or empty if none found.
    static <T> @NotNull Optional<T>
    optionalNonNull(@Nullable T @NonNull ... variants)
    Returns the first nonnull value of specified variants wrapped in Optional or empty if none found.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EMPTY_ARRAY

      @NotNull public static final @NotNull Object @NotNull @Unmodifiable [] EMPTY_ARRAY
      Empty array of objects.
  • Method Details

    • nonNull

      @SafeVarargs @Nullable public static <T> T nonNull(@Nullable @Nullable T @NonNull ... variants)
      Returns the first nonnull value of specified variants or null if none found.
      Type Parameters:
      T - type of value
      Parameters:
      variants - variants which may be nonnull
      Returns:
      first nonnull value found or null if none
    • nonNull

      @SafeVarargs @Nullable public static <T> T nonNull(Supplier<@Nullable T> @NonNull ... variants)
      Returns the first nonnull value of specified variants or null if none found.
      Type Parameters:
      T - type of value
      Parameters:
      variants - variant suppliers whose values may be null
      Returns:
      first nonnull value found or null if none
    • optionalNonNull

      @SafeVarargs @NotNull public static <T> @NotNull Optional<T> optionalNonNull(@Nullable @Nullable T @NonNull ... variants)
      Returns the first nonnull value of specified variants wrapped in Optional or empty if none found.
      Type Parameters:
      T - type of value
      Parameters:
      variants - variants which may be nonnull
      Returns:
      Optional containing first nonnull value found or empty if none
    • optionalNonNull

      @SafeVarargs @NotNull public static <T> @NotNull Optional<T> optionalNonNull(@NotNull @NotNull Supplier<@Nullable T> @NonNull ... variants)
      Returns the first nonnull value of specified variants wrapped in Optional or empty if none found.
      Type Parameters:
      T - type of value
      Parameters:
      variants - variant suppliers whose values may be null
      Returns:
      Optional containing first nonnull value found or empty if none
    • nonNullOrThrow

      @SafeVarargs @NotNull public static <T> T nonNullOrThrow(@Nullable @Nullable T @NonNull ... variants)
      Returns the first nonnull value of specified variants or throws NullPointerException if none found.
      Type Parameters:
      T - type of value
      Parameters:
      variants - variants which may be nonnull
      Returns:
      first nonnull value found
      Throws:
      NullPointerException - if none of the variants specified is nonnull
    • nonNullOrThrow

      @SafeVarargs @NotNull public static <T> T nonNullOrThrow(@NotNull @NotNull Supplier<@Nullable T> @NonNull ... variants)
      Returns the first nonnull value of specified variants or throws NullPointerException if none found.
      Type Parameters:
      T - type of value
      Parameters:
      variants - variant suppliers whose values may be null
      Returns:
      first nonnull value found
      Throws:
      NullPointerException - if none of the variants specified is nonnull
    • nonNullOr

      @SafeVarargs public static <T> T nonNullOr(@NonNull @NonNull Supplier<T> nullHandler, @Nullable @Nullable T @NonNull ... variants)
      Returns the first nonnull value of specified variants or throws NullPointerException if none found.
      Type Parameters:
      T - type of value
      Parameters:
      nullHandler - handler to be used to supply the value in case of all variants being null
      variants - variants which may be nonnull
      Returns:
      first nonnull value found
      Throws:
      NullPointerException - if none of the variants specified is nonnull
    • nonNullOr

      @SafeVarargs public static <T> T nonNullOr(@NonNull @NonNull Supplier<T> nullHandler, @NotNull @NotNull Supplier<@Nullable T> @NonNull ... variants)
      Returns the first nonnull value of specified variants or throws NullPointerException if none found.
      Type Parameters:
      T - type of value
      Parameters:
      nullHandler - handler to be used to supply the value in case of all variants being null
      variants - variant suppliers whose values may be null
      Returns:
      first nonnull value found
      Throws:
      NullPointerException - if none of the variants specified is nonnull
    • map

      public static <T, R> R map(T value, @NonNull @NonNull Function<T,R> mappingFunction)
      Maps (transforms) the value specified using the mapping function. This may come in handy in case of initializing fields with expressions which have checked exceptions.
      Type Parameters:
      T - type of source value
      R - type of resulting value
      Parameters:
      value - value to map
      mappingFunction - function to map the value to the required type
      Returns:
      mapped (transformed) value
    • mapNonNull

      @SafeVarargs public static <T, R> R mapNonNull(@NonNull @NonNull Function<@Nullable T,R> mappingFunction, @Nullable @Nullable T @NonNull ... variants)
      Returns the first nonnull value of specified variants or null if none found mapped using function specified.
      Type Parameters:
      T - type of source value
      R - type of resulting value
      Parameters:
      mappingFunction - function to map the value to the required type
      variants - variants which may be nonnull
      Returns:
      first nonnull value found or null if none found mapped using mapping function
    • mapNonNull

      @SafeVarargs public static <T, R> R mapNonNull(@NonNull @NonNull Function<@Nullable T,R> mappingFunction, @NotNull @NotNull Supplier<@Nullable T> @NonNull ... variants)
      Returns the first nonnull value of specified variants or null if none found mapped using function specified.
      Type Parameters:
      T - type of source value
      R - type of resulting value
      Parameters:
      mappingFunction - function to map the value to the required type
      variants - variant suppliers whose values may be null
      Returns:
      first nonnull value found or null if none found mapped using mapping function
    • mapOnlyNonNull

      @SafeVarargs @Nullable public static <T, R> R mapOnlyNonNull(@NonNull @NonNull Function<@NotNull T,R> mappingFunction, @Nullable @Nullable T @NonNull ... variants)
      Returns the first nonnull value of specified variants mapped using function specified or null if none found.
      Type Parameters:
      T - type of source value
      R - type of resulting value
      Parameters:
      mappingFunction - function to map the value to the required type
      variants - variants which may be nonnull
      Returns:
      first nonnull value found mapped using mapping function or null if none found
    • mapOnlyNonNull

      @SafeVarargs @Nullable public static <T, R> R mapOnlyNonNull(@NonNull @NonNull Function<@NotNull T,R> mappingFunction, @NotNull @NotNull Supplier<@Nullable T> @NonNull ... variants)
      Returns the first nonnull value of specified variants mapped using function specified or null if none found.
      Type Parameters:
      T - type of source value
      R - type of resulting value
      Parameters:
      mappingFunction - function to map the value to the required type
      variants - variant suppliers whose values may be null
      Returns:
      first nonnull value found mapped using mapping function or null if none found
    • mapNonNullOrThrow

      @SafeVarargs @NotNull public static <T, R> R mapNonNullOrThrow(@NonNull @NonNull Function<@NotNull T,R> mappingFunction, @Nullable @Nullable T @NonNull ... variants)
      Returns the first nonnull value of specified variants mapped using function specified or throws NullPointerException if none found.
      Type Parameters:
      T - type of source value
      R - type of resulting value
      Parameters:
      mappingFunction - function to map the value to the required type
      variants - variants which may be nonnull
      Returns:
      first nonnull value found mapped using mapping function
      Throws:
      NullPointerException - if none of the variants specified is nonnull
    • mapNonNullOrThrow

      @SafeVarargs @NotNull public static <T, R> R mapNonNullOrThrow(@NonNull @NonNull Function<@NotNull T,R> mappingFunction, @NotNull @NotNull Supplier<@Nullable T> @NonNull ... variants)
      Returns the first nonnull value of specified variants mapped using function specified or throws NullPointerException if none found.
      Type Parameters:
      T - type of source value
      R - type of resulting value
      Parameters:
      mappingFunction - function to map the value to the required type
      variants - variant suppliers whose values may be null
      Returns:
      first nonnull value found mapped using mapping function
      Throws:
      NullPointerException - if none of the variants specified is nonnull