T1 - type of the first possible valueT2 - type of the second possible valuepublic abstract class NullableUnion<T1,T2> extends UnionBase<T1,T2>
union
structure as defined in the C programming language.
This class allows no overriding to guarantee the following invariances:
u.has1() ^ u.has2() is always trueThere is exactly one way to construct a union of either type with valid values:
t1(Object) will construct a union which contains a value of type T1t2(Object) will construct a union which contains a value of type T2null values:
nul1() will return a static union which contains a null value of type T1nul2() will return a static union which contains a null value of type T2null.
Any consumers should be prepared for null values.
See Union for an implementation which does not allow for null values.
It is more efficient to use the following branchless methods for handling unions instead of combinations of UnionBase.has1()/get1() and UnionBase.has2()/get2():
UnionBase.dispose(Consumer, Consumer) to handle the value wrapped in this union.
Note that the consumers must expect null arguments.
UnionBase.test(Predicate, Predicate) to perform a test on the values in this union.
Note that the predicates must expect null arguments.
UnionBase.condense(Function, Function) to combine both types of values into one common outcoming value.
Note that the functions must expect null arguments.
String txt = u.condense(Objects::toString, Objects::toString);
(note that Objects::toString is used instead of Object::toString (look for an 's')
because of possible null values).| Modifier and Type | Method and Description |
|---|---|
abstract T1 |
get1()
Get the value if it is of type
T1. |
abstract T2 |
get2()
Get the value if it is of type
T2. |
java.lang.Object |
getValue()
Get the value contained in this union.
|
static <V1,V2> NullableUnion<V1,V2> |
nul1()
Get a union with a first value of
null. |
static <V1,V2> NullableUnion<V1,V2> |
nul2()
Get a union with a second value of
null. |
static <V1,V2> NullableUnion<V1,V2> |
t1(V1 v1)
Create a union containing a value of type V1.
|
static <V1,V2> NullableUnion<V1,V2> |
t2(V2 v2)
Create a union containing a value of type V2.
|
condense, dispose, disposeFragile1, disposeFragile2, disposeOnly1, disposeOnly2, has1, has2, isEqual1, isEqual2, map, swap, test@Nullable public abstract T1 get1() throws java.lang.IllegalStateException
T1.
Throw an exception otherwise.get1 in class UnionBase<T1,T2>T1, possibly nulljava.lang.IllegalStateException - if this union does contain a value of type T2UnionBase.has1()@Nullable public abstract T2 get2() throws java.lang.IllegalStateException
T2.
Throw an exception otherwise.get2 in class UnionBase<T1,T2>T2, possibly nulljava.lang.IllegalStateException - if this union does contain a value of type T1UnionBase.has1()@Nullable public java.lang.Object getValue()
@NotNull public static <V1,V2> NullableUnion<V1,V2> t1(@Nullable V1 v1)
V1 - value type of contained valueV2 - implicitv1 - value contained in this unionv1@NotNull public static <V1,V2> NullableUnion<V1,V2> t2(@Nullable V2 v2)
V1 - implicitV2 - value type of contained valuev2 - value contained in this unionv2@NotNull public static <V1,V2> NullableUnion<V1,V2> nul1()
null.V1 - first possible typeV2 - second possible typenull value of type V1@NotNull public static <V1,V2> NullableUnion<V1,V2> nul2()
null.V1 - first possible typeV2 - second possible typenull value of type V2