Java Code Examples for com.mojang.datafixers.util.Either#left()

The following examples show how to use com.mojang.datafixers.util.Either#left() . 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: DataResult.java    From DataFixerUpper with MIT License 6 votes vote down vote up
@Override
public <A, B, R> App<DataResult.Mu, R> ap2(final App<DataResult.Mu, BiFunction<A, B, R>> func, final App<DataResult.Mu, A> a, final App<DataResult.Mu, B> b) {
    final DataResult<BiFunction<A, B, R>> fr = unbox(func);
    final DataResult<A> ra = unbox(a);
    final DataResult<B> rb = unbox(b);

    // for less recursion
    if (fr.result.left().isPresent()
        && ra.result.left().isPresent()
        && rb.result.left().isPresent()
    ) {
        return new DataResult<>(Either.left(fr.result.left().get().apply(
            ra.result.left().get(),
            rb.result.left().get()
        )), fr.lifecycle.add(ra.lifecycle).add(rb.lifecycle));
    }

    return Applicative.super.ap2(func, a, b);
}
 
Example 2
Source File: DataResult.java    From DataFixerUpper with MIT License 6 votes vote down vote up
@Override
public <T1, T2, T3, R> App<DataResult.Mu, R> ap3(final App<DataResult.Mu, Function3<T1, T2, T3, R>> func, final App<DataResult.Mu, T1> t1, final App<DataResult.Mu, T2> t2, final App<DataResult.Mu, T3> t3) {
    final DataResult<Function3<T1, T2, T3, R>> fr = unbox(func);
    final DataResult<T1> dr1 = unbox(t1);
    final DataResult<T2> dr2 = unbox(t2);
    final DataResult<T3> dr3 = unbox(t3);

    // for less recursion
    if (fr.result.left().isPresent()
        && dr1.result.left().isPresent()
        && dr2.result.left().isPresent()
        && dr3.result.left().isPresent()
    ) {
        return new DataResult<>(Either.left(fr.result.left().get().apply(
            dr1.result.left().get(),
            dr2.result.left().get(),
            dr3.result.left().get()
        )), fr.lifecycle.add(dr1.lifecycle).add(dr2.lifecycle).add(dr3.lifecycle));
    }

    return Applicative.super.ap3(func, t1, t2, t3);
}
 
Example 3
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 4
Source File: Sum.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <FT, FR> Either<TypedOptic<Either<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> firstOptic = first.findType(type, resultType, matcher, recurse);
    final Either<TypedOptic<G, ?, FT, FR>, FieldNotFoundException> secondOptic = second.findType(type, resultType, matcher, recurse);
    if (firstOptic.left().isPresent() && secondOptic.left().isPresent()) {
        return Either.left(mergeOptics(firstOptic.left().get(), secondOptic.left().get()));
    }
    if (firstOptic.left().isPresent()) {
        return firstOptic.mapLeft(this::capLeft);
    }
    return secondOptic.mapLeft(this::capRight);
}
 
Example 5
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 6
Source File: NamedChoiceFinder.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <S> Either<TypedOptic<S, ?, FT, FR>, Type.FieldNotFoundException> match(final Type<S> targetType) {
    /*if (targetType instanceof Type.NamedType<?>) {
        final Type.NamedType<?> namedType = (Type.NamedType<?>) targetType;
        if (!Objects.equals(namedType.name, name)) {
            return Either.right(new Type.FieldNotFoundException(String.format("Not found: \"%s\" (type: %s)", name, targetType)));
        }
        if (!Objects.equals(type, namedType.element)) {
            return Either.right(new Type.FieldNotFoundException(String.format("Type error for named type \"%s\": expected type: %s, actual type: %s)", name, targetType, namedType.element)));
        }
        return Either.left((Type.TypedOptic<S, ?, FT, FR>) cap(namedType));
    }*/
    if (targetType instanceof TaggedChoice.TaggedChoiceType<?>) {
        final TaggedChoice.TaggedChoiceType<?> choiceType = (TaggedChoice.TaggedChoiceType<?>) targetType;
        final Type<?> elementType = choiceType.types().get(name);
        if (elementType != null) {
            if (!Objects.equals(type, elementType)) {
                return Either.right(new Type.FieldNotFoundException(String.format("Type error for choice type \"%s\": expected type: %s, actual type: %s)", name, targetType, elementType)));
            }
            return Either.left((TypedOptic<S, ?, FT, FR>) tagged((TaggedChoice.TaggedChoiceType<String>) choiceType, name, type, resultType));
        }
        return Either.right(new Type.Continue());
    }
    if (targetType instanceof Tag.TagType<?>) {
        return Either.right(new Type.FieldNotFoundException("in tag"));
    }
    return Either.right(new Type.Continue());
}
 
