|
| 1 | +package com.jnape.palatable.lambda.adt.choice; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.coproduct.CoProduct2; |
| 4 | +import com.jnape.palatable.lambda.adt.coproduct.CoProduct3; |
| 5 | +import com.jnape.palatable.lambda.functor.Bifunctor; |
| 6 | +import com.jnape.palatable.lambda.functor.Functor; |
| 7 | + |
| 8 | +import java.util.Objects; |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +/** |
| 12 | + * Canonical ADT representation of {@link CoProduct3} that is also a {@link Functor} and {@link Bifunctor}. |
| 13 | + * |
| 14 | + * @param <A> a type parameter representing the first possible type of this choice |
| 15 | + * @param <B> a type parameter representing the second possible type of this choice |
| 16 | + * @param <C> a type parameter representing the third possible type of this choice |
| 17 | + * @see Choice2 |
| 18 | + * @see Choice4 |
| 19 | + */ |
| 20 | +public abstract class Choice3<A, B, C> implements CoProduct3<A, B, C>, Functor<C>, Bifunctor<B, C> { |
| 21 | + |
| 22 | + private Choice3() { |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public final <D> Choice4<A, B, C, D> diverge() { |
| 27 | + return match(Choice4::a, Choice4::b, Choice4::c); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public final Choice2<A, B> converge(Function<? super C, ? extends CoProduct2<A, B>> convergenceFn) { |
| 32 | + return match(Choice2::a, Choice2::b, convergenceFn.andThen(cp2 -> cp2.match(Choice2::a, Choice2::b))); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public final <D> Choice3<A, B, D> fmap(Function<? super C, ? extends D> fn) { |
| 37 | + return biMapR(fn); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + @SuppressWarnings("unchecked") |
| 42 | + public final <D> Choice3<A, D, C> biMapL(Function<? super B, ? extends D> fn) { |
| 43 | + return (Choice3<A, D, C>) Bifunctor.super.biMapL(fn); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + @SuppressWarnings("unchecked") |
| 48 | + public final <D> Choice3<A, B, D> biMapR(Function<? super C, ? extends D> fn) { |
| 49 | + return (Choice3<A, B, D>) Bifunctor.super.biMapR(fn); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public final <D, E> Choice3<A, D, E> biMap(Function<? super B, ? extends D> lFn, |
| 54 | + Function<? super C, ? extends E> rFn) { |
| 55 | + return match(Choice3::a, b -> b(lFn.apply(b)), c -> c(rFn.apply(c))); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Static factory method for wrapping a value of type <code>A</code> in a {@link Choice3}. |
| 60 | + * |
| 61 | + * @param a the value |
| 62 | + * @param <A> a type parameter representing the first possible type of this choice |
| 63 | + * @param <B> a type parameter representing the second possible type of this choice |
| 64 | + * @param <C> a type parameter representing the third possible type of this choice |
| 65 | + * @return the wrapped value as a Choice3<A, B, C> |
| 66 | + */ |
| 67 | + public static <A, B, C> Choice3<A, B, C> a(A a) { |
| 68 | + return new _A<>(a); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Static factory method for wrapping a value of type <code>A</code> in a {@link Choice3}. |
| 73 | + * |
| 74 | + * @param b the value |
| 75 | + * @param <A> a type parameter representing the first possible type of this choice |
| 76 | + * @param <B> a type parameter representing the second possible type of this choice |
| 77 | + * @param <C> a type parameter representing the third possible type of this choice |
| 78 | + * @return the wrapped value as a Choice3<A, B, C> |
| 79 | + */ |
| 80 | + public static <A, B, C> Choice3<A, B, C> b(B b) { |
| 81 | + return new _B<>(b); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Static factory method for wrapping a value of type <code>A</code> in a {@link Choice3}. |
| 86 | + * |
| 87 | + * @param c the value |
| 88 | + * @param <A> a type parameter representing the first possible type of this choice |
| 89 | + * @param <B> a type parameter representing the second possible type of this choice |
| 90 | + * @param <C> a type parameter representing the third possible type of this choice |
| 91 | + * @return the wrapped value as a Choice3<A, B, C> |
| 92 | + */ |
| 93 | + public static <A, B, C> Choice3<A, B, C> c(C c) { |
| 94 | + return new _C<>(c); |
| 95 | + } |
| 96 | + |
| 97 | + private static final class _A<A, B, C> extends Choice3<A, B, C> { |
| 98 | + |
| 99 | + private final A a; |
| 100 | + |
| 101 | + private _A(A a) { |
| 102 | + this.a = a; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn, |
| 107 | + Function<? super C, ? extends R> cFn) { |
| 108 | + return aFn.apply(a); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean equals(Object other) { |
| 113 | + return other instanceof _A |
| 114 | + && Objects.equals(a, ((_A) other).a); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public int hashCode() { |
| 119 | + return Objects.hash(a); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String toString() { |
| 124 | + return "Choice3{" + |
| 125 | + "a=" + a + |
| 126 | + '}'; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static final class _B<A, B, C> extends Choice3<A, B, C> { |
| 131 | + |
| 132 | + private final B b; |
| 133 | + |
| 134 | + private _B(B b) { |
| 135 | + this.b = b; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn, |
| 140 | + Function<? super C, ? extends R> cFn) { |
| 141 | + return bFn.apply(b); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean equals(Object other) { |
| 146 | + return other instanceof _B |
| 147 | + && Objects.equals(b, ((_B) other).b); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int hashCode() { |
| 152 | + return Objects.hash(b); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public String toString() { |
| 157 | + return "Choice3{" + |
| 158 | + "b=" + b + |
| 159 | + '}'; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static final class _C<A, B, C> extends Choice3<A, B, C> { |
| 164 | + |
| 165 | + private final C c; |
| 166 | + |
| 167 | + private _C(C c) { |
| 168 | + this.c = c; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn, |
| 173 | + Function<? super C, ? extends R> cFn) { |
| 174 | + return cFn.apply(c); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public boolean equals(Object other) { |
| 179 | + return other instanceof _C |
| 180 | + && Objects.equals(c, ((_C) other).c); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public int hashCode() { |
| 185 | + return Objects.hash(c); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public String toString() { |
| 190 | + return "Choice3{" + |
| 191 | + "c=" + c + |
| 192 | + '}'; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | +} |
0 commit comments