package textbender.g.util; // Copyright 2006, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.util.*; import textbender.g.lang.ThreadSafe; /** Relayer of collection events to listeners. * Thread-safe if the provided collection of listeners, * and the listeners themselves are thread-safe. */ public @ThreadSafe class CollectionRelayer implements CollectionListener { /** Constructs a CollectionRelayer. * * @param listeners to whom events are relayed */ public CollectionRelayer( Collection> listeners ) { this.listeners = listeners; } // - C o l l e c t i o n - L i s t e n e r -------------------------------------------- /** Relays the event to the listeners. */ public final void elementAdded( CollectionEvent.ElementAdded e ) { for( CollectionListener listener : listenersCopy() ) listener.elementAdded( e ); } /** Relays the event to the listeners. */ public final void elementRemoved( CollectionEvent.ElementRemoved e ) { for( CollectionListener listener : listenersCopy() ) listener.elementRemoved( e ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Collection> listeners; /** Returns a copy of the listener collection. * A copy is used to guard against collection modification during relay, * e.g. by the listeners themselves, * which would cause relay iteration to fail. */ protected Collection> listenersCopy() { return new ArrayList>( listeners ); } }