com.mojang.datafixers.types.Type Java Examples

The following examples show how to use com.mojang.datafixers.types.Type. 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: DataFix.java    From DataFixerUpper with MIT License 6 votes vote down vote up
protected <A, B> TypeRewriteRule writeFixAndRead(final String name, final Type<A> type, final Type<B> newType, final Function<Dynamic<?>, Dynamic<?>> fix) {
    return fixTypeEverywhere(name, type, newType, ops -> input -> {
        final Optional<? extends Dynamic<?>> written = type.writeDynamic(ops, input).resultOrPartial(LOGGER::error);
        if (!written.isPresent()) {
            throw new RuntimeException("Could not write the object in " + name);
        }
        final Optional<? extends Pair<Typed<B>, ?>> read = newType.readTyped(fix.apply(written.get())).resultOrPartial(LOGGER::error);
        if (!read.isPresent()) {
            throw new RuntimeException("Could not read the new object in " + name);
        }
        return read.get().getFirst().getValue();
    });
}
 
Example #2
Source File: TypedOptic.java    From DataFixerUpper with MIT License 6 votes vote down vote up
public static <K, A, B> TypedOptic<Pair<K, ?>, Pair<K, ?>, A, B> tagged(final TaggedChoice.TaggedChoiceType<K> sType, final K key, final Type<A> aType, final Type<B> bType) {
    if (!Objects.equals(sType.types().get(key), aType)) {
        throw new IllegalArgumentException("Focused type doesn't match.");
    }
    final Map<K, Type<?>> newTypes = Maps.newHashMap(sType.types());
    newTypes.put(key, bType);
    final Type<Pair<K, ?>> pairType = DSL.taggedChoiceType(sType.getName(), sType.getKeyType(), newTypes);
    return new TypedOptic<>(
        Cocartesian.Mu.TYPE_TOKEN,
        sType,
        pairType,
        aType,
        bType,
        new InjTagged<>(key)
    );
}
 
Example #3
Source File: TaggedChoice.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public Optional<RewriteResult<Pair<K, ?>, ?>> one(final TypeRewriteRule rule) {
    for (final Map.Entry<K, Type<?>> entry : types.entrySet()) {
        final Optional<? extends RewriteResult<?, ?>> elementResult = rule.rewrite(entry.getValue());
        if (elementResult.isPresent()) {
            return Optional.of(elementResult(entry.getKey(), this, elementResult.get()));
        }
    }
    return Optional.empty();
}
 
Example #4
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<? extends PointFree<?>> doRewrite(final Type<A> type, final Type<?> middleType, final PointFree<? extends Function<?, ?>> first, final PointFree<? extends Function<?, ?>> second) {
    if (first instanceof Apply<?, ?> && second instanceof Apply<?, ?>) {
        final Apply<?, ?> applyFirst = (Apply<?, ?>) first;
        final Apply<?, ?> applySecond = (Apply<?, ?>) second;
        final PointFree<? extends Function<?, ?>> firstFunc = applyFirst.func;
        final PointFree<? extends Function<?, ?>> secondFunc = applySecond.func;
        if (firstFunc instanceof ProfunctorTransformer<?, ?, ?, ?> && secondFunc instanceof ProfunctorTransformer<?, ?, ?, ?>) {
            final ProfunctorTransformer<?, ?, ?, ?> firstOptic = (ProfunctorTransformer<?, ?, ?, ?>) firstFunc;
            final ProfunctorTransformer<?, ?, ?, ?> secondOptic = (ProfunctorTransformer<?, ?, ?, ?>) secondFunc;

            Optic<?, ?, ?, ?, ?> fo = firstOptic.optic;
            while (fo instanceof Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) {
                fo = ((Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) fo).outer();
            }

            Optic<?, ?, ?, ?, ?> so = secondOptic.optic;
            while (so instanceof Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) {
                so = ((Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) so).outer();
            }

            if (Objects.equals(fo, Optics.inj2()) && Objects.equals(so, Optics.inj1())) {
                final Func<?, ?> firstArg = (Func<?, ?>) applyFirst.argType;
                final Func<?, ?> secondArg = (Func<?, ?>) applySecond.argType;
                return Optional.of(cap(firstArg, secondArg, applyFirst, applySecond));
            }
        }
    }
    return Optional.empty();
}
 
