<commit_before>package org.adligo.i.adi.client;


import org.adligo.i.log.client.Log;
import org.adligo.i.log.client.LogFactory;
import org.adligo.i.util.client.ArrayCollection;
import org.adligo.i.util.client.ClassUtils;
import org.adligo.i.util.client.Event;
import org.adligo.i.util.client.I_Iterator;
import org.adligo.i.util.client.I_Listener;
import org.adligo.i.util.client.I_Map;
import org.adligo.i.util.client.MapFactory;

/**
 * this is one way to pass all user events 
 * (user button clicks exc)
 * through a common location
 * 
 * delegation also provides a nice way to 
 * organize code
 * 
 * This is not in i_util simply so that
 * you have a logging to track 
 * what events are going through here
 * 
 * this can be used on J2EE, J2SE, J2ME and GWT
 * 
 * @author scott
 *
 */
public class EventDelegator implements I_Listener {
	private static final Log log = LogFactory.getLog(EventDelegator.class);
	
	private I_Map eventMap = MapFactory.create();
	private String name = "";
	
	public EventDelegator() {}
	
	public EventDelegator(String p_name) {
		name = p_name;
	}
	/**
	 * @param source
	 * @param i
	 */
	public synchronized void addEventHandler(Object source, I_Listener i) {
		Object val = eventMap.get(source);
		if (val == null) {
			eventMap.put(source, i);
		} else if (ClassUtils.typeOf(val, ArrayCollection.class)) {
			ArrayCollection list = (ArrayCollection) val;
			list.add(i);
		} else {
			ArrayCollection list = new ArrayCollection();
			list.add(val);
			list.add(i);
			eventMap.put(source, list);
		}
	}
	
	public synchronized void removeEventHandler(Object source, I_Listener i) {
		Object val = eventMap.get(source);
		if (val == null) {
			// do nothing its not in there anyway
		} else if (ClassUtils.typeOf(val, ArrayCollection.class)) {
			ArrayCollection list = (ArrayCollection) val;
			list.remove(i);
		} else {
			eventMap.remove(source);
		}
		
	}

	public void onEvent(Event p) {
		Object destination = eventMap.get(p.getSource());
		if (log.isDebugEnabled()) {
			log.debug(name + " enter onEvent " + p + "\n destination is " +
					destination);
		}
		
		if (destination == null) {
			throw new NullPointerException(
					name + " No listener found for Event " + p);
		} else if (ClassUtils.typeOf(destination, ArrayCollection.class)) {
			
			ArrayCollection list = (ArrayCollection) destination;
			if (log.isDebugEnabled()) {
				log.debug( name + " onEvent with " + 
						list.size() + " listeners ");
			}
			I_Iterator it = list.getIterator();
			
			boolean first = true;
			int counter = 0;
			while (it.hasNext()) {
				I_Listener listener = (I_Listener) it.next();
				if (first) {
					send(p, listener, counter);
					first = false;
				} else {
					send(p.copy(), listener, counter);
				}
				counter++;
			}
		} else {
			send(p, (I_Listener) destination, 0);
		}
	}

	private void send(Event p, I_Listener listener, int counter) {
		if (log.isDebugEnabled()) {
			log.debug(name + " sending event " + p + "\n to listener " + counter + "," + listener);
		}
		listener.onEvent(p);
	}
}<commit_msg>wrapped exception in onEvent, send to log don't kill the app's whole stack<commit_after>package org.adligo.i.adi.client;


import org.adligo.i.log.client.Log;
import org.adligo.i.log.client.LogFactory;
import org.adligo.i.util.client.ArrayCollection;
import org.adligo.i.util.client.ClassUtils;
import org.adligo.i.util.client.Event;
import org.adligo.i.util.client.I_Iterator;
import org.adligo.i.util.client.I_Listener;
import org.adligo.i.util.client.I_Map;
import org.adligo.i.util.client.MapFactory;

/**
 * this is one way to pass all user events 
 * (user button clicks exc)
 * through a common location
 * 
 * delegation also provides a nice way to 
 * organize code
 * 
 * This is not in i_util simply so that
 * you have a logging to track 
 * what events are going through here
 * 
 * this can be used on J2EE, J2SE, J2ME and GWT
 * 
 * @author scott
 *
 */
public class EventDelegator implements I_Listener {
	private static final Log log = LogFactory.getLog(EventDelegator.class);
	
	private I_Map eventMap = MapFactory.create();
	private String name = "";
	
	public EventDelegator() {}
	
	public EventDelegator(String p_name) {
		name = p_name;
	}
	/**
	 * @param source
	 * @param i
	 */
	public synchronized void addEventHandler(Object source, I_Listener i) {
		Object val = eventMap.get(source);
		if (val == null) {
			eventMap.put(source, i);
		} else if (ClassUtils.typeOf(val, ArrayCollection.class)) {
			ArrayCollection list = (ArrayCollection) val;
			list.add(i);
		} else {
			ArrayCollection list = new ArrayCollection();
			list.add(val);
			list.add(i);
			eventMap.put(source, list);
		}
	}
	
	public synchronized void removeEventHandler(Object source, I_Listener i) {
		Object val = eventMap.get(source);
		if (val == null) {
			// do nothing its not in there anyway
		} else if (ClassUtils.typeOf(val, ArrayCollection.class)) {
			ArrayCollection list = (ArrayCollection) val;
			list.remove(i);
		} else {
			eventMap.remove(source);
		}
		
	}

	public void onEvent(Event p) {
		try {
			Object destination = eventMap.get(p.getSource());
			if (log.isDebugEnabled()) {
				log.debug(name + " enter onEvent " + p + "\n destination is " +
						destination);
			}
			
			if (destination == null) {
				throw new NullPointerException(
						name + " No listener found for Event " + p);
			} else if (ClassUtils.typeOf(destination, ArrayCollection.class)) {
				
				ArrayCollection list = (ArrayCollection) destination;
				if (log.isDebugEnabled()) {
					log.debug( name + " onEvent with " + 
							list.size() + " listeners ");
				}
				I_Iterator it = list.getIterator();
				
				boolean first = true;
				int counter = 0;
				while (it.hasNext()) {
					I_Listener listener = (I_Listener) it.next();
					if (first) {
						send(p, listener, counter);
						first = false;
					} else {
						send(p.copy(), listener, counter);
					}
					counter++;
				}
			} else {
				send(p, (I_Listener) destination, 0);
			}
		} catch (Exception x) {
			log.error(x.getMessage(), x);
		}
	}

	private void send(Event p, I_Listener listener, int counter) {
		if (log.isDebugEnabled()) {
			log.debug(name + " sending event " + p + "\n to listener " + 
					counter + "," + listener);
		}
		listener.onEvent(p);
	}
}
{'fraction_non_alphanumeric': 0.12652414885193983, 'fraction_numerical': 0.001583531274742676, 'mean_word_length': 3.241773002014775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5555887', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2061, 'n_words': 556}