Created
May 6, 2022 05:13
-
-
Save ueno1969/20ea863b0fe19ef552d478d120cd5f40 to your computer and use it in GitHub Desktop.
Result
This file contains 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
// Use JDK 17 preview | |
public class Main { | |
public static void main(String[] args) { | |
new Main().sample(); | |
new Main().sample2(); | |
} | |
private static Result<Integer, RuntimeException> parse(String a) { | |
try { | |
return Result.ok(Integer.parseInt(a)); | |
} catch (NumberFormatException e) { | |
return Result.err(e); | |
} | |
} | |
private static Integer calc(Integer a, Integer b) { | |
return a + b; | |
} | |
private static Result<Double, RuntimeException> div(Integer a, Integer b) { | |
if (b == 0) { | |
return Result.err(new RuntimeException("divide by zero")); | |
} else { | |
return Result.ok(a.doubleValue() / b.doubleValue()); | |
} | |
} | |
private void sample() { | |
var a = "100"; | |
Result<Integer, RuntimeException> result = | |
parse(a) | |
.map(x -> calc(x , 20)) | |
.map(x -> calc(x, 3)) | |
.flatMap(x -> div(x, 2)) | |
.map(x -> calc(x.intValue(), 1000)); | |
switch (result) { | |
case Result.Ok v: | |
System.out.println("Success Result : " + v.getValue()); | |
break; | |
case Result.Err err: | |
System.out.println("Error Result : " + err.getError()); | |
break; | |
default: | |
throw new RuntimeException("Not reach."); | |
} | |
System.out.println("Finish"); | |
} | |
private void sample2() { | |
Result<Integer, RuntimeException> a = Result.ok(1); | |
var result = | |
a.flatMap(x -> div(x, 0)) | |
.map(x -> calc(x.intValue(), 1000)); | |
System.out.println(result.getOrElse(9999)); | |
System.out.println(result.getOrElse(() -> 9999)); | |
System.out.println("Finish"); | |
} | |
} |
This file contains 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
import java.util.function.Function; | |
import java.util.function.Supplier; | |
public abstract class Result<T,E extends Throwable> { | |
public static <T,E extends Throwable> Ok<T,E> ok(T a) { return new Ok<>(a); } | |
public static <T,E extends Throwable> Err<T,E> err(E a) { return new Err<>(a); } | |
public abstract T unwrap() throws E; | |
public abstract boolean isOk(); | |
public abstract boolean isErr(); | |
public abstract T getValue(); | |
public abstract E getError(); | |
public abstract T getOrElse(T defaultValue); | |
public abstract T getOrElse(Supplier<T> defaultFunc); | |
public abstract <R> Result<R, E> map(Function<T, R> func); | |
public abstract <R> Result<R, E> flatMap(Function<T, Result<R, E>> func); | |
public static class Ok<T,E extends Throwable> extends Result<T,E> { | |
private final T val; | |
public Ok(T val) { this.val = val; } | |
public T unwrap() { return this.val; } | |
public boolean isOk() { return true; } | |
public boolean isErr() { return false; } | |
public T getValue() { return this.val; } | |
public E getError() { return null; } | |
@Override | |
public T getOrElse(T defaultValue) { return this.val; } | |
@Override | |
public T getOrElse(Supplier<T> defaultFunc) { return this.val; } | |
public <R> Result<R, E> map(Function<T, R> func) { | |
return Result.ok(func.apply(getValue())); | |
} | |
public <R> Result<R, E> flatMap(Function<T, Result<R, E>> func) { | |
return func.apply(getValue()); | |
} | |
@Override | |
public String toString() { return getClass().getName() + "[" + val + "]"; } | |
} | |
public static class Err<T,E extends Throwable> extends Result<T,E> { | |
private final E err; | |
public Err(E err) { this.err = err; } | |
public T unwrap() throws E { throw this.err; } | |
public boolean isOk() { return false; } | |
public boolean isErr() { return true; } | |
public T getValue() { return null; } | |
public E getError() { return this.err; } | |
@Override | |
public T getOrElse(T defaultValue) { return defaultValue; } | |
@Override | |
public T getOrElse(Supplier<T> defaultFunc) { return defaultFunc.get(); } | |
public <R> Result<R, E> map(Function<T, R> func) { | |
return Result.err(this.getError()); | |
} | |
public <R> Result<R, E> flatMap(Function<T, Result<R, E>> func) { | |
return Result.err(this.getError()); | |
} | |
@Override | |
public String toString() { return getClass().getName() + "[" + err + "]"; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment