All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.util.Either Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.util;

import java.util.NoSuchElementException;

public abstract class Either {
   public enum Type {
      LEFT, RIGHT
   }

   public static  Either newLeft(A a) {
      return new Left<>(a);
   }

   public static  Either newRight(B b) {
      return new Right<>(b);
   }

   public abstract Type type();
   public abstract A left();
   public abstract B right();

   private static class Left extends Either {
      private final A leftValue;
      Left(A a) { leftValue = a; }
      @Override public Type type() { return Type.LEFT; }
      @Override public A left() { return leftValue; }
      @Override public B right() { throw new NoSuchElementException("Either.right() called on Left"); }

      @Override
      public String
      toString() {
         return "Left(" + leftValue + ')';
      }
   }

   private static class Right extends Either {
      private final B rightValue;
      Right(B b) { rightValue = b; }
      @Override public Type type() { return Type.RIGHT; }
      @Override public A left() { throw new NoSuchElementException("Either.left() called on Right"); }
      @Override public B right() {  return rightValue; }

      @Override
      public String
      toString() {
         return "Right(" + rightValue + ')';
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy