mirror of
https://github.com/xoureldeen/Vectras-VM-Android.git
synced 2026-05-03 08:40:23 +00:00
48 lines
No EOL
1.2 KiB
Java
48 lines
No EOL
1.2 KiB
Java
package com.vectras.vm.data;
|
|
|
|
/**
|
|
* A generic class that holds a result success w/ data or an error exception.
|
|
*/
|
|
public class Result<T> {
|
|
// hide the private constructor to limit subclass types (Success, Error)
|
|
private Result() {
|
|
}
|
|
|
|
@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 {
|
|
private T data;
|
|
|
|
public Success(T data) {
|
|
this.data = data;
|
|
}
|
|
|
|
public T getData() {
|
|
return this.data;
|
|
}
|
|
}
|
|
|
|
// Error sub-class
|
|
public final static class Error extends Result {
|
|
private Exception error;
|
|
|
|
public Error(Exception error) {
|
|
this.error = error;
|
|
}
|
|
|
|
public Exception getError() {
|
|
return this.error;
|
|
}
|
|
}
|
|
} |