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 true
There 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 T1
t2(Object)
will construct a union which contains a value of type T2
null
values:
nul1()
will return a static union which contains a null
value of type T1
nul2()
will return a static union which contains a null
value of type T2
null
.
Any consumers should be prepared for null
values.
See Union
for an implementation which does not allow for 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 . |
abstract void |
handle(java.util.function.Consumer<? super T1> handler1,
java.util.function.Consumer<? super T2> handler2)
Call the appropriate handler depending on this unions' value.
|
abstract NullableUnion<T1,T2> |
handle1(java.util.function.Consumer<? super T1> handler)
Call the given handler if this union contains a value of type
T1 . |
abstract NullableUnion<T1,T2> |
handle2(java.util.function.Consumer<? super T2> handler)
Call the given handler if this union contains a value of type
T2 . |
abstract <E extends java.lang.Exception> |
handleFragile1(FragileProcedure1<E,? super T1> handler)
Call the given handler if this union contains a value of type
T1 . |
abstract <E extends java.lang.Exception> |
handleFragile2(FragileProcedure1<E,? super T2> handler)
Call the given handler if this union contains a value of type
T2 . |
abstract boolean |
has1()
Does this union contain a value of type
T1 ? |
abstract boolean |
has2()
Does this union contain a value of type
T2 ? |
static <V1,V2> NullableUnion<V1,V2> |
nul1()
Get a union with a first type of
null . |
static <V1,V2> NullableUnion<V1,V2> |
nul2()
Get a union with a second type 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.
|
public abstract boolean has1()
T1
?public abstract boolean has2()
T2
?@Nullable public abstract T1 get1() throws java.lang.IllegalStateException
T1
.
Throw an exception otherwise.@Nullable public abstract T2 get2() throws java.lang.IllegalStateException
T2
.
Throw an exception otherwise.public abstract void handle(@NotNull java.util.function.Consumer<? super T1> handler1, @NotNull java.util.function.Consumer<? super T2> handler2)
@NotNull public abstract NullableUnion<T1,T2> handle1(@NotNull java.util.function.Consumer<? super T1> handler)
T1
.@NotNull public abstract NullableUnion<T1,T2> handle2(@NotNull java.util.function.Consumer<? super T2> handler)
T2
.@NotNull public abstract <E extends java.lang.Exception> NullableUnion<T1,T2> handleFragile1(@NotNull FragileProcedure1<E,? super T1> handler) throws E extends java.lang.Exception
T1
.
Allows for a handler which might throw a checked exception.handleFragile1
in class UnionBase<T1,T2>
E
- exception the handler might throwhandler
- called with the value of this union if the contained type is T1
, not called otherwiseE
- if handler
throws itE extends java.lang.Exception
@NotNull public abstract <E extends java.lang.Exception> NullableUnion<T1,T2> handleFragile2(@NotNull FragileProcedure1<E,? super T2> handler) throws E extends java.lang.Exception
T2
.
Allows for a handler which might throw a checked exception.handleFragile2
in class UnionBase<T1,T2>
E
- exception the handler might throwhandler
- called with the value of this union if the contained type is T2
, not called otherwiseE
- if handler
throws itE extends java.lang.Exception
@NotNull public static <V1,V2> NullableUnion<V1,V2> t1(V1 v1)
V1
- value type of contained valueV2
- implicitv1
- value contained in this unionv1
@NotNull public static <V1,V2> NullableUnion<V1,V2> t2(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 typeV1
, with a value of null
@NotNull public static <V1,V2> NullableUnion<V1,V2> nul2()
null
.V1
- first possible typeV2
- second possible typeV2
, with a value of null