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.

LuPromise.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.luticate.utils.business;
  2. import com.luticate.utils.dbo.LuPromiseError;
  3. import java.util.List;
  4. import java.util.Vector;
  5. /**
  6. *
  7. * Created by robin on 11/27/15.
  8. */
  9. public class LuPromise<T> {
  10. public enum LuPromiseStatus {
  11. Running,
  12. Resolved,
  13. Failed
  14. }
  15. private List<LuConsumer<T>> _onSuccess = new Vector<>();
  16. private List<LuConsumer<LuPromiseError>> _onFailed = new Vector<>();
  17. protected LuPromiseStatus _status = LuPromiseStatus.Running;
  18. protected T _data = null;
  19. protected LuPromiseError _error = null;
  20. public LuPromiseStatus getStatus()
  21. {
  22. return _status;
  23. }
  24. public T getData()
  25. {
  26. return _data;
  27. }
  28. public LuPromiseError getError()
  29. {
  30. return _error;
  31. }
  32. public LuPromise<T> then(LuConsumer<T> onSuccess, LuConsumer<LuPromiseError> onFailed)
  33. {
  34. _onFailed.add(onFailed);
  35. if (_status == LuPromiseStatus.Failed) {
  36. if (onFailed != null) {
  37. onFailed.execute(_error);
  38. }
  39. }
  40. return then(onSuccess);
  41. }
  42. public LuPromise<T> then(LuConsumer<T> onSuccess)
  43. {
  44. _onSuccess.add(onSuccess);
  45. if (_status == LuPromiseStatus.Resolved) {
  46. if (onSuccess != null) {
  47. onSuccess.execute(_data);
  48. }
  49. }
  50. return this;
  51. }
  52. public LuPromise<T> resolve(T data)
  53. {
  54. if (_status == LuPromiseStatus.Running) {
  55. _data = data;
  56. _error = null;
  57. _status = LuPromiseStatus.Resolved;
  58. onSuccess();
  59. }
  60. return this;
  61. }
  62. public LuPromise<T> reject(LuPromiseError error)
  63. {
  64. if (_status == LuPromiseStatus.Running) {
  65. _data = null;
  66. _error = error;
  67. _status = LuPromiseStatus.Failed;
  68. onFailed();
  69. }
  70. return this;
  71. }
  72. public <T2> LuPromise<T2> map(final LuConverter<T, T2> converter)
  73. {
  74. final LuPromise<T2> promise = new LuPromise<>();
  75. then(new LuConsumer<T>() {
  76. @Override
  77. public void execute(T data) {
  78. promise.resolve(converter.convert(data));
  79. }
  80. }, getRejectConsumer());
  81. return promise;
  82. }
  83. public LuConsumer<LuPromiseError> getRejectConsumer()
  84. {
  85. return new LuConsumer<LuPromiseError>() {
  86. @Override
  87. public void execute(LuPromiseError error) {
  88. reject(error);
  89. }
  90. };
  91. }
  92. protected void onSuccess()
  93. {
  94. for (LuConsumer<T> c : _onSuccess) {
  95. if (c != null) {
  96. c.execute(_data);
  97. }
  98. }
  99. }
  100. protected void onFailed()
  101. {
  102. for (LuConsumer<LuPromiseError> c : _onFailed) {
  103. if (c != null) {
  104. c.execute(_error);
  105. }
  106. }
  107. }
  108. }