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 classOneOfN.Either<T1,T2>Emulates a sum type consisting of 2 cases.static interfaceOneOfN.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 classOneOfN.Just<T1>Emulates a sum type consisting of 1 case.static interfaceOneOfN.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 classOneOfN.NoneEmulates a sum type consisting of 0 cases.static interfaceOneOfN.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 classOneOfN.OneOf3<T1,T2,T3>Emulates a sum type consisting of 3 cases.static interfaceOneOfN.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 classOneOfN.OneOf4<T1,T2,T3,T4>Emulates a sum type consisting of 4 cases.static interfaceOneOfN.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 classOneOfN.OneOf5<T1,T2,T3,T4,T5>Emulates a sum type consisting of 5 cases.static interfaceOneOfN.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 booleanequals(Object o)intgetChoice()protected ObjectgetOrThrow(int choice)ObjectgetValue()Checks which case applies and returns the value for that case.inthashCode()protected booleanis(int choice)protected Optional<Object>select(int choice)StringtoString()
-
-
-
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.
-
getOrThrow
protected final Object getOrThrow(int choice)
- Parameters:
choice- Index of the case to get.- Returns:
- The value of the given case, if present.
- Throws:
IllegalArgumentException- If the given case is not present.
-
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.
-
-