/** * Creates a callback list, populated with the supplied callback. */ public static <T> CallbackList<T> create(Callback<T> callback) { CallbackList<T> list = new CallbackList<T>(); list.add(callback); return list; }
/** * Removes the specified callback from the list. * * @throws IllegalStateException if this callback has already fired. */ public void remove(Callback<T> callback) { checkState(); callbacks.remove(callback); }
/** * Adds the supplied callback to the list. * * @return this instance for convenient chaining. * @throws IllegalStateException if this callback has already fired. */ public CallbackList<T> add(Callback<T> callback) { checkState(); callbacks.add(callback); return this; }
/** * Dispatches success to all of the callbacks registered with this list. This may only be called * once. * * @throws IllegalStateException if this callback has already fired. */ @Override public void onSuccess(T result) { checkState(); for (Callback<T> cb : callbacks) { cb.onSuccess(result); } callbacks = null; // note that we've fired }
/** * Dispatches failure to all of the callbacks registered with this list. This may only be called * once. * * @throws IllegalStateException if this callback has already fired. */ @Override public void onFailure(Throwable cause) { checkState(); for (Callback<T> cb : callbacks) { cb.onFailure(cause); } callbacks = null; // note that we've fired }