Example 7
Source File: DataResult.java    From DataFixerUpper with MIT License 4 votes vote down vote up
public static <R> DataResult<R> success(final R result, final Lifecycle experimental) {
    return new DataResult<>(Either.left(result), experimental);
}
 
Example 8
Source File: FieldFinder.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <S> Either<TypedOptic<S, ?, FT, FR>, Type.FieldNotFoundException> match(final Type<S> targetType) {
    if (name == null && type.equals(targetType, true, false)) {
        return Either.left((TypedOptic<S, FR, FT, FR>) new TypedOptic<>(
            Profunctor.Mu.TYPE_TOKEN,
            targetType,
            resultType,
            targetType,
            resultType,
            Optics.id()
        ));
    }
    if (targetType instanceof Tag.TagType<?>) {
        final Tag.TagType<S> tagType = (Tag.TagType<S>) targetType;
        if (!Objects.equals(tagType.name(), name)) {
            return Either.right(new Type.FieldNotFoundException(String.format("Not found: \"%s\" (in type: %s)", name, targetType)));
        }
        if (!Objects.equals(type, tagType.element())) {
            return Either.right(new Type.FieldNotFoundException(String.format("Type error for field \"%s\": expected type: %s, actual type: %s)", name, type, tagType.element())));
        }
        return Either.left(new TypedOptic<>(
            Profunctor.Mu.TYPE_TOKEN,
            tagType,
            DSL.field(tagType.name(), resultType),
            type,
            resultType,
            (Adapter<S, FR, FT, FR>) Optics.id()
        ));
    }
    if (targetType instanceof TaggedChoice.TaggedChoiceType<?>) {
        final TaggedChoice.TaggedChoiceType<FT> choiceType = (TaggedChoice.TaggedChoiceType<FT>) targetType;
        if (Objects.equals(name, choiceType.getName())) {
            if (!Objects.equals(type, choiceType.getKeyType())) {
                return Either.right(new Type.FieldNotFoundException(String.format("Type error for field \"%s\": expected type: %s, actual type: %s)", name, type, choiceType.getKeyType())));
            }
            if (!Objects.equals(type, resultType)) {
                return Either.right(new Type.FieldNotFoundException("TaggedChoiceType key type change is unsupported."));
            }
            return Either.left((TypedOptic<S, ?, FT, FR>) capChoice(choiceType));
        }
    }
    return Either.right(new Type.Continue());
}
 
Example 9
Source File: CompoundList.java    From DataFixerUpper with MIT License 4 votes vote down vote up
private <FT, K2, FR> Either<TypedOptic<List<Pair<K, V>>, ?, FT, FR>, FieldNotFoundException> capLeft(final TypedOptic<K, K2, FT, FR> optic) {
    return Either.left(TypedOptic.compoundListKeys(optic.sType(), optic.tType(), element).compose(optic));
}
 
Example 10
Source File: Product.java    From DataFixerUpper with MIT License 4 votes vote down vote up
private <FT, F2, FR> Either<TypedOptic<Pair<F, G>, ?, FT, FR>, FieldNotFoundException> capLeft(final TypedOptic<F, F2, FT, FR> optic) {
    return Either.left(TypedOptic.proj1(optic.sType(), second, optic.tType()).compose(optic));
}
 
Example 11
Source File: InjTagged.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Either<Pair<K, ?>, A> match(final Pair<K, ?> pair) {
    return Objects.equals(key, pair.getFirst()) ? Either.right((A) pair.getSecond()) : Either.left(pair);
}
 
Example 12
Source File: Inj1.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public Either<F2, G> build(final F2 f2) {
    return Either.left(f2);
}