Class OptionalField<T>
java.lang.Object
name.martingeisse.grumpyjson.builtin.helper_types.OptionalField<T>
- Type Parameters:
T- the type of the contained value
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 Summary
Modifier and TypeMethodDescriptionbooleangetValue()Getter method for the value in this instance.Getter method for the value in this instance.inthashCode()booleanisAbsent()Checks if this instance is absent.booleanChecks if this instance is present.static <T> OptionalField<T>Creates a new instance that is absent.static <T> OptionalField<T>ofValue(T value) Creates a new instance that is present, i.e. has a value.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.Returns this field's value ifisPresent(), otherwise the argument.Returns this field's value ifisPresent(), otherwise a value obtained from the argument.toString()
-
Method Details
-
ofValue
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
Creates a new instance that is absent.- Type Parameters:
T- the static type of the missing value- Returns:
- the new instance
-
ofValueOrNullAsNothing
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
Getter method for the value in this instance. Returns null if this instance is absent.- Returns:
- the value or null
-
getValue
Getter method for the value in this instance. Throws anIllegalStateExceptionif 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
Returns this field's value ifisPresent(), otherwise the argument.- Parameters:
other- the default to use if this fieldisAbsent()- Returns:
- this value or the argument. Returns the null reference iff both this field
isAbsent()and the argument is a null reference.
-
orElseGet
Returns this field's value ifisPresent(), otherwise a value obtained from the argument.- Parameters:
other- a supplier for the default to use if this fieldisAbsent(). 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
-
hashCode
public int hashCode() -
toString
-