com.mojang.datafixers.util.Either Java Examples

The following examples show how to use com.mojang.datafixers.util.Either. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecursivePoint.java    From DataFixerUpper with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
    @Override
    public <FT, FR> Either<TypedOptic<A, ?, FT, FR>, FieldNotFoundException> findTypeInChildren(final Type<FT> type, final Type<FR> resultType, final TypeMatcher<FT, FR> matcher, final boolean recurse) {
        /*if (!recurse) {
            return delegate.get().findField(name, type, resultType, false).mapLeft(this::wrapOptic);
//                return Either.right(new FieldNotFoundException("Recursion point"));
        }*/
        return family.findType(index, type, resultType, matcher, recurse).mapLeft(o -> {
            if (!Objects.equals(this, o.sType())) {
                throw new IllegalStateException(":/");
            }
            return (TypedOptic<A, ?, FT, FR>) o;
        });
        /*final Either<Pair<TypeTemplate, FieldOptic<?, ?, FT, FR>>, FieldNotFoundException> newTemplate = family.template().findField(family, index, name, type, resultType);
        return newTemplate.mapLeft(nc -> {
            final TypeFamily.Mu newMu = new TypeFamily.Mu(family.name, nc.getLeft());
            final MuType<?> newType = newMu.apply(index);
            if (!Objects.equals(delegate.get(), nc.getRight().sType())) {
                throw new IllegalStateException(":/");
            }
            return cap(newType, type, resultType, (FieldOptic<A, ?, FT, FR>) nc.getRight());
        });*/
    }
 
Example #2
Source File: Optics.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public static <R, A, B> ReForgetE<R, A, B> reForgetE(final String name, final Function<Either<A, R>, B> function) {
    return new ReForgetE<R, A, B>() {
        @Override
        public B run(final Either<A, R> t) {
            return function.apply(t);
        }

        @Override
        public String toString() {
            return "ReForgetE_" + name;
        }
    };
}
 
Example #3
Source File: TypedOptic.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public static <F, G, F2> TypedOptic<Either<F, G>, Either<F2, G>, F, F2> inj1(final Type<F> fType, final Type<G> gType, final Type<F2> newType) {
    return new TypedOptic<>(
        Cocartesian.Mu.TYPE_TOKEN,
        DSL.or(fType, gType),
        DSL.or(newType, gType),
        fType,
        newType,
        new Inj1<>()
    );
}
 
Example #4
Source File: EitherCodec.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <T> DataResult<T> encode(final Either<F, S> input, final DynamicOps<T> ops, final T prefix) {
    return input.map(
        value1 -> first.encode(value1, ops, prefix),
        value2 -> second.encode(value2, ops, prefix)
    );
}
 
Example #5
Source File: EitherMapCodec.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <T> RecordBuilder<T> encode(final Either<F, S> input, final DynamicOps<T> ops, final RecordBuilder<T> prefix) {
    return input.map(
        value1 -> first.encode(value1, ops, prefix),
        value2 -> second.encode(value2, ops, prefix)
    );
}
 
Example #6
Source File: ReForgetEP.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<ReForgetEP.Mu<R>, Either<A, C>, Either<B, C>> left(final App2<ReForgetEP.Mu<R>, A, B> input) {
    final ReForgetEP<R, A, B> reForgetEP = ReForgetEP.unbox(input);
    return Optics.reForgetEP("left",
        e -> e.map(
            e2 -> e2.mapLeft(
                a -> reForgetEP.run(Either.left(a))
            ),
            (Pair<Either<A, C>, R> p) -> p.getFirst().mapLeft(
                a -> reForgetEP.run(Either.right(Pair.of(a, p.getSecond())))
            )
        )
    );
}
 
Example #7
Source File: EitherMapCodec.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <T> DataResult<Either<F, S>> decode(final DynamicOps<T> ops, final MapLike<T> input) {
    final DataResult<Either<F, S>> firstRead = first.decode(ops, input).map(Either::left);
    if (firstRead.result().isPresent()) {
        return firstRead;
    }
    return second.decode(ops, input).map(Either::right);
}
 
