Use Option instead of Optional for ValueNode#getName.

Also clean up nullability annotations in ConfigNode and co.
This commit is contained in:
Jeremy Wood 2023-03-28 16:38:35 -04:00
parent 63078270bf
commit 0cc3f93e0b
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
4 changed files with 52 additions and 40 deletions

View File

@ -1,6 +1,7 @@
package com.onarandombox.MultiverseCore.configuration.node;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Strings;
import org.jetbrains.annotations.NotNull;
@ -16,14 +17,14 @@ public class ConfigHeaderNode implements CommentedNode {
* @param path The path of the node.
* @return The new builder.
*/
public static Builder<? extends Builder> builder(String path) {
public static @NotNull Builder<? extends Builder<?>> builder(String path) {
return new Builder<>(path);
}
private final String path;
private final String[] comments;
private final @NotNull String path;
private final @NotNull String[] comments;
protected ConfigHeaderNode(String path, String[] comments) {
protected ConfigHeaderNode(@NotNull String path, @NotNull String[] comments) {
this.path = path;
this.comments = comments;
}
@ -40,16 +41,16 @@ public class ConfigHeaderNode implements CommentedNode {
* {@inheritDoc}
*/
@Override
public String[] getComments() {
public @NotNull String[] getComments() {
return comments;
}
public static class Builder<B extends Builder<B>> {
protected final String path;
protected final ArrayList<String> comments;
protected final @NotNull String path;
protected final @NotNull List<String> comments;
public Builder(String path) {
public Builder(@NotNull String path) {
this.path = path;
this.comments = new ArrayList<>();
}
@ -60,7 +61,7 @@ public class ConfigHeaderNode implements CommentedNode {
* @param comment The comment to add.
* @return This builder.
*/
public B comment(String comment) {
public @NotNull B comment(@NotNull String comment) {
if (!Strings.isNullOrEmpty(comment) && !comment.startsWith("#")) {
comment = "# " + comment;
}
@ -73,7 +74,7 @@ public class ConfigHeaderNode implements CommentedNode {
*
* @return The built node.
*/
public ConfigHeaderNode build() {
public @NotNull ConfigHeaderNode build() {
return new ConfigHeaderNode(path, comments.toArray(new String[0]));
}
}

View File

@ -1,9 +1,9 @@
package com.onarandombox.MultiverseCore.configuration.node;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import io.vavr.control.Option;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -21,17 +21,28 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* @return The new builder.
* @param <T> The type of the value.
*/
public static <T> ConfigNode.Builder<T, ? extends ConfigNode.Builder> builder(String path, Class<T> type) {
public static @NotNull <T> ConfigNode.Builder<T, ? extends ConfigNode.Builder<T, ?>> builder(
@NotNull String path,
@NotNull Class<T> type
) {
return new ConfigNode.Builder<>(path, type);
}
protected final String name;
protected final Class<T> type;
protected final T defaultValue;
protected final Function<T, Boolean> validator;
protected final BiConsumer<T, T> onSetValue;
protected final @Nullable String name;
protected final @NotNull Class<T> type;
protected final @Nullable T defaultValue;
protected final @Nullable Function<T, Boolean> validator;
protected final @Nullable BiConsumer<T, T> onSetValue;
protected ConfigNode(String path, String[] comments, String name, Class<T> type, T defaultValue, Function<T, Boolean> validator, BiConsumer<T, T> onSetValue) {
protected ConfigNode(
@NotNull String path,
@NotNull String[] comments,
@Nullable String name,
@NotNull Class<T> type,
@Nullable T defaultValue,
@Nullable Function<T, Boolean> validator,
@Nullable BiConsumer<T, T> onSetValue
) {
super(path, comments);
this.name = name;
this.type = type;
@ -44,8 +55,8 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* {@inheritDoc}
*/
@Override
public Optional<String> getName() {
return Optional.ofNullable(name);
public @NotNull Option<String> getName() {
return Option.of(name);
}
/**
@ -68,7 +79,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* {@inheritDoc}
*/
@Override
public boolean validate(T value) {
public boolean validate(@Nullable T value) {
if (validator != null) {
return validator.apply(value);
}
@ -79,7 +90,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* {@inheritDoc}
*/
@Override
public void onSetValue(T oldValue, T newValue) {
public void onSetValue(@Nullable T oldValue, @Nullable T newValue) {
if (onSetValue != null) {
onSetValue.accept(oldValue, newValue);
}
@ -93,11 +104,11 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
*/
public static class Builder<T, B extends ConfigNode.Builder<T, B>> extends ConfigHeaderNode.Builder<B> {
protected String name;
protected final Class<T> type;
protected T defaultValue;
protected Function<T, Boolean> validator;
protected BiConsumer<T, T> onSetValue;
protected @Nullable String name;
protected @NotNull final Class<T> type;
protected @Nullable T defaultValue;
protected @Nullable Function<T, Boolean> validator;
protected @Nullable BiConsumer<T, T> onSetValue;
/**
* Creates a new builder.
@ -117,7 +128,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* @param defaultValue The default value.
* @return This builder.
*/
public B defaultValue(@NotNull T defaultValue) {
public @NotNull B defaultValue(@NotNull T defaultValue) {
this.defaultValue = defaultValue;
return (B) this;
}
@ -128,12 +139,12 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* @param name The name of this node.
* @return This builder.
*/
public B name(@Nullable String name) {
public @NotNull B name(@Nullable String name) {
this.name = name;
return (B) this;
}
public B validator(@Nullable Function<T, Boolean> validator) {
public @NotNull B validator(@NotNull Function<T, Boolean> validator) {
this.validator = validator;
return (B) this;
}
@ -144,7 +155,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* @param onSetValue The action to be performed.
* @return This builder.
*/
public B onSetValue(@Nullable BiConsumer<T, T> onSetValue) {
public @NotNull B onSetValue(@NotNull BiConsumer<T, T> onSetValue) {
this.onSetValue = onSetValue;
return (B) this;
}
@ -153,7 +164,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
* {@inheritDoc}
*/
@Override
public ConfigNode<T> build() {
public @NotNull ConfigNode<T> build() {
return new ConfigNode<>(path, comments.toArray(new String[0]), name, type, defaultValue, validator, onSetValue);
}
}

View File

@ -30,13 +30,13 @@ public class NodeGroup implements Collection<Node> {
private void addNodeIndex(Node node) {
if (node instanceof ValueNode) {
((ValueNode<?>) node).getName().ifPresent(name -> nodesMap.put(name, node));
((ValueNode<?>) node).getName().peek(name -> nodesMap.put(name, node));
}
}
private void removeNodeIndex(Node node) {
if (node instanceof ValueNode) {
((ValueNode<?>) node).getName().ifPresent(nodesMap::remove);
((ValueNode<?>) node).getName().peek(nodesMap::remove);
}
}

View File

@ -1,17 +1,17 @@
package com.onarandombox.MultiverseCore.configuration.node;
import java.util.Optional;
import io.vavr.control.Option;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface ValueNode<T> extends Node {
/**
* Gets the name of this node. Used for identifying the node from user input.
*
* @return The name of this node.
* @return An {@link Option} containing the name of this node, or {@link Option.None} if the node has no name.
*/
Optional<String> getName();
@NotNull Option<String> getName();
/**
* Gets the class type {@link T} of the node value.
@ -33,7 +33,7 @@ public interface ValueNode<T> extends Node {
* @param value The value to validate.
* @return True if the value is valid, false otherwise.
*/
boolean validate(T value);
boolean validate(@Nullable T value);
/**
* Called when the value of this node is set.
@ -41,5 +41,5 @@ public interface ValueNode<T> extends Node {
* @param oldValue The old value.
* @param newValue The new value.
*/
void onSetValue(T oldValue, T newValue);
void onSetValue(@Nullable T oldValue, @Nullable T newValue);
}