Java源码示例:com.mojang.datafixers.types.Type

示例1
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();
    });
}
 
示例2
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)
    );
}
 
示例3
/**
 * 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;
}
 
示例4
@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()));
        }
    );
}
 
示例5
@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());
}
 
示例6
@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();
}
 
示例7
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"));
    });
}
 
示例8
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"));
    });
}
 
示例9
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<>()
    );
}
 
示例10
@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);
        }*/
    };
}
 
示例11
@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);
}
 
示例12
@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;
}
 
示例13
@Override
public Optional<Type<?>> findFieldTypeOpt(final String name) {
    if (Objects.equals(name, this.name)) {
        return Optional.of(element);
    }
    return Optional.empty();
}
 
示例14
@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);
}
 
示例15
@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));
}
 
示例16
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;
}
 
示例17
@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();
}
 
示例18
@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);
}
 
示例19
@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"));
        }*/
    };
}
 
示例20
@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();
}
 
示例21
@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();
}
 
示例22
@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();
}
 
示例23
@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();
}
 
示例24
@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;
}
 
示例25
@Override
public Optional<Type<?>> findFieldTypeOpt(final String name) {
    if (index == expectedIndex) {
        return delegate.findFieldTypeOpt(name);
    }
    return Optional.empty();
}
 
示例26
@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);
}
 
示例27
@Override
public Type<?> updateMu(final RecursiveTypeFamily newFamily) {
    return DSL.taggedChoiceType(name, keyType, types.entrySet().stream().map(e -> Pair.of(e.getKey(), e.getValue().updateMu(newFamily))).collect(Pair.toMap()));
}
 
示例28
@Override
public Optional<? extends PointFree<Function<A, C>>> one(final PointFreeRule rule, final Type<Function<A, C>> type) {
    final Func<A, C> funcType = (Func<A, C>) type;
    return rule.rewrite(DSL.func(middleType, funcType.second()), first).map(f -> Optional.of(Functions.comp(middleType, f, second)))
        .orElseGet(() -> rule.rewrite(DSL.func(funcType.first(), middleType), second).map(s -> Functions.comp(middleType, first, s)));
}
 
示例29
protected TypeRewriteRule writeAndRead(final String name, final Type<?> type, final Type<?> newType) {
    return writeFixAndRead(name, type, newType, Function.identity());
}
 
示例30
static <A> List.ListType<A> list(final Type<A> first) {
    return new List.ListType<>(first);
}