summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/lazy/NewLazyPair.java
blob: 4bd38322565fa042b3d1b50c4997d557390cee55 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package bjc.utils.data.lazy;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Supplier;

import bjc.utils.data.GenHolder;
import bjc.utils.data.IPair;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IFunctionalList;

/**
 * New implementation of lazy pair not delegating to {@link LazyHolder}
 * 
 * @author ben
 * @param <L>
 *            The type on the left side of the pair
 * @param <R>
 *            The type on the right side of the pair
 *
 */
public class NewLazyPair<L, R> implements IPair<L, R>, ILazy {
	private static class BoundLazyPair<OldL, OldR, NewL, NewR>
			implements IPair<NewL, NewR> {
		private Supplier<OldL>								oldLeftSupplier;
		private Supplier<OldR>								oldRightSupplier;

		private BiFunction<OldL, OldR, IPair<NewL, NewR>>	newPairSupplier;

		private IPair<NewL, NewR>							newPair;

		private boolean										newPairMaterialized;

		public BoundLazyPair(Supplier<OldL> leftSup,
				Supplier<OldR> rightSup,
				BiFunction<OldL, OldR, IPair<NewL, NewR>> binder) {
			oldLeftSupplier = leftSup;
			oldRightSupplier = rightSup;

			newPairSupplier = binder;
		}

		@Override
		public <L2, R2> IPair<L2, R2> bind(
				BiFunction<NewL, NewR, IPair<L2, R2>> binder) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void doWith(BiConsumer<NewL, NewR> action) {
			// TODO Auto-generated method stub

		}

		@Override
		public <E> E merge(BiFunction<NewL, NewR, E> merger) {
			if (!newPairMaterialized) {
				newPair = newPairSupplier.apply(oldLeftSupplier.get(),
						oldRightSupplier.get());
			}

			return newPair.merge(merger);
		}
	}

	private L									leftValue;
	private R									rightValue;

	private Supplier<R>							rightValueSupplier;
	private Supplier<L>							leftValueSupplier;

	private boolean								rightMaterialized;
	private boolean								leftMaterialized;

	private IFunctionalList<BiConsumer<L, R>>	actions;

	/**
	 * Create a new lazy pair
	 * 
	 * @param leftVal
	 *            The left value in the pair
	 * @param rightVal
	 *            The right value in the pair
	 */
	public NewLazyPair(L leftVal, R rightVal) {
		leftValueSupplier = () -> leftVal;
		rightValueSupplier = () -> rightVal;
	}

	/**
	 * Create a new lazy pair
	 * 
	 * @param leftSupplier
	 *            The supplier for the left value in the pair
	 * @param rightSupplier
	 *            The supplier for the right value in the pair
	 */
	public NewLazyPair(Supplier<L> leftSupplier,
			Supplier<R> rightSupplier) {
		leftValueSupplier = leftSupplier;
		rightValueSupplier = rightSupplier;
	}

	@Override
	public void applyPendingActions() {
		if (!isMaterialized()) {
			throw new UnsupportedOperationException(
					"Can only apply actions to materialized values");
		}

		actions.forEach((action) -> {
			action.accept(leftValue, rightValue);
		});

		actions = new FunctionalList<>();
	}

	private void applyPossiblyPendingActions(GenHolder<Boolean> hasActions,
			IFunctionalList<BiConsumer<L, R>> pendingActions) {
		if (hasActions.unwrap((val) -> val) == true) {
			pendingActions.forEach((action) -> {
				action.accept(leftValue, rightValue);
			});

			hasActions.transform((val) -> false);
		}
	}

	@Override
	public <L2, R2> IPair<L2, R2> bind(
			BiFunction<L, R, IPair<L2, R2>> binder) {
		GenHolder<Boolean> hasActions = new GenHolder<>(true);
		IFunctionalList<BiConsumer<L, R>> pendingActions = new FunctionalList<>();

		actions.forEach(pendingActions::add);

		return new BoundLazyPair<>(() -> {
			applyPossiblyPendingActions(hasActions, pendingActions);

			return leftValue;
		}, () -> {
			applyPossiblyPendingActions(hasActions, pendingActions);

			return rightValue;
		}, binder);
	}

	@Override
	public void doWith(BiConsumer<L, R> action) {
		actions.add(action);
	}

	@Override
	public boolean hasPendingActions() {
		return !actions.isEmpty();
	}

	@Override
	public boolean isMaterialized() {
		if (leftValueSupplier == null) {
			if (rightValueSupplier == null) {
				return true;
			}

			return rightMaterialized;
		}

		if (rightValueSupplier == null) {
			return leftMaterialized;
		}

		return leftMaterialized && rightMaterialized;
	}

	@Override
	public void materialize() {
		if (isMaterialized()) {
			throw new UnsupportedOperationException(
					"Cannot materialize lazy object twice");
		}

		if (leftValueSupplier != null) {
			leftValue = leftValueSupplier.get();
		}

		if (rightValueSupplier != null) {
			rightValue = rightValueSupplier.get();
		}
	}

	@Override
	public <E> E merge(BiFunction<L, R, E> merger) {
		if (!isMaterialized()) {
			materialize();
		}

		applyPendingActions();

		return merger.apply(leftValue, rightValue);
	}
}