package jrummikub.util;

import java.util.HashSet;

/**
 * Mock class for Event1s
 * 
 * @param <T>
 *          event type
 */
public class MockEvent1<T> implements IEvent1<T> {
	/** */
	public 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() {
				MockEvent1.this.remove(listener);
			}
		};
	}

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

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