/** * Creates a reactive queue with the supplied underlying queue implementation. */ public static <E> RQueue<E> create (Queue<E> impl) { return new RQueue<E>(impl); }
@Override public boolean addAll (Collection<? extends E> elements) { checkMutate(); // call add instead of calling _impl.addAll so if a listener throws an exception on // emission, we don't have elements added without a corresponding emission for (E elem : elements) add(elem); return true; }
/** * Connects the supplied listener to this queue, such that it will be notified on offers and * polls. * @return a connection instance which can be used to cancel the connection. */ public Connection connect (Listener<? super E> listener) { return addConnection(listener); }
@Override public E remove () { checkMutate(); E elem = _impl.remove(); // throws on empty emitPoll(elem); return elem; }
@Override public boolean offer (E element) { checkMutate(); if (!_impl.offer(element)) return false; emitOffer(element); return true; }
protected void emitOffer (E elem) { notify(OFFER, elem, null, null); }
/** * Creates a reactive queue backed by an {@link LinkedList}. */ public static <E> RQueue<E> create () { return create(new LinkedList<E>()); }
/** * Invokes {@code onOffer} for all existing queue elements, then connects {@code listener}. */ public Connection connectNotify (Listener<? super E> listener) { for (E elem : _impl) listener.onOffer(elem); return connect(listener); }
@Override public E poll () { checkMutate(); E elem = _impl.poll(); if (elem != null) emitPoll(elem); return elem; }
@Override public boolean add (E element) { checkMutate(); _impl.add(element); // throws on failure emitOffer(element); return true; }
protected void emitPoll (E elem) { notify(POLL, elem, null, null); }
/** * Creates a reactive queue backed by an {@link LinkedList}. */ public static <E> RQueue<E> create () { return create(new LinkedList<E>()); }
/** * Invokes {@code onOffer} for all existing queue elements, then connects {@code listener}. */ public Connection connectNotify (Listener<? super E> listener) { for (E elem : _impl) listener.onOffer(elem); return connect(listener); }
@Override public E poll () { checkMutate(); E elem = _impl.poll(); if (elem != null) emitPoll(elem); return elem; }
@Override public boolean offer (E element) { checkMutate(); if (!_impl.offer(element)) return false; emitOffer(element); return true; }
@Override public boolean addAll (Collection<? extends E> elements) { checkMutate(); // call add instead of calling _impl.addAll so if a listener throws an exception on // emission, we don't have elements added without a corresponding emission for (E elem : elements) add(elem); return true; }
protected void emitOffer (E elem) { notify(OFFER, elem, null, null); }
/** * Connects the supplied listener to this queue, such that it will be notified on offers and * polls. * @return a connection instance which can be used to cancel the connection. */ public Connection connect (Listener<? super E> listener) { return addConnection(listener); }