Class OptionalField<T>

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

public final class OptionalField<T> extends Object
This can be used to wrap a property and allow it to be absent in JSON. Normally, a missing property in the JSON will cause a deserialization error.

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

An optional 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 missing JSON property is mapped to and from an OptionalField that isAbsent().

  • Method Details

    • ofValue

      public static <T> OptionalField<T> ofValue(T value)
      Creates a new instance that is present, 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
    • ofNothing

      public static <T> OptionalField<T> ofNothing()
      Creates a new instance that is absent.
      Type Parameters:
      T - the static type of the missing value
      Returns:
      the new instance
    • ofValueOrNullAsNothing

      public static <T> OptionalField<T> ofValueOrNullAsNothing(T value)
      Creates a new instance that is present if the argument is non-null, and absent if the argument is null.
      Type Parameters:
      T - the static type of the value
      Parameters:
      value - the value or null
      Returns:
      the new instance
    • getValueOrNothingAsNull

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

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

      public boolean isPresent()
      Checks if this instance is present.
      Returns:
      true if this instance is present, false if absent
    • isAbsent

      public boolean isAbsent()
      Checks if this instance is absent.
      Returns:
      true if this instance is absent, false if present
    • orElse

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

      public T orElseGet(Supplier<T> other)
      Returns this field's value if isPresent(), otherwise a value obtained from the argument.
      Parameters:
      other - a supplier for the default to use if this field isAbsent(). Must not be a null reference.
      Returns:
      this value or the argument-provided value. Returns the null reference iff both this field isAbsent() 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