You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

package com.community.pocket.data.login;
import org.jetbrains.annotations.NotNull;
/**
* A generic class that holds a result success w/ data or an error exception.
* 一个泛型类,它持有一个结果成功w/数据或一个错误异常。
*/
public class Result<T> {
// hide the private constructor to limit subclass types (Success, Error)
private Result() {
}
@NotNull
@Override
public String toString() {
if (this instanceof Result.Success) {
Result.Success success = (Result.Success) this;
return "Success[data=" + success.getData().toString() + "]";
} else if (this instanceof Result.Error) {
Result.Error error = (Result.Error) this;
return "Error[exception=" + error.getError().toString() + "]";
}
return "";
}
// Success sub-class
public final static class Success<T> extends Result<T> {
private T data;
Success(T data) {
this.data = data;
}
public T getData() {
return this.data;
}
}
// Error sub-class
final static class Error<T> extends Result<T> {
private Exception error;
private Class<T> c;
Error(Exception error, Class<T> c) {
this.error = error;
this.c = c;
}
Exception getError() {
return this.error;
}
public Class<T> getC() {
return c;
}
}
}