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:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Emulates a sum type consisting of 2 cases.
    static interface 
    A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.
    static final class 
    Emulates a sum type consisting of 1 case.
    static interface 
    A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.
    static final class 
    Emulates a sum type consisting of 0 cases.
    static interface 
    A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.
    static final class 
    Emulates a sum type consisting of 3 cases.
    static interface 
    A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.
    static final class 
    Emulates a sum type consisting of 4 cases.
    static interface 
    A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.
    static final class 
    Emulates a sum type consisting of 5 cases.
    static interface 
    A matcher object that contains a handler for each case and can be used to reduce a sum type to a single value.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final int
     
    protected final Object
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    int
     
    protected final Object
    getOrThrow(int choice)
     
    Checks which case applies and returns the value for that case.
    int
     
    protected final boolean
    is(int choice)
     
    protected final Optional<Object>
    select(int choice)
     
    final String
     

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • choice

      protected final int choice
    • value

      protected final Object value
  • Method Details

    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • 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.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • 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.
    • select

      protected final Optional<Object> select(int choice)
      Parameters:
      choice - Index of the case to get.
      Returns:
      The value of the given case, if present.