Package de.xima.fc.common.data.algebraic
Class OneOfN
- java.lang.Object
-
- de.xima.fc.common.data.algebraic.OneOfN
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
OneOfN.Either
,OneOfN.Just
,OneOfN.None
,OneOfN.OneOf3
,OneOfN.OneOf4
,OneOfN.OneOf5
public abstract class OneOfN extends Object implements Serializable
Base class for emulating algebraic sum types (also known as a discriminated union) of N elements.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
OneOfN.Either<T1,T2>
Emulates a sum type consisting of 2 cases.static interface
OneOfN.EitherMatcher<R,T1,T2>
A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.static class
OneOfN.Just<T1>
Emulates a sum type consisting of 1 case.static interface
OneOfN.JustMatcher<R,T1>
A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.static class
OneOfN.None
Emulates a sum type consisting of 0 cases.static interface
OneOfN.NoneMatcher<R>
A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.static class
OneOfN.OneOf3<T1,T2,T3>
Emulates a sum type consisting of 3 cases.static interface
OneOfN.OneOf3Matcher<R,T1,T2,T3>
A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.static class
OneOfN.OneOf4<T1,T2,T3,T4>
Emulates a sum type consisting of 4 cases.static interface
OneOfN.OneOf4Matcher<R,T1,T2,T3,T4>
A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.static class
OneOfN.OneOf5<T1,T2,T3,T4,T5>
Emulates a sum type consisting of 5 cases.static interface
OneOfN.OneOf5Matcher<R,T1,T2,T3,T4,T5>
A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
int
getChoice()
Object
getValue()
Checks which case applies and returns the value for that case.int
hashCode()
protected boolean
is(int choice)
protected Optional<Object>
select(int choice)
-
-
-
Field Detail
-
choice
protected final int choice
-
value
protected final Object value
-
-
Method Detail
-
getChoice
public int getChoice()
- Returns:
- The 0-based index representing the case for which a value is present.
-
getValue
public Object getValue()
Checks which case applies and returns the value for that case. You need to cast it yourself to the required type.- Returns:
- The value of this sum type.
-
is
protected final boolean is(int choice)
- Parameters:
choice
- Index of the case to check.- Returns:
- Whether a value is present for the given case.
-
-