Skip to content

Instantly share code, notes, and snippets.

@hkusu
Created May 26, 2017 05:00
Show Gist options
  • Save hkusu/2fa623ee434f614f3ba8f50020577a12 to your computer and use it in GitHub Desktop.
Save hkusu/2fa623ee434f614f3ba8f50020577a12 to your computer and use it in GitHub Desktop.
package jp.sample.android.lib.type;
import android.support.annotation.NonNull;
public final class SuccessFailure<Success, Failure> {
public interface SuccessResult<Success> {
void result(Success successResult);
}
public interface FailureResult<Failure> {
void result(Failure failureResult);
}
private enum Type {
SUCCESS,
FAILURE,
}
private final Type type;
private final Success successResult;
private final Failure failureResult;
private SuccessFailure(Type type, Success successResult, Failure failureResult) {
this.type = type;
this.successResult = successResult;
this.failureResult = failureResult;
}
public static <Success, Failure> SuccessFailure<Success, Failure> success(@NonNull Success successResult) {
return new SuccessFailure<>(Type.SUCCESS, successResult, null);
}
public static <Success, Failure> SuccessFailure<Success, Failure> failure(@NonNull Failure failureResult) {
return new SuccessFailure<>(Type.FAILURE, null, failureResult);
}
public void apply(SuccessResult<Success> successResult, FailureResult<Failure> failureResult) {
if (type == Type.SUCCESS) {
successResult.result(this.successResult);
} else if (type == Type.FAILURE) {
failureResult.result(this.failureResult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment