summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java
blob: 016f49226eba0e210abb8f694608787e33268f12 (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
package bjc.utils.data;

import java.util.function.Supplier;

public class SingleSupplier<T> implements Supplier<T> {
	private Supplier<T>	source;

	private boolean		gotten;

	private long		id;

	private static long	nextID	= 0;

	public SingleSupplier(Supplier<T> supp) {
		source = supp;

		gotten = false;

		id = nextID++;
	}

	@Override
	public T get() {
		if (gotten == true) {
			throw new IllegalStateException(
					"Attempted to get value more than once"
							+ " from single supplier #" + id);
		}

		gotten = true;

		return source.get();
	}
}