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: 9.1.7.Final
Show newest version
package org.infinispan.commons.util;

import java.util.NoSuchElementException;

public abstract class Either {

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

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

   public static enum Type {
      LEFT, RIGHT
   }

   private static class Left extends Either {
      private 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"); }
   }

   private static class Right extends Either {
      private 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; }
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy