|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Multiplexor.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | /* $Id: Multiplexor.java,v 1.2 2004/05/21 23:29:45 johndavidtaylor Exp $ | |
| 2 | * Created on Feb 20, 2004 by John Taylor jdt@roe.ac.uk . | |
| 3 | */ | |
| 4 | ||
| 5 | package org.abraracourcix.alipes.pipes; | |
| 6 | ||
| 7 | import java.util.ArrayList; | |
| 8 | import java.util.Collection; | |
| 9 | import java.util.Iterator; | |
| 10 | ||
| 11 | import org.abraracourcix.alipes.common.Event; | |
| 12 | import org.abraracourcix.alipes.listeners.Listener; | |
| 13 | import org.abraracourcix.alipes.listeners.ListenerException; | |
| 14 | import org.abraracourcix.alipes.monitors.Monitor; | |
| 15 | ||
| 16 | /** | |
| 17 | * Multiplexes an event to several listeners | |
| 18 | * @author jdt | |
| 19 | */ | |
| 20 | public class Multiplexor implements Monitor, Listener { | |
| 21 | /** | |
| 22 | * Collection of Listeners | |
| 23 | */ | |
| 24 | private final Collection listeners = new ArrayList(); | |
| 25 | /** | |
| 26 | * Add a new one | |
| 27 | * @param listener Listener to add | |
| 28 | */ | |
| 29 | 8 | public final void addListener(final Listener listener) { |
| 30 | 8 | listeners.add(listener); |
| 31 | } | |
| 32 | ||
| 33 | /** | |
| 34 | * something happened - pass the word on | |
| 35 | * @param event the event | |
| 36 | * @param resource happened to this resouce | |
| 37 | * @throws MultiplexorException if one or more of the underlying listeners throws an exception, but processing continues across all the listeners even it does | |
| 38 | * @see org.abraracourcix.alipes.listeners.Listener#eventOccurred(org.abraracourcix.alipes.common.Event, java.lang.Object) | |
| 39 | */ | |
| 40 | 3 | public final void eventOccurred(final Event event, final Object resource) throws MultiplexorException { |
| 41 | 3 | final Iterator it = listeners.iterator(); |
| 42 | 3 | MultiplexorException exception=null; |
| 43 | 3 | while (it.hasNext()) { |
| 44 | 6 | final Listener listener = (Listener) it.next(); |
| 45 | 6 | try { |
| 46 | 6 | listener.eventOccurred(event, resource); |
| 47 | } catch (ListenerException e) { | |
| 48 | 2 | if (exception==null) { |
| 49 | 1 | exception=new MultiplexorException("One or more of my listeners has thrown an exception",e); |
| 50 | } else { | |
| 51 | 1 | exception.addCause(e); |
| 52 | } | |
| 53 | } | |
| 54 | } //end while | |
| 55 | 3 | if (exception!=null) { |
| 56 | 1 | throw exception; |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Clear the list of listeners | |
| 62 | */ | |
| 63 | 1 | public void clear() { |
| 64 | 1 | listeners.clear(); |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | ||
| 69 | /* | |
| 70 | *$Log: Multiplexor.java,v $ | |
| 71 | *Revision 1.2 2004/05/21 23:29:45 johndavidtaylor | |
| 72 | *Added a clear() method | |
| 73 | * | |
| 74 | *Revision 1.1 2004/03/17 21:18:40 johndavidtaylor | |
| 75 | *Copied across from incubation in beanpeeler | |
| 76 | * | |
| 77 | *Revision 1.5 2004/03/14 18:49:15 johndavidtaylor | |
| 78 | *corrected comments | |
| 79 | * | |
| 80 | *Revision 1.4 2004/03/14 00:44:22 johndavidtaylor | |
| 81 | *Added ListenerException | |
| 82 | * | |
| 83 | *Revision 1.3 2004/03/11 15:46:35 johndavidtaylor | |
| 84 | *Applied coding standards | |
| 85 | * | |
| 86 | *Revision 1.2 2004/03/08 20:16:45 johndavidtaylor | |
| 87 | *refactored to new package name | |
| 88 | * | |
| 89 | *Revision 1.1 2004/03/08 19:58:25 johndavidtaylor | |
| 90 | *Initial commit -transfer from previous repository | |
| 91 | * | |
| 92 | *Revision 1.1 2004/03/02 23:04:37 jdt | |
| 93 | *new | |
| 94 | * | |
| 95 | */ |
|
||||||||||