summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/data/OneWayToggle.java
blob: b6074b02568160fdc8ce2a5ae9cec43e29ee9bc4 (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
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;
		else          return first;
	}

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