Skip to content

Commit 0db0e07

Browse files
committed
HList subtype specializations 2-element and up (Tuple2 - Tuple5) now support constant time element lookup using custom accessors.
Adding copious amounts of missing JavaDoc
1 parent 87ed771 commit 0db0e07

22 files changed

Lines changed: 805 additions & 66 deletions

File tree

‎src/main/java/com/jnape/palatable/lambda/adt/Either.java‎

Lines changed: 147 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,110 @@ public abstract class Either<L, R> implements Functor<R>, Bifunctor<L, R> {
2626
private Either() {
2727
}
2828

29+
/**
30+
* Return the value wrapped by this <code>Either</code> if it's a right value; otherwise, return defaultValue.
31+
*
32+
* @param defaultValue the value to return if this is a left
33+
* @return the value wrapped by this Either if right; otherwise, defaultValue
34+
*/
2935
public final R or(R defaultValue) {
3036
return recover(l -> defaultValue);
3137
}
3238

39+
/**
40+
* "Recover" from a left value by applying a recoveryFn to the wrapped value and returning it in the case of a left
41+
* value; otherwise, return the wrapped right value.
42+
*
43+
* @param recoveryFn a function from L to R
44+
* @return either the wrapped value (if right) or the result of the left value applied to recoveryFn
45+
*/
3346
public final R recover(Fn1<? super L, ? extends R> recoveryFn) {
3447
return match(recoveryFn, id());
3548
}
3649

50+
/**
51+
* Inverse of recover. If this is a right value, apply the wrapped value to forfeitFn and return it; otherwise,
52+
* return the wrapped left value;
53+
*
54+
* @param forfeitFn a function from R to L
55+
* @return either the wrapped value (if left) or the result of the right value applied to forfeitFn
56+
*/
3757
public final L forfeit(Fn1<? super R, ? extends L> forfeitFn) {
3858
return match(id(), forfeitFn);
3959
}
4060

41-
public final <E extends RuntimeException> R orThrow(
42-
Fn1<? super L, ? extends E> throwableFn) throws E {
61+
/**
62+
* Return the wrapped value if this is a right; otherwise, map the wrapped left value to an <code>E</code> and throw
63+
* it.
64+
*
65+
* @param throwableFn a function from L to E
66+
* @param <E> the left parameter type (the throwable exception type)
67+
* @return the wrapped value if this is a right
68+
* @throws E the result of applying the wrapped left value to throwableFn, if this is a left
69+
*/
70+
public final <E extends RuntimeException> R orThrow(Fn1<? super L, ? extends E> throwableFn) throws E {
4371
return match(l -> {
4472
throw throwableFn.apply(l);
4573
}, id());
4674
}
4775

48-
public final Either<L, R> filter(Fn1<? super R, Boolean> pred,
49-
Supplier<L> leftSupplier) {
76+
/**
77+
* If this is a right value, apply pred to it. If the result is true, return the same value; otherwise, return the
78+
* result of leftSupplier wrapped as a left value.
79+
* <p>
80+
* If this is a left value, return it.
81+
*
82+
* @param pred the predicate to apply to a right value
83+
* @param leftSupplier the supplier of a left value if pred fails
84+
* @return this if a left value or a right value that pred matches; otherwise, the result of leftSupplier wrapped in
85+
* a left
86+
*/
87+
public final Either<L, R> filter(Fn1<? super R, Boolean> pred, Supplier<L> leftSupplier) {
5088
return flatMap(r -> pred.apply(r) ? right(r) : left(leftSupplier.get()));
5189
}
5290

91+
/**
92+
* If a right value, unwrap it and apply it to <code>rightFn</code>, returning the resulting
93+
* <code>Either&lt;L ,R&gt;</code>. Otherwise, return the left value.
94+
* <p>
95+
* Note that because this monadic form of <code>flatMap</code> only supports mapping over a theoretical right value,
96+
* the resulting <code>Either</code> must be invariant on the same left value to flatten properly.
97+
*
98+
* @param rightFn the function to apply to a right value
99+
* @param <R2> the new right parameter type
100+
* @return the Either resulting from applying rightFn to this right value, or this left value if left
101+
*/
53102
public final <R2> Either<L, R2> flatMap(Fn1<? super R, ? extends Either<L, R2>> rightFn) {
54103
return flatMap(Either::left, rightFn);
55104
}
56105

106+
/**
107+
* If a right value, apply <code>rightFn</code> to the unwrapped right value and return the resulting
108+
* <code>Either</code>; otherwise, apply the unwrapped left value to leftFn and return the resulting
109+
* <code>Either</code>.
110+
*
111+
* @param leftFn the function to apply if a left value
112+
* @param rightFn the function to apply if a right value
113+
* @param <L2> the new left parameter type
114+
* @param <R2> the new right parameter type
115+
* @return the result of either rightFn or leftFn, depending on whether this is a right or a left
116+
*/
57117
public final <L2, R2> Either<L2, R2> flatMap(Fn1<? super L, ? extends Either<L2, R2>> leftFn,
58118
Fn1<? super R, ? extends Either<L2, R2>> rightFn) {
59119
return match(leftFn, rightFn);
60120
}
61121

122+
/**
123+
* Given two binary operators over L and R, merge two <code>Either&lt;L, R&gt;</code>s into a single
124+
* <code>Either&lt;L, R&gt;</code>. Note that <code>merge</code> biases towards left values; that is, if any left
125+
* value exists, the result will be a left value, such that only unanimous right values result in an ultimate right
126+
* value.
127+
*
128+
* @param leftFn the binary operator for L
129+
* @param rightFn the binary operator for R
130+
* @param other the other Either
131+
* @return the merged Either
132+
*/
62133
public final Either<L, R> merge(Fn2<? super L, ? super L, ? extends L> leftFn,
63134
Fn2<? super R, ? super R, ? extends R> rightFn,
64135
Either<L, R> other) {
@@ -67,11 +138,24 @@ public final Either<L, R> merge(Fn2<? super L, ? super L, ? extends L> leftFn,
67138
r1 -> other.match(Either::left, r2 -> right(rightFn.apply(r1, r2))));
68139
}
69140

141+
/**
142+
* Perform side-effects against a wrapped right value, returning back the <code>Either</code> unaltered.
143+
*
144+
* @param rightConsumer the effecting consumer
145+
* @return the Either, unaltered
146+
*/
70147
public Either<L, R> peek(Consumer<R> rightConsumer) {
71148
return peek(l -> {
72149
}, rightConsumer);
73150
}
74151

152+
/**
153+
* Perform side-effects against a wrapped right or left value, returning back the <code>Either</code> unaltered.
154+
*
155+
* @param leftConsumer the effecting consumer for left values
156+
* @param rightConsumer the effecting consumer for right values
157+
* @return the Either, unaltered
158+
*/
75159
public Either<L, R> peek(Consumer<L> leftConsumer, Consumer<R> rightConsumer) {
76160
return flatMap(l -> {
77161
leftConsumer.accept(l);
@@ -82,8 +166,17 @@ public Either<L, R> peek(Consumer<L> leftConsumer, Consumer<R> rightConsumer) {
82166
});
83167
}
84168

85-
public abstract <V> V match(Fn1<? super L, ? extends V> leftFn,
86-
Fn1<? super R, ? extends V> rightFn);
169+
/**
170+
* Given two mapping functions (one from an <code>L</code> to a <code>V</code>, one from an <code>R</code> to a
171+
* <code>V</code>), unwrap the value stored in this <code>Either</code>, apply the appropriate mapping function,
172+
* and return the result.
173+
*
174+
* @param leftFn the left value mapping function
175+
* @param rightFn the right value mapping function
176+
* @param <V> the result type
177+
* @return the result of applying the appropriate mapping function to the wrapped value
178+
*/
179+
public abstract <V> V match(Fn1<? super L, ? extends V> leftFn, Fn1<? super R, ? extends V> rightFn);
87180

88181
@Override
89182
public final <R2> Either<L, R2> fmap(Fn1<? super R, ? extends R2> fn) {
@@ -108,11 +201,32 @@ public final <L2, R2> Either<L2, R2> biMap(Fn1<? super L, ? extends L2> leftFn,
108201
return match(l -> left(leftFn.apply(l)), r -> right(rightFn.apply(r)));
109202
}
110203

204+
/**
205+
* Convert an {@link Optional}&lt;R&gt; into an <code>Either&lt;L, R&gt;</code>, supplying the left value from
206+
* <code>leftFn</code> in the case of {@link Optional#empty()}.
207+
*
208+
* @param optional the optional
209+
* @param leftFn the supplier to use for left values
210+
* @param <L> the left parameter type
211+
* @param <R> the right parameter type
212+
* @return a right value of the contained optional value, or a left value of leftFn's result
213+
*/
111214
public static <L, R> Either<L, R> fromOptional(Optional<R> optional, Supplier<L> leftFn) {
112215
return optional.<Either<L, R>>map(Either::right)
113216
.orElse(left(leftFn.get()));
114217
}
115218

219+
/**
220+
* Attempt to execute the {@link CheckedSupplier}, returning its result in a right value. If the supplier throws an
221+
* exception, apply leftFn to it, wrap it in a left value and return it.
222+
*
223+
* @param supplier the supplier of the right value
224+
* @param leftFn a function mapping E to L
225+
* @param <E> the most contravariant exception that the supplier might throw
226+
* @param <L> the left parameter type
227+
* @param <R> the right parameter type
228+
* @return the supplier result as a right value, or leftFn's mapping result as a left value
229+
*/
116230
@SuppressWarnings("unchecked")
117231
public static <E extends Exception, L, R> Either<L, R> trying(CheckedSupplier<E, ? extends R> supplier,
118232
Fn1<? super E, ? extends L> leftFn) {
@@ -123,14 +237,39 @@ public static <E extends Exception, L, R> Either<L, R> trying(CheckedSupplier<E,
123237
}
124238
}
125239

240+
/**
241+
* Attempt to execute the {@link CheckedSupplier}, returning its result in a right value. If the supplier throws an
242+
* exception, wrap it in a left value and return it.
243+
*
244+
* @param supplier the supplier of the right value
245+
* @param <E> the left parameter type (the most contravariant exception that supplier might throw)
246+
* @param <R> the right parameter type
247+
* @return the supplier result as a right value, or a left value of the thrown exception
248+
*/
126249
public static <E extends Exception, R> Either<E, R> trying(CheckedSupplier<E, R> supplier) {
127250
return trying(supplier, id());
128251
}
129252

253+
/**
254+
* Static factory method for creating a left value.
255+
*
256+
* @param l the wrapped value
257+
* @param <L> the left parameter type
258+
* @param <R> the right parameter type
259+
* @return a left value of l
260+
*/
130261
public static <L, R> Either<L, R> left(L l) {
131262
return new Left<>(l);
132263
}
133264

265+
/**
266+
* Static factory method for creating a right value.
267+
*
268+
* @param r the wrapped value
269+
* @param <L> the left parameter type
270+
* @param <R> the right parameter type
271+
* @return a right value of r
272+
*/
134273
public static <L, R> Either<L, R> right(R r) {
135274
return new Right<>(r);
136275
}
@@ -143,8 +282,7 @@ private Left(L l) {
143282
}
144283

145284
@Override
146-
public <V> V match(Fn1<? super L, ? extends V> leftFn,
147-
Fn1<? super R, ? extends V> rightFn) {
285+
public <V> V match(Fn1<? super L, ? extends V> leftFn, Fn1<? super R, ? extends V> rightFn) {
148286
return leftFn.apply(l);
149287
}
150288

@@ -174,8 +312,7 @@ private Right(R r) {
174312
}
175313

176314
@Override
177-
public <V> V match(Fn1<? super L, ? extends V> leftFn,
178-
Fn1<? super R, ? extends V> rightFn) {
315+
public <V> V match(Fn1<? super L, ? extends V> leftFn, Fn1<? super R, ? extends V> rightFn) {
179316
return rightFn.apply(r);
180317
}
181318

0 commit comments

Comments
 (0)