Example #5
Source File: Typed.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public <FT, FR> Typed<?> updateTyped(final OpticFinder<FT> optic, final Type<FR> newType, final Function<Typed<?>, Typed<?>> updater) {
    final TypedOptic<A, ?, FT, FR> field = optic.findType(type, newType, false).orThrow();
    return updateCap(field, ft -> {
        final Typed<?> newValue = updater.apply(new Typed<>(optic.type(), ops, ft));
        return field.bType().ifSame(newValue).orElseThrow(() -> new IllegalArgumentException("Function didn't update to the expected type"));
    });
}
 
Example #6
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<? extends PointFree<?>> doRewrite(final Type<A> type, final Type<?> middleType, final PointFree<? extends Function<?, ?>> first, final PointFree<? extends Function<?, ?>> second) {
    if (first instanceof Apply<?, ?> && second instanceof Apply<?, ?>) {
        final Apply<?, ?> applyFirst = (Apply<?, ?>) first;
        final Apply<?, ?> applySecond = (Apply<?, ?>) second;
        final PointFree<? extends Function<?, ?>> firstFunc = applyFirst.func;
        final PointFree<? extends Function<?, ?>> secondFunc = applySecond.func;
        if (firstFunc instanceof ProfunctorTransformer<?, ?, ?, ?> && secondFunc instanceof ProfunctorTransformer<?, ?, ?, ?>) {
            final ProfunctorTransformer<?, ?, ?, ?> firstOptic = (ProfunctorTransformer<?, ?, ?, ?>) firstFunc;
            final ProfunctorTransformer<?, ?, ?, ?> secondOptic = (ProfunctorTransformer<?, ?, ?, ?>) secondFunc;

            Optic<?, ?, ?, ?, ?> fo = firstOptic.optic;
            while (fo instanceof Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) {
                fo = ((Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) fo).outer();
            }

            Optic<?, ?, ?, ?, ?> so = secondOptic.optic;
            while (so instanceof Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) {
                so = ((Optic.CompositionOptic<?, ?, ?, ?, ?, ?, ?>) so).outer();
            }

            if (Objects.equals(fo, Optics.proj2()) && Objects.equals(so, Optics.proj1())) {
                final Func<?, ?> firstArg = (Func<?, ?>) applyFirst.argType;
                final Func<?, ?> secondArg = (Func<?, ?>) applySecond.argType;
                return Optional.of(cap(firstArg, secondArg, applyFirst, applySecond));
            }
        }
    }
    return Optional.empty();
}
 
Example #7
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<? extends PointFree<A>> rewrite(final Type<A> type, final PointFree<A> expr) {
    Optional<? extends PointFree<A>> result = Optional.of(expr);
    for (final Supplier<PointFreeRule> rule : rules) {
        result = result.flatMap(pf -> rule.get().<A>rewrite(type, pf));
    }
    return result;
}
 
Example #8
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<? extends PointFree<A>> rewrite(final Type<A> type, final PointFree<A> expr) {
    if (expr instanceof Comp<?, ?, ?>) {
        final Comp<?, ?, ?> comp = (Comp<?, ?, ?>) expr;
        final PointFree<? extends Function<?, ?>> first = comp.first;
        final PointFree<? extends Function<?, ?>> second = comp.second;
        if (first instanceof ProfunctorTransformer<?, ?, ?, ?> && second instanceof ProfunctorTransformer<?, ?, ?, ?>) {
            final ProfunctorTransformer<?, ?, ?, ?> firstOptic = (ProfunctorTransformer<?, ?, ?, ?>) first;
            final ProfunctorTransformer<?, ?, ?, ?> secondOptic = (ProfunctorTransformer<?, ?, ?, ?>) second;
            return Optional.of(cap(firstOptic, secondOptic));
        }
    }
    return Optional.empty();
}
 
Example #9
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 #10
Source File: CompoundList.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B> FamilyOptic<A, B> applyO(final FamilyOptic<A, B> input, final Type<A> aType, final Type<B> bType) {
    return TypeFamily.familyOptic(
        i -> {
            final OpticParts<A, B> optic = element.applyO(input, aType, bType).apply(i);
            final Set<TypeToken<? extends K1>> bounds = Sets.newHashSet(optic.bounds());
            bounds.add(TraversalP.Mu.TYPE_TOKEN);
            return new OpticParts<>(bounds, cap(optic.optic()));
        }
    );
}
 
Example #11
Source File: Functions.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <A, B, C> PointFree<Function<A, C>> comp(final Type<B> middleType, final PointFree<Function<B, C>> f1, final PointFree<Function<A, B>> f2) {
    if (Objects.equals(f1, id())) {
        return (PointFree<Function<A, C>>) (PointFree<?>) f2;
    }
    if (Objects.equals(f2, id())) {
        return (PointFree<Function<A, C>>) (PointFree<?>) f1;
    }
    return new Comp<>(middleType, f1, f2);
}
 
Example #12
Source File: Check.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public Optional<Type<?>> findFieldTypeOpt(final String name) {
    if (index == expectedIndex) {
        return delegate.findFieldTypeOpt(name);
    }
    return Optional.empty();
}
 
Example #13
Source File: Typed.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public <FT, FR> Typed<?> updateRecursiveTyped(final OpticFinder<FT> optic, final Type<FR> newType, final Function<Typed<?>, Typed<?>> updater) {
    final TypedOptic<A, ?, FT, FR> field = optic.findType(type, newType, true).orThrow();
    return updateCap(field, ft -> {
        final Typed<?> newValue = updater.apply(new Typed<>(optic.type(), ops, ft));
        return field.bType().ifSame(newValue).orElseThrow(() -> new IllegalArgumentException("Function didn't update to the expected type"));
    });
}
 
Example #14
Source File: TypedOptic.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public static <F, G, G2> TypedOptic<Pair<F, G>, Pair<F, G2>, G, G2> proj2(final Type<F> fType, final Type<G> gType, final Type<G2> newType) {
    return new TypedOptic<>(
        Cartesian.Mu.TYPE_TOKEN,
        DSL.and(fType, gType),
        DSL.and(fType, newType),
        gType,
        newType,
        new Proj2<>()
    );
}
 
Example #15
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<? extends PointFree<A>> rewrite(final Type<A> type, final PointFree<A> expr) {
    if (expr instanceof Apply<?, ?>) {
        final Apply<?, ?> applyFirst = (Apply<?, ?>) expr;
        if (applyFirst.arg instanceof Apply<?, ?>) {
            final Apply<?, ?> applySecond = (Apply<?, ?>) applyFirst.arg;
            return cap(applyFirst, applySecond);
        }
    }
    return Optional.empty();
}
 
Example #16
Source File: Const.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public TypeFamily apply(final TypeFamily family) {
    return new TypeFamily() {
        @Override
        public Type<?> apply(final int index) {
            return type;
        }

        /*@Override
        public <A, B> Either<Type.FieldOptic<?, ?, A, B>, Type.FieldNotFoundException> findField(final int index, final String name, final Type<A> aType, final Type<B> bType) {
            return type.findField(name, aType, bType, false).mapLeft(o -> o);
        }*/
    };
}
 
Example #17
Source File: TypeRewriteRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<RewriteResult<A, ?>> rewrite(final Type<A> type) {
    RewriteResult<A, ?> result = RewriteResult.nop(type);
    for (final TypeRewriteRule rule : rules) {
        final Optional<RewriteResult<A, ?>> newResult = cap1(rule, result);
        if (!newResult.isPresent()) {
            return Optional.empty();
        }
        result = newResult.get();
    }
    return Optional.of(result);
}
 
Example #18
Source File: Tag.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public TypeFamily apply(final TypeFamily family) {
    return new TypeFamily() {
        @Override
        public Type<?> apply(final int index) {
            return DSL.field(name, element.apply(family).apply(index));
        }

        /*@Override
        public <A, B> Either<Type.FieldOptic<?, ?, A, B>, Type.FieldNotFoundException> findField(final int index, final String name, final Type<A> aType, final Type<B> bType) {
            if (!Objects.equals(name, NameTag.this.name)) {
                return Either.right(new Type.FieldNotFoundException("Names don't match"));
            }
            if (element instanceof Const) {
                final Const c = (Const) element;
                if (Objects.equals(aType, c.type)) {
                    final Type.FieldOptic<A, B, A, B> optic = new Type.FieldOptic<>(
                        Lenses.Profunctor.Mu.TYPE_TOKEN,
                        Type.tag(name, aType), // this.apply(family).apply(index)
                        Type.tag(name, bType), // newCode.apply(family).apply(index)
                        aType,
                        bType,
                        Lenses.id()
                    );
                    return Either.left(optic);
                }
                return Either.right(new Type.FieldNotFoundException("don't match"));
            }
            return Either.right(new Type.FieldNotFoundException("Recursive field"));
        }*/
    };
}
 
