summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/util/Event1.java
blob: f93d519d6651689cbb2b950f61273abb867684dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
		}
	}
}