T1
- type of the first possible valueT2
- type of the second possible valuepublic abstract class Union<T1,T2> extends UnionBase<T1,T2>
This is sometimes useful to transport errors from one part to the other, when
throwing them is no option. This is a type-safe implementation of a 2-member 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:
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
This implementation does not allow null
values.
Any consumers can safely assume that the values they achieve are non-null
.
See NullableUnion
if support for null
is required.
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 java.lang.Object |
getValue()
Get the value contained in this union.
|
static <V1,V2> Union<V1,V2> |
t1(V1 v1)
Create a union containing a value of type V1.
|
static <V1,V2> Union<V1,V2> |
t2(V2 v2)
Create a union containing a value of type V2.
|
dispose, disposeFragile1, disposeFragile2, disposeOnly1, disposeOnly2, has1, has2, isEqual1, isEqual2
@NotNull public abstract T1 get1() throws java.lang.IllegalStateException
T1
.
Throw an exception otherwise.get1
in class UnionBase<T1,T2>
T1
java.lang.IllegalStateException
- if this union does contain a value of type T2
UnionBase.has1()
@NotNull public abstract T2 get2() throws java.lang.IllegalStateException
T2
.
Throw an exception otherwise.get2
in class UnionBase<T1,T2>
T2
java.lang.IllegalStateException
- if this union does contain a value of type T1
UnionBase.has1()
@NotNull public abstract java.lang.Object getValue()
@NotNull public static <V1,V2> Union<V1,V2> t1(@NotNull V1 v1)
V1
- value type of contained valueV2
- implicitv1
- value contained in this unionv1