summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java
blob: 061486eb1b3c6d98206b3ecb07092eeb7f278446 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package bjc.utils.data;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IFunctionalList;

/**
 * A holder that holds a means to create a value, but doesn't actually
 * compute the value until it's needed
 * 
 * @author ben
 *
 * @param <ContainedType>
 */
public class Lazy<ContainedType> implements IHolder<ContainedType> {
	private Supplier<ContainedType>							valueSupplier;

	private IFunctionalList<UnaryOperator<ContainedType>>	actions	= new FunctionalList<>();

	private boolean											valueMaterialized;

	private ContainedType									heldValue;

	/**
	 * Create a new lazy value from the specified seed value
	 * 
	 * @param value
	 *            The seed value to use
	 */
	public Lazy(ContainedType value) {
		heldValue = value;

		valueMaterialized = true;
	}

	/**
	 * Create a new lazy value from the specified value source
	 * 
	 * @param supp
	 *            The source of a value to use
	 */
	public Lazy(Supplier<ContainedType> supp) {
		valueSupplier = supp;

		valueMaterialized = false;
	}

	private Lazy(Supplier<ContainedType> supp,
			IFunctionalList<UnaryOperator<ContainedType>> pendingActions) {
		valueSupplier = supp;

		actions = pendingActions;
	}

	@Override
	public <BoundType> IHolder<BoundType> bind(
			Function<ContainedType, IHolder<BoundType>> binder) {
		IFunctionalList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();

		actions.forEach(pendingActions::add);

		Supplier<ContainedType> supplier = () -> {
			if (valueMaterialized) {
				return heldValue;
			}

			return valueSupplier.get();
		};

		return new BoundLazy<>(() -> {
			return new Lazy<>(supplier, pendingActions);
		}, binder);
	}

	@Override
	public <MappedType> IHolder<MappedType> map(
			Function<ContainedType, MappedType> mapper) {
		IFunctionalList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();

		actions.forEach(pendingActions::add);

		return new Lazy<>(() -> {
			ContainedType currVal = heldValue;

			if (!valueMaterialized) {
				currVal = valueSupplier.get();
			}

			return pendingActions.reduceAux(currVal,
					UnaryOperator<ContainedType>::apply,
					(value) -> mapper.apply(value));
		});
	}

	@Override
	public String toString() {
		if (valueMaterialized) {
			if (actions.isEmpty()) {
				return "value[v='" + heldValue + "']";
			}

			return "value[v='" + heldValue + "'] (has pending transforms)";
		}

		return "(unmaterialized)";
	}

	@Override
	public IHolder<ContainedType> transform(
			UnaryOperator<ContainedType> transformer) {
		actions.add(transformer);

		return this;
	}

	@Override
	public <UnwrappedType> UnwrappedType unwrap(
			Function<ContainedType, UnwrappedType> unwrapper) {
		if (!valueMaterialized) {
			heldValue = valueSupplier.get();

			valueMaterialized = true;
		}

		actions.forEach((action) -> {
			heldValue = action.apply(heldValue);
		});

		actions = new FunctionalList<>();

		return unwrapper.apply(heldValue);
	}
}