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 subclassed to 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();
    
    Instead of putting a Null object in here, either return a null OneOf2 or wrap OneOf2 in an Option
    • Field Detail

      • item

        @NotNull
        protected final @NotNull Object item
    • Constructor Detail

      • OneOf2

        protected OneOf2​(@NotNull
                         @NotNull Object o,
                         @NotNull
                         @NotNull Class<A> aClass,
                         @NotNull
                         @NotNull Class<B> bClass,
                         int index)
        Protected constructor for subclassing. Be extremely careful to pass the correct index!
        Parameters:
        o - the item
        aClass - class 0
        bClass - class 1
        index - 0 means this represents an A, 1 represents a B
    • Method Detail

      • match

        public <R> R match​(@NotNull
                           @NotNull Fn1<A,​R> fa,
                           @NotNull
                           @NotNull 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 - applied iff this stores the first type.
        fb - applied iff this stores the second type.
        Returns:
        the return value of whichever function is executed.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object