summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/data/OneWayToggle.java
blob: a4557cd3f54c48f0b07e5422d3a2b1d3cd650403 (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
51
52
53
package bjc.data;

/**
 * A toggle that will only give the first value once, only yielding the second
 * value afterwards.
 * 
 * @author student
 *
 * @param <E>
 *            The type of value stored
 */
public class OneWayToggle<E> implements Toggle<E> {
	private E first;
	private E second;

	private boolean gotFirst;

	/**
	 * Create a new one-way toggle
	 * 
	 * @param first
	 *            The value to offer first, and only once
	 * @param second
	 *            The value to offer second and repeatedly
	 */
	public OneWayToggle(E first, E second) {
		this.first = first;

		this.second = second;
	}

	@Override
	public E get() {
		if (gotFirst)
			return second;

		gotFirst = true;
		return first;
	}

	@Override
	public E peek() {
		if (gotFirst)
			return second;
		return first;
	}

	@Override
	public void set(boolean gotFirst) {
		this.gotFirst = gotFirst;
	}

}