Created
          June 24, 2016 22:20 
        
      - 
      
- 
        Save panicbit/fc48b71bb11d0be929864d6e71ced2ae to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package wt2.notes.util; | |
| import java.util.function.Supplier; | |
| import java.util.NoSuchElementException; | |
| import java.util.Optional; | |
| public abstract class Result<T, E> { | |
| public static <T, E> Ok<T, E> Ok(T value) { | |
| return new Ok<T, E>(value); | |
| } | |
| public static <T, E> Err<T, E> Err(E err) { | |
| return new Err<T, E>(err); | |
| } | |
| public abstract boolean isOk(); | |
| public abstract T get(); | |
| public abstract E getErr(); | |
| public boolean isErr() { return !this.isOk(); }; | |
| public <U> Result<U, E> map(FunctionE<? super T, ? extends U> f) throws Exception { | |
| if (this.isOk()) { | |
| return Ok(f.apply(this.get())); | |
| } | |
| else { | |
| return Err(this.getErr()); | |
| } | |
| } | |
| public <U> Result<T, U> mapErr(FunctionE<? super E, ? extends U> f) throws Exception { | |
| if (this.isErr()) { | |
| return Err(f.apply(this.getErr())); | |
| } | |
| else { | |
| return Ok(this.get()); | |
| } | |
| } | |
| public Result<T, E> ifOk(ConsumerE<? super T> f) throws Exception { | |
| if (this.isOk()) { | |
| f.accept(this.get()); | |
| } | |
| return this; | |
| } | |
| public Result<T, E> ifErr(ConsumerE<? super E> f) throws Exception { | |
| if (this.isErr()) { | |
| f.accept(this.getErr()); | |
| } | |
| return this; | |
| } | |
| public <U> Result<U, E> flatMap(FunctionE<? super T, Result<U, E>> f) throws Exception { | |
| if (this.isOk()) { | |
| return f.apply(this.get()); | |
| } else { | |
| return Err(this.getErr()); | |
| } | |
| } | |
| public Optional<T> toOptional() { | |
| if (this.isOk()) { | |
| return Optional.of(this.get()); | |
| } else { | |
| return Optional.empty(); | |
| } | |
| } | |
| public T getOr(T value) { | |
| System.out.println("Value"); | |
| return value; | |
| } | |
| public T getOr(Supplier<T> f) { | |
| System.out.println("Supplier"); | |
| return f.get(); | |
| } | |
| public static class Ok<T, E> extends Result<T, E> { | |
| private T value; | |
| Ok(T value) { this.value = value; } | |
| @Override public boolean isOk() { return true; } | |
| @Override public T get() { return this.value; } | |
| @Override public E getErr() { throw new NoSuchElementException("called Ok.getErr()"); } | |
| } | |
| public static class Err<T, E> extends Result<T, E> { | |
| private E err; | |
| Err(E err) { this.err = err; } | |
| @Override public boolean isOk() { return false; } | |
| @Override public T get() { throw new NoSuchElementException("called Err.get()"); } | |
| @Override public E getErr() {return this.err; } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment