Result.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package me.yoqi.android.netauth.data;
  2. /**
  3. * A generic class that holds a result success w/ data or an error exception.
  4. */
  5. public class Result<T> {
  6. // hide the private constructor to limit subclass types (Success, Error)
  7. private Result() {
  8. }
  9. @Override
  10. public String toString() {
  11. if (this instanceof Result.Success) {
  12. Result.Success success = (Result.Success) this;
  13. return "Success[data=" + success.getData().toString() + "]";
  14. } else if (this instanceof Result.Error) {
  15. Result.Error error = (Result.Error) this;
  16. return "Error[exception=" + error.getError().toString() + "]";
  17. }
  18. return "";
  19. }
  20. // Success sub-class
  21. public final static class Success<T> extends Result {
  22. private T data;
  23. public Success(T data) {
  24. this.data = data;
  25. }
  26. public T getData() {
  27. return this.data;
  28. }
  29. }
  30. // Error sub-class
  31. public final static class Error extends Result {
  32. private Exception error;
  33. public Error(Exception error) {
  34. this.error = error;
  35. }
  36. public Exception getError() {
  37. return this.error;
  38. }
  39. }
  40. }