/** * Registers the specified handler instance with this MultiHandlerManager. * <p> * Handlers don't have to be unique instances, if the same handler is * registered multiple times, it will be invoked multiple times as well. * <p> * This method is threadsafe. * @param handler the handler to register * @return the Disposable instance to unregister the handler. */ @NonNull public final Disposable register(@NonNull H handler) { ObjectHelper.requireNonNull(handler, "handler is null"); HandlerRegistration<H> hr = new HandlerRegistration<H>(this, handler); handlers.add(hr); return hr; }
/** * The given consumer is invoked with each registered handler instance. * <p> * Exceptions raised by the invocation of the consumer for a particular * handler are printed to the console and the current thread's * uncaught exception handler is notified. * <p> * This method is threadsafe. * @param <S> the type of the extra state provided to the consumer * @param state the extra state provided to the consumer * @param consumer the consumer to invoke */ public final <S> void forEach(S state, @NonNull BiConsumer<S, H> consumer) { ObjectHelper.requireNonNull(consumer, "consumer is null"); Iterator<HandlerRegistration<H>> it = handlers.iterator(); while (it.hasNext()) { try { HandlerRegistration<H> hr = it.next(); H h = hr.get(); if (h != null) { consumer.accept(state, h); } } catch (Throwable ex) { ex.printStackTrace(); Thread t = Thread.currentThread(); t.getUncaughtExceptionHandler().uncaughtException(t, ex); } } }
/** * The given consumer is invoked with each registered handler instance. * <p> * Exceptions raised by the invocation of the consumer for a particular * handler are printed to the console and the current thread's * uncaught exception handler is notified. * <p> * This method is threadsafe. * @param <S> the type of the extra state provided to the consumer * @param state the extra state provided to the consumer * @param consumer the consumer to invoke */ public final <S> void forEach(S state, @NonNull BiConsumer<S, H> consumer) { ObjectHelper.requireNonNull(consumer, "consumer is null"); Iterator<HandlerRegistration<H>> it = handlers.iterator(); while (it.hasNext()) { try { HandlerRegistration<H> hr = it.next(); H h = hr.get(); if (h != null) { consumer.accept(state, h); } } catch (Throwable ex) { ex.printStackTrace(); Thread t = Thread.currentThread(); t.getUncaughtExceptionHandler().uncaughtException(t, ex); } } }
/** * The given consumer is invoked with each registered handler instance. * <p> * Exceptions raised by the invocation of the consumer for a particular * handler are printed to the console and the current thread's * uncaught exception handler is notified. * <p> * This method is threadsafe. * @param consumer the consumer to invoke */ public final void forEach(@NonNull Consumer<H> consumer) { ObjectHelper.requireNonNull(consumer, "consumer is null"); Iterator<HandlerRegistration<H>> it = handlers.iterator(); while (it.hasNext()) { try { HandlerRegistration<H> hr = it.next(); H h = hr.get(); if (h != null) { consumer.accept(h); } } catch (Throwable ex) { ex.printStackTrace(); Thread t = Thread.currentThread(); t.getUncaughtExceptionHandler().uncaughtException(t, ex); } } }
/** * The given consumer is invoked with each registered handler instance. * <p> * Exceptions raised by the invocation of the consumer for a particular * handler are printed to the console and the current thread's * uncaught exception handler is notified. * <p> * This method is threadsafe. * @param consumer the consumer to invoke */ public final void forEach(@NonNull Consumer<H> consumer) { ObjectHelper.requireNonNull(consumer, "consumer is null"); Iterator<HandlerRegistration<H>> it = handlers.iterator(); while (it.hasNext()) { try { HandlerRegistration<H> hr = it.next(); H h = hr.get(); if (h != null) { consumer.accept(h); } } catch (Throwable ex) { ex.printStackTrace(); Thread t = Thread.currentThread(); t.getUncaughtExceptionHandler().uncaughtException(t, ex); } } }
/** * Registers the specified handler instance with this MultiHandlerManager. * <p> * Handlers don't have to be unique instances, if the same handler is * registered multiple times, it will be invoked multiple times as well. * <p> * This method is threadsafe. * @param handler the handler to register * @return the Disposable instance to unregister the handler. */ @NonNull public final Disposable register(@NonNull H handler) { ObjectHelper.requireNonNull(handler, "handler is null"); HandlerRegistration<H> hr = new HandlerRegistration<H>(this, handler); handlers.add(hr); return hr; }
@Override public boolean isDisposed() { return get() == null; } }
@Override public void dispose() { H h = getAndSet(null); if (h != null) { parent.unregister(this); } }
HandlerRegistration(MultiHandlerManager<H> parent, H handler) { this.parent = parent; lazySet(handler); }
HandlerRegistration(MultiHandlerManager<H> parent, H handler) { this.parent = parent; lazySet(handler); }
@Override public boolean isDisposed() { return get() == null; } }
@Override public void dispose() { H h = getAndSet(null); if (h != null) { parent.unregister(this); } }