Package discord4j.discordjson.possible
Class Possible<T>
- java.lang.Object
-
- discord4j.discordjson.possible.Possible<T>
-
- Type Parameters:
T- The type of the stored value.
public final class Possible<T> extends Object
A container for a value which may or may not be absent. IfisAbsent()is true, no value is present.Possibleis extremely similar toOptionaland exists solely for (de)serialization purposes. By combiningPossibleandOptional, we can model the four different possibilities for field "types" used by Discord.Field name (in Discord docs) Type (in Discord docs) Java Type Meaning normal_field T T Always present, never null optional_field? T Possible<T>Sometimes present, but if present, never null nullable_field ?T Optional<T>Always present, but possibly null optional_and_nullable_field? ?T Possible<Optional<T>>Somtimes present, and even if present, possibly null You probably don't need to use this class!
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> Possible<T>absent()Returns aPossiblewith no value.booleanequals(Object o)static <T> Optional<T>flatOpt(Possible<Optional<T>> possible)Flattens aPossible<Optional<T>>into aOptional<T>.Tget()Gets the stored value, if any.inthashCode()booleanisAbsent()If no value is stored, returns true, otherwise false.static <T> Possible<T>of(T value)Returns aPossiblewith the given value.Optional<T>toOptional()Converts thisPossibleto anOptional.StringtoString()
-
-
-
Method Detail
-
of
public static <T> Possible<T> of(T value)
Returns aPossiblewith the given value.- Type Parameters:
T- The type the value.- Parameters:
value- The value the returned Possible will store.- Returns:
- A
Possiblewith the given value.
-
absent
public static <T> Possible<T> absent()
Returns aPossiblewith no value.- Type Parameters:
T- The type of the non-existent value.- Returns:
- An absent
Possible.
-
flatOpt
public static <T> Optional<T> flatOpt(Possible<Optional<T>> possible)
Flattens aPossible<Optional<T>>into aOptional<T>. The returnedOptionalcontains a value iff the givenPossibleis not absent and its storedOptionalis present.- Type Parameters:
T- The type of the inner value.- Parameters:
possible- ThePossibleto flatten.- Returns:
- An
Optionalcontaining if the inner value, if any.
-
isAbsent
public boolean isAbsent()
If no value is stored, returns true, otherwise false.- Returns:
- True if no value is stored, otherwise false.
-
get
public T get()
Gets the stored value, if any.- Returns:
- The stored value, if any.
- Throws:
NoSuchElementException- If no value is stored.
-
toOptional
public Optional<T> toOptional()
Converts thisPossibleto anOptional. The returnedOptionalis present iff thisPossibleis not absent.- Returns:
- An
Optionalcontaining thisPossible's value, if it has one.
-
-