package jrummikub.util;

import java.util.HashSet;

/**
 * Simple single parameter event generator
 * 
 * @param <T>
 *          type of the event parameter
 */
public class Event1<T> implements IEvent1<T> {
	private HashSet<IListener1<T>> listeners = new HashSet<IListener1<T>>();

	@Override
	public Connection add(final IListener1<T> listener) {
		listeners.add(listener);
		return new Connection() {

			@Override
			public void remove() {
				Event1.this.remove(listener);
			}
		};
	}

	@Override
	public void remove(IListener1<T> listener) {
		listeners.remove(listener);
	}

	/**
	 * Generate a single event
	 * 
	 * @param value
	 *          the event parameter
	 */
	public void emit(T value) {
		for (IListener1<T> listener : new HashSet<IListener1<T>>(listeners)) {
			listener.handle(value);
		}
	}
}
