src/core/net/sf/basedb/util/bfs/MultiEventHandler.java

Code
Comments
Other
Rev Date Author Line
5224 27 Jan 10 nicklas 1 /**
5224 27 Jan 10 nicklas 2   $Id$
5224 27 Jan 10 nicklas 3
5224 27 Jan 10 nicklas 4   Copyright (C) 2009 Nicklas Nordborg
5224 27 Jan 10 nicklas 5
5224 27 Jan 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5224 27 Jan 10 nicklas 7   Available at http://base.thep.lu.se/
5224 27 Jan 10 nicklas 8
5224 27 Jan 10 nicklas 9   BASE is free software; you can redistribute it and/or
5224 27 Jan 10 nicklas 10   modify it under the terms of the GNU General Public License
5224 27 Jan 10 nicklas 11   as published by the Free Software Foundation; either version 3
5224 27 Jan 10 nicklas 12   of the License, or (at your option) any later version.
5224 27 Jan 10 nicklas 13
5224 27 Jan 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5224 27 Jan 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5224 27 Jan 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5224 27 Jan 10 nicklas 17   GNU General Public License for more details.
5224 27 Jan 10 nicklas 18
5224 27 Jan 10 nicklas 19   You should have received a copy of the GNU General Public License
5224 27 Jan 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5224 27 Jan 10 nicklas 21 */
5224 27 Jan 10 nicklas 22 package net.sf.basedb.util.bfs;
5224 27 Jan 10 nicklas 23
5224 27 Jan 10 nicklas 24 import java.util.ArrayList;
5224 27 Jan 10 nicklas 25 import java.util.List;
5224 27 Jan 10 nicklas 26
5224 27 Jan 10 nicklas 27 /**
5224 27 Jan 10 nicklas 28   Event handler implementation that forwards a single event to multiple
5224 27 Jan 10 nicklas 29   event handlers. This is useful since a parser only sends the event
5224 27 Jan 10 nicklas 30   to a single handler. The event is forwarded to all registered event 
5224 27 Jan 10 nicklas 31   handlers in the order they have been registered.
5224 27 Jan 10 nicklas 32   
5224 27 Jan 10 nicklas 33   @author Nicklas
5224 27 Jan 10 nicklas 34   @version 2.15
5224 27 Jan 10 nicklas 35   @base.modified $Date$
5224 27 Jan 10 nicklas 36 */
5224 27 Jan 10 nicklas 37 public class MultiEventHandler
5224 27 Jan 10 nicklas 38   implements EventHandler
5224 27 Jan 10 nicklas 39 {
5224 27 Jan 10 nicklas 40
5224 27 Jan 10 nicklas 41   
5224 27 Jan 10 nicklas 42   private List<EventHandler> handlers;
5224 27 Jan 10 nicklas 43   /**
5224 27 Jan 10 nicklas 44     Create a new event handler. Use {@link #addEventHandler(EventHandler)}
5224 27 Jan 10 nicklas 45     to add the event handlers that the events should be forwarded to.
5224 27 Jan 10 nicklas 46    */
5224 27 Jan 10 nicklas 47   public MultiEventHandler()
5224 27 Jan 10 nicklas 48   {
5224 27 Jan 10 nicklas 49     this.handlers = new ArrayList<EventHandler>();
5224 27 Jan 10 nicklas 50   }
5224 27 Jan 10 nicklas 51   
5224 27 Jan 10 nicklas 52   /**
5224 27 Jan 10 nicklas 53     Adds an event handler.
5224 27 Jan 10 nicklas 54   */
5224 27 Jan 10 nicklas 55   public void addEventHandler(EventHandler e)
5224 27 Jan 10 nicklas 56   {
5224 27 Jan 10 nicklas 57     if (e == null) throw new NullPointerException("e");
5224 27 Jan 10 nicklas 58     handlers.add(e);
5224 27 Jan 10 nicklas 59   }
5224 27 Jan 10 nicklas 60   
5224 27 Jan 10 nicklas 61   /**
5224 27 Jan 10 nicklas 62     Removes an event handler.
5224 27 Jan 10 nicklas 63   */
5224 27 Jan 10 nicklas 64   public void removeEventHandler(EventHandler e)
5224 27 Jan 10 nicklas 65   {
5224 27 Jan 10 nicklas 66     handlers.remove(e);
5224 27 Jan 10 nicklas 67   }
5224 27 Jan 10 nicklas 68   
5224 27 Jan 10 nicklas 69   /**
5224 27 Jan 10 nicklas 70     Resends the incoming event to all registered event handlers.
5224 27 Jan 10 nicklas 71   */
5224 27 Jan 10 nicklas 72   @Override
5224 27 Jan 10 nicklas 73   public <T> void handleEvent(EventType<T> eventType, T eventData, BfsParser parser)
5224 27 Jan 10 nicklas 74   {
5224 27 Jan 10 nicklas 75     for (EventHandler e : handlers)
5224 27 Jan 10 nicklas 76     {
5224 27 Jan 10 nicklas 77       e.handleEvent(eventType, eventData, parser);
5224 27 Jan 10 nicklas 78     }
5224 27 Jan 10 nicklas 79   }
5224 27 Jan 10 nicklas 80
5224 27 Jan 10 nicklas 81 }