Class ObjectUtils

java.lang.Object
no.mnemonic.commons.utilities.ObjectUtils

public class ObjectUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T, V> V
    ifNotNull(T value, Function<T,V> converter)
    Applies a conversion to 'value', or returns null if 'value' is null.
    static <T, V> V
    ifNotNull(T value, Function<T,V> converter, V nullValue)
    Applies a conversion to 'value', or returns 'nullValue' if 'value' is null.
    static <T> void
    ifNotNullDo(T value, Consumer<T> consumer)
    Executes a consumer function on given value, unless value is null
    static <T> T
    ifNull(T value, Supplier<T> defaultValue)
    Returns a default value if 'value' is null, returns 'value' otherwise.
    static <T> T
    ifNull(T value, T defaultValue)
    Returns a default value if 'value' is null, returns 'value' otherwise.
    static <T> T
    notNull(T value, Exception exception)
    Throws an exception if 'value' is null, returns 'value' otherwise.
    static <T> T
    notNull(T value, String message)
    Throws a RuntimeException if 'value' is null, returns 'value' otherwise.

    Methods inherited from class java.lang.Object

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

    • notNull

      public static <T> T notNull(T value, String message)
      Throws a RuntimeException if 'value' is null, returns 'value' otherwise.
      Type Parameters:
      T - Type of value parameter.
      Parameters:
      value - Object to be tested for null.
      message - Message passed to the exception thrown.
      Returns:
      'value' if not null, throws a RuntimeException otherwise.
    • notNull

      public static <T> T notNull(T value, Exception exception) throws Exception
      Throws an exception if 'value' is null, returns 'value' otherwise.
      Type Parameters:
      T - Type of value parameter.
      Parameters:
      value - Object to be tested for null.
      exception - Exception to be thrown.
      Returns:
      'value' if not null, throws an exception otherwise.
      Throws:
      Exception - Thrown if 'value' is null.
    • ifNull

      public static <T> T ifNull(T value, T defaultValue)
      Returns a default value if 'value' is null, returns 'value' otherwise.
      Type Parameters:
      T - Type of value parameter.
      Parameters:
      value - Value to be tested for null.
      defaultValue - Value to return if 'value' is null.
      Returns:
      Either 'value' or 'defaultValue'.
    • ifNull

      public static <T> T ifNull(T value, Supplier<T> defaultValue)
      Returns a default value if 'value' is null, returns 'value' otherwise.
      Type Parameters:
      T - Type of value parameter.
      Parameters:
      value - Value to be tested for null.
      defaultValue - Supplier which provides a value to return if 'value' is null.
      Returns:
      Either 'value' or default value provided by supplier.
    • ifNotNull

      public static <T, V> V ifNotNull(T value, Function<T,V> converter)
      Applies a conversion to 'value', or returns null if 'value' is null.
      Type Parameters:
      T - Type of value parameter.
      V - Type of return value.
      Parameters:
      value - Value to be converted.
      converter - Converter function.
      Returns:
      Converted value, or null if 'value' was null.
    • ifNotNullDo

      public static <T> void ifNotNullDo(T value, Consumer<T> consumer)
      Executes a consumer function on given value, unless value is null
      Type Parameters:
      T - Type of value parameter.
      Parameters:
      value - Value to be evaluated.
      consumer - Consumer function to give value to
    • ifNotNull

      public static <T, V> V ifNotNull(T value, Function<T,V> converter, V nullValue)
      Applies a conversion to 'value', or returns 'nullValue' if 'value' is null.
      Type Parameters:
      T - Type of value parameter.
      V - Type of return value.
      Parameters:
      value - Value to be converted.
      converter - Converter function.
      nullValue - Value to return if 'value' is null.
      Returns:
      Converted value, or 'nullValue' if 'value' was null.