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

hydra.mantle.Either Maven / Gradle / Ivy

package hydra.mantle;

import java.io.Serializable;

/**
 * A disjoint union between a 'left' type and a 'right' type
 */
public abstract class Either implements Serializable {
  public static final hydra.core.Name NAME = new hydra.core.Name("hydra/mantle.Either");
  
  private Either () {
  
  }
  
  public abstract  R accept(Visitor visitor) ;
  
  public interface Visitor {
    R visit(Left instance) ;
    
    R visit(Right instance) ;
  }
  
  public interface PartialVisitor extends Visitor {
    default R otherwise(Either instance) {
      throw new IllegalStateException("Non-exhaustive patterns when matching: " + (instance));
    }
    
    default R visit(Left instance) {
      return otherwise((instance));
    }
    
    default R visit(Right instance) {
      return otherwise((instance));
    }
  }
  
  public static final class Left extends hydra.mantle.Either implements Serializable {
    public final A value;
    
    public Left (A value) {
      this.value = value;
    }
    
    @Override
    public boolean equals(Object other) {
      if (!(other instanceof Left)) {
        return false;
      }
      Left o = (Left) (other);
      return value.equals(o.value);
    }
    
    @Override
    public int hashCode() {
      return 2 * value.hashCode();
    }
    
    @Override
    public  R accept(Visitor visitor) {
      return visitor.visit(this);
    }
  }
  
  public static final class Right extends hydra.mantle.Either implements Serializable {
    public final B value;
    
    public Right (B value) {
      this.value = value;
    }
    
    @Override
    public boolean equals(Object other) {
      if (!(other instanceof Right)) {
        return false;
      }
      Right o = (Right) (other);
      return value.equals(o.value);
    }
    
    @Override
    public int hashCode() {
      return 2 * value.hashCode();
    }
    
    @Override
    public  R accept(Visitor visitor) {
      return visitor.visit(this);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy