/*
 * JGrapes Event Driven Framework
 * Copyright (C) 2016-2018 Michael N. Lipp
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */

package org.jgrapes.core.internal;

import java.util.ArrayList;

import org.jgrapes.core.EventPipeline;

/**
 * A list of handlers for a given event type and set of channels.
 */
@SuppressWarnings("serial")
class HandlerList extends ArrayList<HandlerReference> {

	/**
	 * Invoke all handlers with the given event as parameter.
	 * 
	 * @param eventProcessor
	 * @param event the event
	 */
	public void process(EventPipeline eventPipeline, EventBase<?> event) {
		for (HandlerReference hdlr: this) {
			try {
				hdlr.invoke(event);
				if (event.isStopped()) {
					break;
				}
			} catch (Throwable t) {
				if (t instanceof AssertionError) {
					Common.setAssertionError((AssertionError)t);
				}
				event.handlingError(eventPipeline, t);
			}
		}
		try {
			event.handled();
		} catch (AssertionError t) {
			Common.setAssertionError(t);
			event.handlingError(eventPipeline, t);
		} catch (Throwable t) {
			event.handlingError(eventPipeline, t);
		}
	}

}

@@ -1197,16 +1197,25 @@
 vent) %7B%0A
+%09%09try %7B%0A%09
 %09%09for (H
@@ -1234,16 +1234,17 @@
 nce hdlr
+ 
 : this) 
@@ -1248,22 +1248,24 @@
 s) %7B%0A%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09hdlr
@@ -1276,24 +1276,25 @@
 oke(event);%0A
+%09
 %09%09%09%09if (even
@@ -1315,16 +1315,17 @@
  %7B%0A%09%09%09%09%09
+%09
 break;%0A%09
@@ -1327,18 +1327,20 @@
 ak;%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%7D cat
@@ -1358,24 +1358,25 @@
 le t) %7B%0A%09%09%09%09
+%09
 if (t instan
@@ -1394,24 +1394,25 @@
 ionError) %7B%0A
+%09
 %09%09%09%09%09Common.
@@ -1445,24 +1445,25 @@
 onError)
+ 
 t);%0A%09%09%09%09
 %7D%0A%09%09%09%09ev
@@ -1450,26 +1450,28 @@
 or) t);%0A%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09event.ha
@@ -1508,22 +1508,39 @@
 %0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A
+%09%09%7D finally %7B%0A%09
 %09%09try %7B%0A
 %09%09%09e
@@ -1535,16 +1535,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09event
@@ -1556,16 +1556,17 @@
 dled();%0A
+%09
 %09%09%7D catc
@@ -1591,16 +1591,17 @@
 t) %7B%0A%09%09%09
+%09
 Common.s
@@ -1621,16 +1621,17 @@
 ror(t);%0A
+%09
 %09%09%09event
@@ -1664,16 +1664,17 @@
 ne, t);%0A
+%09
 %09%09%7D catc
@@ -1694,16 +1694,17 @@
 t) %7B%0A%09%09%09
+%09
 event.ha
@@ -1734,16 +1734,21 @@
 ne, t);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Make absolutely sure that handled() is called.