Created
May 4, 2016 15:28
-
-
Save pablisco/aae67a54242bd522776b43f5176ad4e8 to your computer and use it in GitHub Desktop.
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
public final class Result<S, F> { | |
private final Optional<S> success; | |
private final Optional<F> failure; | |
public Result(Optional<S> success, Optional<F> failure) { | |
this.success = success; | |
this.failure = failure; | |
} | |
public static <S, F> Result<S, F> success(S success) { | |
return new Result<>(Optional.of(success), Optional.empty()); | |
} | |
public static <S, F> Result<S, F> failure(F failure) { | |
return new Result<>(Optional.empty(), Optional.of(failure)); | |
} | |
public Optional<S> getSuccess() { | |
return success; | |
} | |
public Optional<F> getFailure() { | |
return failure; | |
} | |
public Result<S, F> onSuccess(Consumer<S> consumer) { | |
success.ifPresent(consumer); | |
return this; | |
} | |
public Result<S, F> onFailure(Consumer<F> consumer) { | |
failure.ifPresent(consumer); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment