summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/data/Holder.java
blob: 4a1de75f31c225fd4badf21a23c2ab687ef817a7 (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
/* 
 * esodata - data structures and other things, of varying utility
 * Copyright 2022, Ben Culkin
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package bjc.data;

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

import bjc.data.internals.BoundListHolder;
import bjc.data.internals.WrappedLazy;
import bjc.data.internals.WrappedOption;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.theory.Functor;

/**
 * A holder of a single value.
 *
 * @author ben
 *
 * @param <ContainedType>
 *                        The type of value held.
 */
public interface Holder<ContainedType> extends Functor<ContainedType> {
	/**
	 * Bind a function across the value in this container.
	 *
	 * @param <BoundType>
	 *                    The type of value in this container.
	 *
	 * @param binder
	 *                    The function to bind to the value.
	 *
	 * @return A holder from binding the value.
	 */
	public <BoundType> Holder<BoundType>
			bind(Function<ContainedType, Holder<BoundType>> binder);

	/**
	 * Apply an action to the value.
	 *
	 * @param action
	 *               The action to apply to the value.
	 */
	public default void doWith(final Consumer<? super ContainedType> action) {
		transform(value -> {
			action.accept(value);

			return value;
		});
	}

	@Override
	default <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>>
			fmap(final Function<ArgType, ReturnType> func) {
		return argumentFunctor -> {
			if (!(argumentFunctor instanceof Holder<?>)) {
				final String msg
						= "This functor only supports mapping over instances of IHolder";

				throw new IllegalArgumentException(msg);
			}

			final Holder<ArgType> holder = (Holder<ArgType>) argumentFunctor;

			return holder.map(func);
		};
	}

	@Override
	public default ContainedType getValue() {
		return unwrap(value -> value);
	}

	/**
	 * Lifts a function to bind over this holder.
	 *
	 * @param <NewType>
	 *                  The type of the functions return.
	 *
	 * @param func
	 *                  The function to lift over the holder.
	 *
	 * @return The function lifted over the holder.
	 */
	public <NewType> Function<ContainedType, Holder<NewType>>
			lift(Function<ContainedType, NewType> func);

	/**
	 * Make this holder lazy.
	 *
	 * @return A lazy version of this holder.
	 */
	public default Holder<ContainedType> makeLazy() {
		return new WrappedLazy<>(this);
	}

	/**
	 * Make this holder a list.
	 *
	 * @return A list version of this holder.
	 */
	public default Holder<ContainedType> makeList() {
		return new BoundListHolder<>(new FunctionalList<>(this));
	}

	/**
	 * Make this holder optional.
	 *
	 * @return An optional version of this holder.
	 */
	public default Holder<ContainedType> makeOptional() {
		return new WrappedOption<>(this);
	}

	/**
	 * Create a new holder with a mapped version of the value in this holder.
	 *
	 * Does not change the internal state of this holder.
	 *
	 * @param <MappedType>
	 *                     The type of the mapped value.
	 *
	 * @param mapper
	 *                     The function to do mapping with.
	 *
	 * @return A holder with the mapped value
	 */
	public <MappedType> Holder<MappedType>
			map(Function<ContainedType, MappedType> mapper);

	/**
	 * Replace the held value with a new one.
	 *
	 * @param newValue
	 *                 The value to hold instead.
	 *
	 * @return The holder itself.
	 */
	public default Holder<ContainedType> replace(final ContainedType newValue) {
		return transform(oldValue -> newValue);
	}

	/**
	 * Transform the value held in this holder.
	 *
	 * @param transformer
	 *                    The function to transform the value with.
	 *
	 * @return The holder itself, for easy chaining.
	 */
	public Holder<ContainedType> transform(UnaryOperator<ContainedType> transformer);

	/**
	 * Unwrap the value contained in this holder so that it is no longer held.
	 *
	 * @param <UnwrappedType>
	 *                        The type of the unwrapped value.
	 *
	 * @param unwrapper
	 *                        The function to use to unwrap the value.
	 *
	 * @return The unwrapped held value.
	 */
	public <UnwrappedType> UnwrappedType
			unwrap(Function<ContainedType, UnwrappedType> unwrapper);
	
	/**
	 * Create an instace of IHolder containing a single value.
	 * 
	 * @param <ElementType> The type of the value contained.
	 * 
	 * @param contained The value to contain.
	 * 
	 * @return An instance of IHolder containing that value.
	 */
	static <ElementType> Holder<ElementType> of(ElementType contained) {
		return new Identity<>(contained);
	}
}