Example #19
Source File: TypeRewriteRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<RewriteResult<A, ?>> rewrite(final Type<A> type) {
    final Optional<RewriteResult<A, ?>> result = rule.rewrite(type);
    if (!result.isPresent() || Objects.equals(result.get().view.function(), Functions.id())) {
        onFail.accept(type);
    }
    return result;
}
 
Example #20
Source File: Tag.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public Optional<Type<?>> findFieldTypeOpt(final String name) {
    if (Objects.equals(name, this.name)) {
        return Optional.of(element);
    }
    return Optional.empty();
}
 
Example #21
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 #22
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<? extends PointFree<A>> rewrite(final Type<A> type, final PointFree<A> expr) {
    final Optional<? extends PointFree<A>> view = first.rewrite(type, expr);
    if (view.isPresent()) {
        return view;
    }
    return second.get().rewrite(type, expr);
}
 
Example #23
Source File: MixinEntityTypeBuilder.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Fixes MC-170128: Cannot build an EntityType without a datafixer due to an IllegalArgumentException.
 */
@Redirect(method = "build", at = @At(value = "INVOKE", target = "Lcom/mojang/datafixers/schemas/Schema;getChoiceType(Lcom/mojang/datafixers/DSL$TypeReference;Ljava/lang/String;)Lcom/mojang/datafixers/types/Type;", remap = false))
public Type catchIllegalArgumentExceptionForDataFixers(Schema schema, DSL.TypeReference type, String choiceName) {
	try {
		return schema.getChoiceType(type, choiceName);
	} catch (IllegalArgumentException ex) {
		LOGGER.warn("No data fixer registered for entity {}", choiceName);
	}

	// This return result is ignored
	return null;
}
 
Example #24
Source File: Check.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@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 (index != expectedIndex) {
        return Either.right(new FieldNotFoundException("Incorrect index in CheckType"));
    }
    return delegate.findType(type, resultType, matcher, recurse).mapLeft(optic -> wrapOptic(this, optic));
}
 
Example #25
Source File: TypedOptic.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public TypedOptic(final Set<TypeToken<? extends K1>> proofBounds, final Type<S> sType, final Type<T> tType, final Type<A> aType, final Type<B> bType, final Optic<?, S, T, A, B> optic) {
    this.proofBounds = proofBounds;
    this.sType = sType;
    this.tType = tType;
    this.aType = aType;
    this.bType = bType;
    this.optic = optic;
}
 
Example #26
Source File: PointFreeRule.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A> Optional<? extends PointFree<A>> rewrite(final Type<A> type, final PointFree<A> expr) {
    if (expr instanceof Comp<?, ?, ?>) {
        final Comp<?, ?, ?> comp2 = (Comp<?, ?, ?>) expr;
        final PointFree<? extends Function<?, ?>> second = comp2.second;
        if (second instanceof Comp<?, ?, ?>) {
            final Comp<?, ?, ?> comp1 = (Comp<?, ?, ?>) second;
            return swap(comp1, comp2);
        }
    }
    return Optional.empty();
}
 
Example #27
Source File: DSL.java    From DataFixerUpper with MIT License 4 votes vote down vote up
static <FT> OpticFinder<FT> namedChoice(final String name, final Type<FT> type) {
    return new NamedChoiceFinder<>(name, type);
}
 
Example #28
Source File: RecursivePoint.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public Optional<Type<?>> findCheckedType(final int index) {
    return unfold().findCheckedType(this.index);
}
 
Example #29
Source File: Sum.java    From DataFixerUpper with MIT License 4 votes vote down vote up
private static <F, G, G2> RewriteResult<Either<F, G>, Either<F, G2>> fixRight(final Type<Either<F, G>> type, final Type<F> first, final Type<G> second, final RewriteResult<G, G2> view) {
    return opticView(type, view, TypedOptic.inj2(first, second, view.view().newType()));
}
 
Example #30
Source File: List.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public Type<?> updateMu(final RecursiveTypeFamily newFamily) {
    return DSL.list(element.updateMu(newFamily));
}