Package discord4j.discordjson.possible
Class Possible<T>
java.lang.Object
discord4j.discordjson.possible.Possible<T>
- Type Parameters:
T- The type of the stored value.
A container for a value which may or may not be absent. If
isAbsent() is true, no value is present.
Possible is extremely similar to Optional and exists solely for (de)serialization purposes.
By combining Possible and Optional, 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
Modifier and TypeMethodDescriptionstatic <T> Possible<T>absent()Returns aPossiblewith no value.booleanstatic <T> Optional<T>Flattens aPossible<Optional<T>>into aOptional<T>.get()Gets the stored value, if any.inthashCode()booleanisAbsent()If no value is stored, returns true, otherwise false.<R> Possible<R>Map the current data to a new type.static <T> Possible<T>of(T value) Returns aPossiblewith the given value.Converts thisPossibleto anOptional.toString()
-
Method Details
-
of
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
Returns aPossiblewith no value.- Type Parameters:
T- The type of the non-existent value.- Returns:
- An absent
Possible.
-
flatOpt
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
Gets the stored value, if any.- Returns:
- The stored value, if any.
- Throws:
NoSuchElementException- If no value is stored.
-
toOptional
Converts thisPossibleto anOptional. The returnedOptionalis present iff thisPossibleis not absent.- Returns:
- An
Optionalcontaining thisPossible's value, if it has one.
-
equals
-
hashCode
public int hashCode() -
toString
-
map
Map the current data to a new type.- Type Parameters:
R- the new type- Parameters:
mapper- the function to map the current data to a new type- Returns:
- a new Possible with the mapped data
-