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.

Callbacks.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2013, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. */
  14. package com.google.gwt.query.client.plugins.deferred;
  15. import com.google.gwt.query.client.Function;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.List;
  19. /**
  20. * Implementation of jQuery.Callbacks for gwtquery.
  21. */
  22. public class Callbacks {
  23. /**
  24. * Iterface used for callbacks which could cancel the execution
  25. * when returning false.
  26. */
  27. public interface Callback {
  28. /**
  29. * Return false to avoid executing the rest of functions.
  30. */
  31. boolean f(Object... objects);
  32. }
  33. private List<Object> stack = new ArrayList<>();
  34. private boolean done = false;
  35. private List<Object> memory = null;
  36. private boolean isOnce, isMemory, isUnique, stopOnFalse;
  37. public Callbacks() {
  38. this("");
  39. }
  40. /**
  41. * Create a new Callbacks object with options given as a space delimited string.
  42. *
  43. * Valid options are:
  44. *
  45. * once, memory, unique, stopOnFalse
  46. */
  47. public Callbacks(String options) {
  48. isOnce = options.contains("once");
  49. isMemory = options.contains("memory");
  50. isUnique = options.contains("unique");
  51. stopOnFalse = options.contains("stopOnFalse");
  52. }
  53. /**
  54. * Add a Callback or a collection of callbacks to a callback list.
  55. *
  56. */
  57. public Callbacks add(Callback... c) {
  58. addAll((Object[]) c);
  59. return this;
  60. }
  61. /**
  62. * Add a Callback or a collection of callbacks to a callback list.
  63. */
  64. public Callbacks add(com.google.gwt.core.client.Callback<?, ?>... c) {
  65. addAll((Object[]) c);
  66. return this;
  67. }
  68. /**
  69. * Add a Function or a collection of Function to a callback list.
  70. */
  71. public Callbacks add(Function... f) {
  72. addAll((Object[]) f);
  73. return this;
  74. }
  75. /**
  76. * Disable a callback list from doing anything more.
  77. */
  78. public Callbacks disable() {
  79. stack = null;
  80. done = true;
  81. return this;
  82. }
  83. /**
  84. * lock.
  85. */
  86. public Callbacks lock() {
  87. if (!isMemory) {
  88. disable();
  89. }
  90. stack = null;
  91. return this;
  92. }
  93. /**
  94. * Call all of the callbacks with the given arguments.
  95. */
  96. public Callbacks fire(Object... o) {
  97. if (!done) {
  98. done = isOnce;
  99. if (isMemory) {
  100. memory = new ArrayList<>(Arrays.asList(o));
  101. }
  102. if (stack != null)
  103. for (Object c : stack) {
  104. if (!run(c, o) && stopOnFalse) {
  105. break;
  106. }
  107. }
  108. }
  109. return this;
  110. }
  111. /**
  112. * Remove a callback or a collection of callbacks from a callback list.
  113. */
  114. public Callbacks remove(Object... o) {
  115. stack.removeAll(Arrays.asList(o));
  116. return this;
  117. }
  118. private void addAll(Object... o) {
  119. for (Object c : o) {
  120. if (!done && stack != null && c != null && (!isUnique || !stack.contains(c))) {
  121. stack.add(c);
  122. }
  123. // In jQuery add always is run when memory is true even when unique is set
  124. if (isMemory && memory != null) {
  125. run(c, memory.toArray());
  126. }
  127. }
  128. }
  129. @SuppressWarnings({"unchecked", "rawtypes"})
  130. private boolean run(Object c, Object... o) {
  131. // Unbox array inside array when there is only an element.
  132. // It happens when running filters in Promise.then()
  133. if (o != null && o.length == 1 && o[0] != null && o[0].getClass().isArray()) {
  134. o = (Object[]) o[0];
  135. }
  136. if (c instanceof Callback) {
  137. return ((Callback) c).f(o);
  138. } else if (c instanceof Function) {
  139. Object r = ((Function) c).f(o);
  140. return (r instanceof Boolean) ? (Boolean) r : true;
  141. } else if (c instanceof com.google.gwt.core.client.Callback) {
  142. ((com.google.gwt.core.client.Callback) c).onSuccess(o);
  143. }
  144. return true;
  145. }
  146. public String status() {
  147. return "stack=" + (stack == null ? "null" : stack.size()) + " " + done;
  148. }
  149. }