Example #8
Source File: Optics.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public static <S, T, A, B> Prism<S, T, A, B> prism(final Function<S, Either<T, A>> match, final Function<B, T> build) {
    return new Prism<S, T, A, B>() {
        @Override
        public Either<T, A> match(final S s) {
            return match.apply(s);
        }

        @Override
        public T build(final B b) {
            return build.apply(b);
        }
    };
}
 
Example #9
Source File: ChunkHolder_scarpetChunkCreationMixin.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<Either<Chunk, ChunkHolder.Unloaded>> setDefaultProtoChunk(ChunkPos chpos, ThreadExecutor<Runnable> executor)
{
    int i = ChunkStatus.EMPTY.getIndex();
    CompletableFuture<Either<Chunk, ChunkHolder.Unloaded>> completableFuture2 = CompletableFuture.supplyAsync(
            () -> Either.left(new ProtoChunk(chpos, UpgradeData.NO_UPGRADE_DATA)),
            executor
    );
    updateFuture(completableFuture2);
    futuresByStatus.set(i, completableFuture2);
    return completableFuture2;
}
 
Example #10
Source File: UnionArgumentType.java    From multiconnect with MIT License 5 votes vote down vote up
@Override
public Either<L, R> parse(StringReader reader) throws CommandSyntaxException {
    int start = reader.getCursor();
    try {
        return Either.left(left.parse(reader));
    } catch (CommandSyntaxException leftError) {
        reader.setCursor(start);
        try {
            return Either.right(right.parse(reader));
        } catch (CommandSyntaxException rightError) {
            reader.setCursor(start);
            throw leftError;
        }
    }
}
 
Example #11
Source File: ReForgetEP.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<ReForgetEP.Mu<R>, Either<C, A>, Either<C, B>> right(final App2<ReForgetEP.Mu<R>, A, B> input) {
    final ReForgetEP<R, A, B> reForgetEP = ReForgetEP.unbox(input);
    return Optics.reForgetEP("right",
        e -> e.map(
            e2 -> e2.mapRight(
                a -> reForgetEP.run(Either.left(a))
            ),
            (Pair<Either<C, A>, R> p) -> p.getFirst().mapRight(
                a -> reForgetEP.run(Either.right(Pair.of(a, p.getSecond())))
            )
        )
    );
}
 
Example #12
Source File: ReForgetE.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<ReForgetE.Mu<R>, Either<C, A>, Either<C, B>> right(final App2<ReForgetE.Mu<R>, A, B> input) {
    final ReForgetE<R, A, B> reForgetE = ReForgetE.unbox(input);
    return Optics.reForgetE("right",
        e -> e.map(
            e2 -> e2.map(
                Either::left,
                a -> Either.right(reForgetE.run(Either.left(a)))
            ),
            r -> Either.right(reForgetE.run(Either.right(r)))
        )
    );
}
 
Example #13
Source File: ReForgetE.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<ReForgetE.Mu<R>, Either<A, C>, Either<B, C>> left(final App2<ReForgetE.Mu<R>, A, B> input) {
    final ReForgetE<R, A, B> reForgetE = ReForgetE.unbox(input);
    return Optics.reForgetE("left",
        e -> e.map(
            e2 -> e2.map(
                a -> Either.left(reForgetE.run(Either.left(a))),
                Either::right
            ),
            r -> Either.left(reForgetE.run(Either.right(r)))
        )
    );
}
 
Example #14
Source File: DataResult.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public DataResult<R> promotePartial(final Consumer<String> onError) {
    return result.map(
        r -> new DataResult<>(Either.left(r), lifecycle),
        r -> {
            onError.accept(r.message);
            return r.partialResult
                .map(pr -> new DataResult<>(Either.left(pr), lifecycle))
                .orElseGet(() -> create(Either.right(r), lifecycle));
        }
    );
}
 
Example #15
Source File: Typed.java    From DataFixerUpper with MIT License 5 votes vote down vote up
private <B, FT, FR> Typed<B> setCap(final TypedOptic<A, B, FT, FR> field, final Typed<FR> newValue) {
    final B b = ReForgetC.unbox(field.apply(
        new TypeToken<ReForgetC.Instance.Mu<FR>>() {},
        new ReForgetC.Instance<>(),
        Optics.reForgetC("set", Either.left(Function.identity())))
    ).run(value, newValue.value);
    return new Typed<>(field.tType(), ops, b);
}
 
Example #16
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <R, A, A2, B, B2> R cap(final Func<B, B2> firstArg, final Func<A, A2> secondArg, final Apply<?, ?> first, final Apply<?, ?> second) {
    return (R) Functions.comp(
        DSL.or(secondArg.first(), firstArg.second()),
        (PointFree<Function<Either<A, B2>, Either<A2, B2>>>) second,
        (PointFree<Function<Either<A, B>, Either<A, B2>>>) first
    );
}
 
Example #17
Source File: Sum.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public Optional<RewriteResult<Either<F, G>, ?>> one(final TypeRewriteRule rule) {
    return DataFixUtils.or(
        rule.rewrite(first).map(v -> fixLeft(this, first, second, v)),
        () -> rule.rewrite(second).map(v -> fixRight(this, first, second, v))
    );
}
 
Example #18
Source File: Tag.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <FT, FR> Either<TypeTemplate, Type.FieldNotFoundException> findFieldOrType(final int index, @Nullable final String name, final Type<FT> type, final Type<FR> resultType) {
    if (!Objects.equals(name, this.name)) {
        return Either.right(new Type.FieldNotFoundException("Names don't match"));
    }
    if (element instanceof Const) {
        final Const c = (Const) element;
        if (Objects.equals(type, c.type())) {
            return Either.left(new Tag(name, new Const(resultType)));
        }
        return Either.right(new Type.FieldNotFoundException("don't match"));
    }
    // safe to return the same template
    if (Objects.equals(type, resultType)) {
        return Either.left(this);
    }
    if (type instanceof RecursivePoint.RecursivePointType<?> && element instanceof RecursivePoint) {
        if (((RecursivePoint) element).index() == ((RecursivePoint.RecursivePointType<?>) type).index()) {
            if (resultType instanceof RecursivePoint.RecursivePointType<?>) {
                if (((RecursivePoint.RecursivePointType<?>) resultType).index() == ((RecursivePoint) element).index()) {
                    return Either.left(this);
                }
            } else {
                return Either.left(DSL.constType(resultType));
            }
        }
    }
    return Either.right(new Type.FieldNotFoundException("Recursive field"));
}
 
Example #19
Source File: Sum.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <FT, FR> Either<TypeTemplate, Type.FieldNotFoundException> findFieldOrType(final int index, @Nullable final String name, final Type<FT> type, final Type<FR> resultType) {
    final Either<TypeTemplate, Type.FieldNotFoundException> either = f.findFieldOrType(index, name, type, resultType);
    return either.map(
        f2 -> Either.left(new Sum(f2, g)),
        r -> g.findFieldOrType(index, name, type, resultType).mapLeft(g2 -> new Sum(f, g2))
    );
}
 
Example #20
Source File: Const.java    From DataFixerUpper with MIT License 5 votes vote down vote up
private <T, A, B> TypedOptic<T, T, A, B> makeIgnoreOptic(final Type<T> type, final Type<A> aType, final Type<B> bType) {
    return new TypedOptic<>(
        AffineP.Mu.TYPE_TOKEN,
        type,
        type,
        aType,
        bType,
        Optics.affine(Either::left, (b, t) -> t)
    );
}
 
Example #21
Source File: ReForgetEP.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C, D> FunctionType<App2<ReForgetEP.Mu<R>, A, B>, App2<ReForgetEP.Mu<R>, C, D>> dimap(final Function<C, A> g, final Function<B, D> h) {
    return input -> Optics.reForgetEP("dimap", e -> {
        final Either<A, Pair<A, R>> either = e.mapBoth(g, p -> Pair.of(g.apply(p.getFirst()), p.getSecond()));
        final B b = ReForgetEP.unbox(input).run(either);
        final D d = h.apply(b);
        return d;
    });
}
 
