aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigFieldImpl.java
blob: aeed5acfd2027b26b654f2f41345ae20649eb301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package dev.isxander.yacl3.config.v2.impl;

import dev.isxander.yacl3.config.v2.api.*;
import dev.isxander.yacl3.config.v2.api.autogen.AutoGen;
import dev.isxander.yacl3.config.v2.api.autogen.AutoGenField;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public class ConfigFieldImpl<T> implements ConfigField<T> {
    private ReflectionFieldAccess<T> field;
    private final ReflectionFieldAccess<T> defaultField;
    private final ConfigClassHandler<?> parent;
    private final Optional<SerialField> serial;
    private final Optional<AutoGenField> autoGen;

    public ConfigFieldImpl(ReflectionFieldAccess<T> field, ReflectionFieldAccess<T> defaultField, ConfigClassHandler<?> parent, @Nullable SerialEntry config, @Nullable AutoGen autoGen) {
        this.field = field;
        this.defaultField = defaultField;
        this.parent = parent;

        this.serial = config != null
                ? Optional.of(
                new SerialFieldImpl(
                        "".equals(config.value()) ? field.name() : config.value(),
                        "".equals(config.comment()) ? Optional.empty() : Optional.of(config.comment()),
                        config.required(),
                        config.nullable()
                )
        )
                : Optional.empty();
        this.autoGen = autoGen != null
                ? Optional.of(
                new AutoGenFieldImpl<>(
                        autoGen.category(),
                        "".equals(autoGen.group()) ? Optional.empty() : Optional.of(autoGen.group())
                )
        )
                : Optional.empty();
    }

    @Override
    public ReflectionFieldAccess<T> access() {
        return field;
    }

    public void setFieldAccess(ReflectionFieldAccess<T> field) {
        this.field = field;
    }

    @Override
    public ReflectionFieldAccess<T> defaultAccess() {
        return defaultField;
    }

    @Override
    public ConfigClassHandler<?> parent() {
        return parent;
    }

    @Override
    public Optional<SerialField> serial() {
        return this.serial;
    }

    @Override
    public Optional<AutoGenField> autoGen() {
        return this.autoGen;
    }

    private record SerialFieldImpl(String serialName, Optional<String> comment, boolean required, boolean nullable) implements SerialField {
    }
    private record AutoGenFieldImpl<T>(String category, Optional<String> group) implements AutoGenField {
    }
}