Class OneOf2<A,​B>


  • public class OneOf2<A,​B>
    extends Object
    This is designed to represent a union of 2 types, meaning an object that can be one type, or another. Instead of a get() method, pass 2 functions to match(), one to handle the case where this contains the first thing, the other if it contains the second thing. In theory, this could work with two things of the same type, but Java has polymorphism to handle that more easily. Before using a OneOf2, make sure you don't really need a Tuple2. OneOf2 is designed to be sub-classed so you can add descriptive names. The safest way to use Union classes is to always call match() because it forces you to think about how to handle each type you could possibly receive. Usage:
    
    thingy.match(fst -> fst.doOneThing(),
                 sec -> sec.doSomethingElse());
    
    Sometimes it's a programming error to pass one type or another and you may want to throw an exception.
    
    oneOf.match(fst -> fst.doOneThing(),
                sec -> { throw new IllegalStateException("Asked for a 2nd; only had a 1st."); });
    
    For the shortest syntax and best names, define your own subclass. This is similar to sub-classing Tuples.
    
    static class String_Integer extends OneOf2<String,Integer> {
    
        // Private Constructor because the o parameter is not type safe.
        private String_Integer(Object o, int n) { super(o, String.class, Integer.class, n); }
    
        // Static factory methods ensure type-safe construction.
        public static String_Integer ofStr(String o) { return new String_Integer(o, 0); }
        public static String_Integer ofInt(Integer o) { return new String_Integer(o, 1); }
    }
    
    equals(), hashcode(), and toString() are all taken care of for you. Now you use descriptive and extremely brief syntax:
    
    // Type-safe switching - always works at runtime.
    x.match(s -> (s == null) ? null : s.lowerCase(),
            n -> "This is the number " + n);
    
    // If not a String at runtime throws "Expected a(n) String but found a(n) Integer"
    x.str().contains("goody!");
    
    // If not an Integer at runtime throws "Expected a(n) Integer but found a(n) String"
    3 + x.integer();
    
    • Constructor Detail

      • OneOf2

        protected OneOf2​(Object o,
                         Class<A> aClass,
                         Class<B> bClass,
                         int index)
        Protected constructor for subclassing. A, B, and C parameters can be null, but if one is non-null, the index must specify the non-null value (to keep you from assigning a bogus index value).
        Parameters:
        o - the item
        aClass - class 0 (to have at runtime for descriptive error messages and toString()).
        bClass - class 1 (to have at runtime for descriptive error messages and toString()).
        index - 0 means this represents an A, 1 represents a B, 2 represents a C, 3 means D
    • Method Detail

      • match

        public <R> R match​(Fn1<A,​R> fa,
                           Fn1<B,​R> fb)
        Languages that have union types built in have a match statement that works like this method. Exactly one of these functions will be executed - determined by which type of item this object holds.
        Parameters:
        fa - the function to be executed if this OneOf stores the first type.
        fb - the function to be executed if this OneOf stores the second type.
        Returns:
        the return value of whichever function is executed.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object