Class NullableField<T>

java.lang.Object
name.martingeisse.grumpyjson.builtin.helper_types.NullableField<T>
Type Parameters:
T - the type of the contained value

public final class NullableField<T> extends Object
This can be used to wrap a property and allow JSON null instead of an actual value. Normally, JSON null cannot be used even for properties which use reference types on the Java side.

A nullable field is not by itself optional, i.e. cannot be absent from the JSON. To get a property that can be absent, null, or something, use OptionalField<NullableField<YourType>>.

A nullable field does not map to a null reference on the Java side, and using a null reference during JSON serialization throws an exception. Instead, a JSON null is mapped to and from a NullableField that isNull().

  • Method Details

    • ofValue

      public static <T> NullableField<T> ofValue(T value)
      Creates a new instance that is non-null, i.e. has a value.
      Type Parameters:
      T - the static type of the value
      Parameters:
      value - the value (must not be null)
      Returns:
      the new instance
    • ofNull

      public static <T> NullableField<T> ofNull()
      Creates a new instance that is null.
      Type Parameters:
      T - the static type of the missing value
      Returns:
      the new instance
    • ofValueOrNull

      public static <T> NullableField<T> ofValueOrNull(T value)
      Creates a new instance that is non-null if the argument is non-null, and null if the argument is null.
      Type Parameters:
      T - the static type of the value
      Parameters:
      value - the value or null
      Returns:
      the new instance
    • getValueOrNull

      public T getValueOrNull()
      Getter method for the value in this instance. Returns null if this instance is null.
      Returns:
      the value or null
    • getValue

      public T getValue()
      Getter method for the value in this instance. Throws an IllegalStateException if null.
      Returns:
      the value
    • isNonNull

      public boolean isNonNull()
      Checks if this instance is non-null.
      Returns:
      true if this instance is non-null, false if null
    • isNull

      public boolean isNull()
      Checks if this instance is null.
      Returns:
      true if this instance is null, false if non-null
    • orElse

      public T orElse(T other)
      Returns this field's value if isNonNull(), otherwise the argument.
      Parameters:
      other - the default to use if this field isNull()
      Returns:
      this value or the argument. Returns the null reference iff both this field isNull() and the argument is a null reference.
    • orElseGet

      public T orElseGet(Supplier<T> other)
      Returns this field's value if isNonNull(), otherwise a value obtained from the argument.
      Parameters:
      other - a supplier for the default to use if this field isNull(). Must not be a null reference.
      Returns:
      this value or the argument-provided value. Returns the null reference iff both this field isNull() and the argument provides a null reference.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object