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

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

import bjc.utils.data.IHolder;
import bjc.utils.funcdata.FunctionalList;

/**
 * Holds a single value of a specific type. This is used for indirect
 * references to data, and more specifically for accessing non-final
 * variables from a lambda. AKA the identity monad
 * 
 * This is a lazy variant of {@link IHolder}
 * 
 * @author ben
 *
 * @param <T>
 *            The type of the data being held
 */
public class LazyHolder<T> implements IHolder<T> {
	private final class LazyHolderSupplier<NewT>
			implements Supplier<NewT> {
		private FunctionalList<Function<T, T>>	pendingActions;
		private Function<T, NewT>				pendingTransform;

		public LazyHolderSupplier(FunctionalList<Function<T, T>> actons,
				Function<T, NewT> transform) {
			// Resolve latent bug I just realized. After a map, adding new
			// actions to the original holder could've resulted in changes
			// to all unactualized mapped values from that holder
			pendingActions = actons.clone();

			this.pendingTransform = transform;
		}

		@Override
		public NewT get() {
			if (heldValue == null) {
				return pendingActions.reduceAux(heldSource.get(),
						Function<T, T>::apply, pendingTransform::apply);
			} else {
				return pendingActions.reduceAux(heldValue,
						Function<T, T>::apply, pendingTransform::apply);
			}
		}
	}

	/**
	 * List of queued actions to be performed on realized values
	 */
	private FunctionalList<Function<T, T>>	actions	=
			new FunctionalList<>();

	/**
	 * The value internally held by this lazy holder
	 */
	private T								heldValue;

	/**
	 * The source for a value held by this lazy holder
	 */
	private Supplier<T>						heldSource;

	/**
	 * Create a new lazy holder with the given supplier
	 * 
	 * @param source
	 *            The supplier for a value when it is neededs
	 */
	public LazyHolder(Supplier<T> source) {
		heldSource = source;

		heldValue = null;
	}

	/**
	 * Create a new lazy holder with the given value
	 * 
	 * @param value
	 *            The value held in the holder
	 */
	public LazyHolder(T value) {
		heldValue = value;
	}

	@Override
	public void doWith(Consumer<T> action) {
		transform((value) -> {
			// Do the action with the value
			action.accept(value);

			// Return the untransformed value
			return value;
		});
	}

	@Override
	public <NewT> IHolder<NewT> map(Function<T, NewT> transform) {
		// Don't actually map until we need to
		return new LazyHolder<>(
				new LazyHolderSupplier<>(actions, transform));
	}

	@Override
	public IHolder<T> transform(Function<T, T> transform) {
		// Queue the transform until we need to apply it
		actions.add(transform);

		return this;
	}

	@Override
	public <E> E unwrap(Function<T, E> unwrapper) {
		// Actualize ourselves
		if (heldValue == null) {
			heldValue = heldSource.get();
		}

		// Apply all pending transforms
		actions.forEach((action) -> heldValue = action.apply(heldValue));

		return unwrapper.apply(heldValue);
	}

}