summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/util/Event3.java
blob: 9510e48633f702b39c7f33ab8a34d367e9a50ccc (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
43
44
45
46
47
48
49
50
package jrummikub.util;

import java.util.HashSet;

/**
 * Simple single parameter event generator
 * 
 * @param <T1>
 *            type of the first event parameter
 * @param <T2>
 *            type of the second event parameter
 * @param <T3>
 *            type of third event parameter
 */
public class Event3<T1, T2, T3> implements IEvent3<T1, T2, T3> {
	private HashSet<IListener3<T1, T2, T3>> listeners = new HashSet<IListener3<T1, T2, T3>>();

	@Override
	public Connection add(final IListener3<T1, T2, T3> listener) {
		listeners.add(listener);
		return new Connection() {

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

	@Override
	public void remove(IListener3<T1, T2, T3> listener) {
		listeners.remove(listener);
	}

	/**
	 * Generate a single event
	 * 
	 * @param value1
	 *            the first event parameter
	 * @param value2
	 *            the second event parameter
	 * @param value3
	 *            the third event parameter
	 */
	public void emit(T1 value1, T2 value2, T3 value3) {
		for (IListener3<T1, T2, T3> listener : new HashSet<IListener3<T1, T2, T3>>(listeners)) {
			listener.handle(value1, value2, value3);
		}
	}
}