Class NullableField<T>
java.lang.Object
name.martingeisse.grumpyjson.builtin.helper_types.NullableField<T>
- Type Parameters:
T- the type of the contained value
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 Summary
Modifier and TypeMethodDescriptionbooleangetValue()Getter method for the value in this instance.Getter method for the value in this instance.inthashCode()booleanChecks if this instance is non-null.booleanisNull()Checks if this instance is null.static <T> NullableField<T>ofNull()Creates a new instance that is null.static <T> NullableField<T>ofValue(T value) Creates a new instance that is non-null, i.e. has a value.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.Returns this field's value ifisNonNull(), otherwise the argument.Returns this field's value ifisNonNull(), otherwise a value obtained from the argument.toString()
-
Method Details
-
ofValue
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
Creates a new instance that is null.- Type Parameters:
T- the static type of the missing value- Returns:
- the new instance
-
ofValueOrNull
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
Getter method for the value in this instance. Returns null if this instance is null.- Returns:
- the value or null
-
getValue
Getter method for the value in this instance. Throws anIllegalStateExceptionif 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
Returns this field's value ifisNonNull(), otherwise the argument. -
orElseGet
Returns this field's value ifisNonNull(), otherwise a value obtained from the argument. -
equals
-
hashCode
public int hashCode() -
toString
-