Make defaultValue a supplier and fix some checkstyle in ConfigNode

This commit is contained in:
Ben Woo 2023-09-20 13:45:14 +08:00
parent e4bff69372
commit 6611ec4457
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
1 changed files with 47 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package org.mvplugins.multiverse.core.configuration.node;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import io.vavr.control.Option;
import io.vavr.control.Try;
@ -10,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
/**
* A node that contains a value.
*
* @param <T> The type of the value.
*/
public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
@ -19,19 +21,18 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
*
* @param path The path of the node.
* @param type The type of the value.
* @return The new builder.
* @param <T> The type of the value.
* @return The new builder.
*/
public static @NotNull <T> ConfigNode.Builder<T, ? extends ConfigNode.Builder<T, ?>> builder(
@NotNull String path,
@NotNull Class<T> type
) {
@NotNull Class<T> type) {
return new ConfigNode.Builder<>(path, type);
}
protected final @Nullable String name;
protected final @NotNull Class<T> type;
protected final @Nullable T defaultValue;
protected final @Nullable Supplier<T> defaultValue;
protected final @Nullable NodeSerializer<T> serializer;
protected final @Nullable Function<T, Try<Void>> validator;
protected final @Nullable BiConsumer<T, T> onSetValue;
@ -41,11 +42,10 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
@NotNull String[] comments,
@Nullable String name,
@NotNull Class<T> type,
@Nullable T defaultValue,
@Nullable Supplier<T> defaultValue,
@Nullable NodeSerializer<T> serializer,
@Nullable Function<T, Try<Void>> validator,
@Nullable BiConsumer<T, T> onSetValue
) {
@Nullable BiConsumer<T, T> onSetValue) {
super(path, comments);
this.name = name;
this.type = type;
@ -76,7 +76,10 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
*/
@Override
public @Nullable T getDefaultValue() {
return defaultValue;
if (defaultValue != null) {
return defaultValue.get();
}
return null;
}
public @Nullable NodeSerializer<T> getSerializer() {
@ -115,7 +118,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
protected @Nullable String name;
protected @NotNull final Class<T> type;
protected @Nullable T defaultValue;
protected @Nullable Supplier<T> defaultValue;
protected @Nullable NodeSerializer<T> serializer;
protected @Nullable Function<T, Try<Void>> validator;
protected @Nullable BiConsumer<T, T> onSetValue;
@ -142,8 +145,19 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* @return This builder.
*/
public @NotNull B defaultValue(@NotNull T defaultValue) {
this.defaultValue = () -> defaultValue;
return self();
}
/**
* Sets the default value for this node.
*
* @param defaultValue The default value supplier.
* @return This builder.
*/
public @NotNull B defaultValue(@NotNull Supplier<T> defaultValue) {
this.defaultValue = defaultValue;
return (B) this;
return self();
}
/**
@ -154,17 +168,29 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
*/
public @NotNull B name(@Nullable String name) {
this.name = name;
return (B) this;
return self();
}
/**
* Sets the serializer for this node.
*
* @param serializer The serializer for this node.
* @return This builder.
*/
public @NotNull B serializer(@NotNull NodeSerializer<T> serializer) {
this.serializer = serializer;
return (B) this;
return self();
}
/**
* Sets the validator for this node.
*
* @param validator The validator for this node.
* @return This builder.
*/
public @NotNull B validator(@NotNull Function<T, Try<Void>> validator) {
this.validator = validator;
return (B) this;
return self();
}
/**
@ -175,7 +201,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
*/
public @NotNull B onSetValue(@NotNull BiConsumer<T, T> onSetValue) {
this.onSetValue = onSetValue;
return (B) this;
return self();
}
/**
@ -183,7 +209,13 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
*/
@Override
public @NotNull ConfigNode<T> build() {
return new ConfigNode<>(path, comments.toArray(new String[0]), name, type, defaultValue, serializer, validator, onSetValue);
return new ConfigNode<>(path, comments.toArray(new String[0]),
name, type, defaultValue,serializer, validator, onSetValue);
}
protected @NotNull B self() {
//noinspection unchecked
return (B) this;
}
}
}