All Downloads are FREE. Search and download functionalities are using the official Maven repository.

personthecat.catlib.serialization.codec.DynamicField Maven / Gradle / Ivy

Go to download

Utilities for serialization, commands, noise generation, IO, and some new data types.

The newest version!
package personthecat.catlib.serialization.codec;

import com.mojang.serialization.Codec;
import org.jetbrains.annotations.Nullable;

import java.util.function.BiConsumer;
import java.util.function.Function;

@SuppressWarnings("unused")
public class DynamicField {
    final @Nullable Codec codec;
    final String key;
    final Function getter;
    final BiConsumer setter;
    final Type type;

    public DynamicField(final @Nullable Codec codec, final String key, final Function getter, final BiConsumer setter) {
        this(codec, key, getter, setter, Type.IGNORE_NULL);
    }

    public DynamicField(final @Nullable Codec codec, final String key, final Function getter, final BiConsumer setter, final Type type) {
        this.codec = codec;
        this.key = key;
        this.getter = getter;
        this.setter = setter;
        this.type = type;
        if (codec == null && type == Type.IMPLICIT) throw new IllegalArgumentException("Implicit fields cannot be recursive");
    }

    public static  DynamicField field(final Codec type, final String name, final Function getter, final BiConsumer setter) {
        return new DynamicField<>(type, name, getter, setter);
    }

    public static  DynamicField extend(final Codec type, final String name, final Function getter, final BiConsumer setter) {
        return new DynamicField<>(type, name, getter, setter, Type.IMPLICIT);
    }

    public static  DynamicField nullable(final Codec type, final String name, final Function getter, final BiConsumer setter) {
        return new DynamicField<>(type, name, getter, setter, Type.NULLABLE);
    }

    public static  DynamicField required(final Codec type, final String name, final Function getter, final BiConsumer setter) {
        return new DynamicField<>(type, name, getter, setter, Type.NONNULL);
    }

    public static  DynamicField recursive(final String name, final Function getter, final BiConsumer setter) {
        return new DynamicField<>(null, name, getter, setter);
    }

    public @Nullable Codec type() {
        return this.codec;
    }

    public String key() {
        return this.key;
    }

    public Function getter() {
        return this.getter;
    }

    public BiConsumer setter() {
        return this.setter;
    }

    public boolean isImplicit() {
        return this.type == Type.IMPLICIT;
    }

    public boolean isNullable() {
        return this.type == Type.NULLABLE;
    }

    public boolean isRequired() {
        return this.type == Type.NONNULL;
    }

    public enum Type {
        NONNULL,
        NULLABLE,
        IGNORE_NULL,
        IMPLICIT
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy