|
| 1 | +package com.jnape.palatable.lambda.adt.hmap; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.Maybe; |
| 4 | +import com.jnape.palatable.lambda.adt.hlist.HList; |
| 5 | +import com.jnape.palatable.lambda.adt.hlist.HList.HCons; |
| 6 | +import com.jnape.palatable.lambda.adt.hlist.SingletonHList; |
| 7 | +import com.jnape.palatable.lambda.adt.hlist.Tuple2; |
| 8 | +import com.jnape.palatable.lambda.adt.hlist.Tuple3; |
| 9 | +import com.jnape.palatable.lambda.adt.hlist.Tuple4; |
| 10 | +import com.jnape.palatable.lambda.adt.hlist.Tuple5; |
| 11 | +import com.jnape.palatable.lambda.adt.hlist.Tuple6; |
| 12 | +import com.jnape.palatable.lambda.adt.hlist.Tuple7; |
| 13 | +import com.jnape.palatable.lambda.adt.hlist.Tuple8; |
| 14 | +import com.jnape.palatable.lambda.functions.builtin.fn2.Both; |
| 15 | +import com.jnape.palatable.lambda.lens.Lens; |
| 16 | + |
| 17 | +import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into; |
| 18 | +import static com.jnape.palatable.lambda.lens.lenses.HMapLens.valueAt; |
| 19 | + |
| 20 | +public interface Schema<Values extends HCons<?, ?>> extends Lens.Simple<HMap, Maybe<Values>> { |
| 21 | + |
| 22 | + @SuppressWarnings("unchecked") |
| 23 | + default <A, NewValues extends HCons<A, Values>> Schema<NewValues> add(TypeSafeKey<?, A> key) { |
| 24 | + return Lens.both(this, valueAt(key)) |
| 25 | + .<Maybe<NewValues>>mapA(into((maybeValues, maybeA) -> maybeValues.zip(maybeA.fmap(a -> values -> (NewValues) values.cons(a))))) |
| 26 | + .<Maybe<NewValues>>mapB(Both.both(maybeNewValues -> maybeNewValues.fmap(HCons::tail), |
| 27 | + maybeNewValues -> maybeNewValues.fmap(HCons::head))) |
| 28 | + ::apply; |
| 29 | + } |
| 30 | + |
| 31 | + static <A> Schema<SingletonHList<A>> schema(TypeSafeKey<?, A> key) { |
| 32 | + return valueAt(key) |
| 33 | + .mapA(ma -> ma.fmap(HList::singletonHList)) |
| 34 | + .<Maybe<SingletonHList<A>>>mapB(maybeSingletonA -> maybeSingletonA.fmap(HCons::head)) |
| 35 | + ::apply; |
| 36 | + } |
| 37 | + |
| 38 | + static <A, B> Schema<Tuple2<A, B>> schema(TypeSafeKey<?, A> aKey, |
| 39 | + TypeSafeKey<?, B> bKey) { |
| 40 | + return schema(bKey).add(aKey); |
| 41 | + } |
| 42 | + |
| 43 | + static <A, B, C> Schema<Tuple3<A, B, C>> schema(TypeSafeKey<?, A> aKey, |
| 44 | + TypeSafeKey<?, B> bKey, |
| 45 | + TypeSafeKey<?, C> cKey) { |
| 46 | + return schema(bKey, cKey).add(aKey); |
| 47 | + } |
| 48 | + |
| 49 | + static <A, B, C, D> Schema<Tuple4<A, B, C, D>> schema(TypeSafeKey<?, A> aKey, |
| 50 | + TypeSafeKey<?, B> bKey, |
| 51 | + TypeSafeKey<?, C> cKey, |
| 52 | + TypeSafeKey<?, D> dKey) { |
| 53 | + return schema(bKey, cKey, dKey).add(aKey); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + static <A, B, C, D, E> Schema<Tuple5<A, B, C, D, E>> schema(TypeSafeKey<?, A> aKey, |
| 58 | + TypeSafeKey<?, B> bKey, |
| 59 | + TypeSafeKey<?, C> cKey, |
| 60 | + TypeSafeKey<?, D> dKey, |
| 61 | + TypeSafeKey<?, E> eKey) { |
| 62 | + return schema(bKey, cKey, dKey, eKey).add(aKey); |
| 63 | + } |
| 64 | + |
| 65 | + static <A, B, C, D, E, F> Schema<Tuple6<A, B, C, D, E, F>> schema(TypeSafeKey<?, A> aKey, |
| 66 | + TypeSafeKey<?, B> bKey, |
| 67 | + TypeSafeKey<?, C> cKey, |
| 68 | + TypeSafeKey<?, D> dKey, |
| 69 | + TypeSafeKey<?, E> eKey, |
| 70 | + TypeSafeKey<?, F> fKey) { |
| 71 | + return schema(bKey, cKey, dKey, eKey, fKey).add(aKey); |
| 72 | + } |
| 73 | + |
| 74 | + static <A, B, C, D, E, F, G> Schema<Tuple7<A, B, C, D, E, F, G>> schema(TypeSafeKey<?, A> aKey, |
| 75 | + TypeSafeKey<?, B> bKey, |
| 76 | + TypeSafeKey<?, C> cKey, |
| 77 | + TypeSafeKey<?, D> dKey, |
| 78 | + TypeSafeKey<?, E> eKey, |
| 79 | + TypeSafeKey<?, F> fKey, |
| 80 | + TypeSafeKey<?, G> gKey) { |
| 81 | + return schema(bKey, cKey, dKey, eKey, fKey, gKey).add(aKey); |
| 82 | + } |
| 83 | + |
| 84 | + static <A, B, C, D, E, F, G, H> Schema<Tuple8<A, B, C, D, E, F, G, H>> schema(TypeSafeKey<?, A> aKey, |
| 85 | + TypeSafeKey<?, B> bKey, |
| 86 | + TypeSafeKey<?, C> cKey, |
| 87 | + TypeSafeKey<?, D> dKey, |
| 88 | + TypeSafeKey<?, E> eKey, |
| 89 | + TypeSafeKey<?, F> fKey, |
| 90 | + TypeSafeKey<?, G> gKey, |
| 91 | + TypeSafeKey<?, H> hKey) { |
| 92 | + return schema(bKey, cKey, dKey, eKey, fKey, gKey, hKey).add(aKey); |
| 93 | + } |
| 94 | +} |
0 commit comments