package waymaker.gen; // Copyright © 2015 Michael Allan. Licence MIT. import java.util.Set; import waymaker.gen.CopyOnResizeArraySet; import static waymaker.gen.Auditor.EMPTY_AUDITOR_ARRAY; /** A bell that re-emits the same ding. * * @param The type of ding emitted. */ public final class ReRinger> implements Bell { /** Constructs a ReRinger. * * @see #ding() */ public @ThreadSafe ReRinger( D _ding ) { ding = _ding; } // -------------------------------------------------------------------------------------------------- /** The ding that is emitted on each ring of the bell. */ public @ThreadSafe D ding() { return ding; } private final D ding; /** Emits a ding to all registered auditors. */ public void ring() { for( Auditor auditor: register ) auditor.hear( ding ); } // - B e l l ---------------------------------------------------------------------------------------- public void register( final Auditor auditor ) { register.add( auditor ); } public void registerDestructibly( final Auditor auditor, final Destructor destructor ) { register.add( auditor ); destructor.add( new Destructible() { public void close() { unregister( auditor ); } }); } public void unregister( final Auditor auditor ) { register.remove( auditor ); } //// P r i v a t e ///////////////////////////////////////////////////////////////////////////////////// private final Set> register = new CopyOnResizeArraySet>() { public @SuppressWarnings("unchecked") Auditor[] emptyArray() { return EMPTY_AUDITOR_ARRAY; } public @SuppressWarnings({"rawtypes","unchecked"}) Auditor[] newArray( final int length ) { return new Auditor[length]; } }; }