Example #22
Source File: Product.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <FT, FR> Either<TypedOptic<Pair<F, G>, ?, FT, FR>, FieldNotFoundException> findTypeInChildren(final Type<FT> type, final Type<FR> resultType, final TypeMatcher<FT, FR> matcher, final boolean recurse) {
    final Either<TypedOptic<F, ?, FT, FR>, FieldNotFoundException> firstFieldLens = first.findType(type, resultType, matcher, recurse);
    return firstFieldLens.map(
        this::capLeft,
        r -> {
            final Either<TypedOptic<G, ?, FT, FR>, FieldNotFoundException> secondFieldLens = second.findType(type, resultType, matcher, recurse);
            return secondFieldLens.mapLeft(this::capRight);
        }
    );
}
 
Example #23
Source File: DataResult.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public <R2> DataResult<R2> ap(final DataResult<Function<R, R2>> functionResult) {
    return create(result.map(
        arg -> functionResult.result.mapBoth(
            func -> func.apply(arg),
            funcError -> new PartialResult<>(funcError.message, funcError.partialResult.map(f -> f.apply(arg)))
        ),
        argError -> Either.right(functionResult.result.map(
            func -> new PartialResult<>(argError.message, argError.partialResult.map(func)),
            funcError -> new PartialResult<>(
                appendMessages(argError.message, funcError.message),
                argError.partialResult.flatMap(a -> funcError.partialResult.map(f -> f.apply(a)))
            )
        ))
    ), lifecycle.add(functionResult.lifecycle));
}
 
Example #24
Source File: ReForgetE.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C, D> FunctionType<App2<ReForgetE.Mu<R>, A, B>, App2<ReForgetE.Mu<R>, C, D>> dimap(final Function<C, A> g, final Function<B, D> h) {
    return input -> Optics.reForgetE("dimap", e -> {
        final Either<A, R> either = e.mapLeft(g);
        final B b = ReForgetE.unbox(input).run(either);
        final D d = h.apply(b);
        return d;
    });
}
 
Example #25
Source File: CompoundList.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public <FT, FR> Either<TypeTemplate, Type.FieldNotFoundException> findFieldOrType(final int index, @Nullable final String name, final Type<FT> type, final Type<FR> resultType) {
    return element.findFieldOrType(index, name, type, resultType).mapLeft(element1 -> new CompoundList(key, element1));
}
 
Example #26
Source File: Forget.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public <A, B, C> App2<Forget.Mu<R>, A, B> unleft(final App2<Forget.Mu<R>, Either<A, C>, Either<B, C>> input) {
    return Optics.forget(a -> Forget.unbox(input).run(Either.left(a)));
}
 
Example #27
Source File: ForgetOpt.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public <A, B, C> App2<ForgetOpt.Mu<R>, Either<A, C>, Either<B, C>> left(final App2<ForgetOpt.Mu<R>, A, B> input) {
    return Optics.forgetOpt(e -> e.left().flatMap(ForgetOpt.unbox(input)::run));
}
 
Example #28
Source File: Forget.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public <A, B, C> App2<Forget.Mu<R>, A, B> unright(final App2<Forget.Mu<R>, Either<C, A>, Either<C, B>> input) {
    return Optics.forget(a -> Forget.unbox(input).run(Either.right(a)));
}
 
Example #29
Source File: Inj2.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public Either<F, G2> build(final G2 g2) {
    return Either.right(g2);
}
 
Example #30
Source File: Optics.java    From DataFixerUpper with MIT License 4 votes vote down vote up
public static <S, T, A, B> Prism<S, T, A, B> toPrism(final Optic<? super Cocartesian.Mu, S, T, A, B> optic) {
    final Function<App2<Prism.Mu<A, B>, A, B>, App2<Prism.Mu<A, B>, S, T>> eval = optic.eval(new Prism.Instance<>());
    return Prism.unbox(eval.apply(prism(Either::right, Function.identity())));
}