Use Option instead of Optional in NodeGroup.

This commit is contained in:
Jeremy Wood 2023-03-28 16:22:50 -04:00
parent a19fe76cbc
commit 63078270bf
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
3 changed files with 13 additions and 13 deletions

View File

@ -23,6 +23,7 @@ import com.onarandombox.MultiverseCore.display.filters.ContentFilter;
import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter; import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter;
import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter; import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter;
import com.onarandombox.MultiverseCore.utils.PlayerFinder; import com.onarandombox.MultiverseCore.utils.PlayerFinder;
import io.vavr.control.Option;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import org.bukkit.GameRule; import org.bukkit.GameRule;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -123,7 +124,7 @@ public class MVCommandContexts extends PaperCommandContexts {
if (Strings.isNullOrEmpty(configName)) { if (Strings.isNullOrEmpty(configName)) {
throw new InvalidCommandArgument("No config name specified."); throw new InvalidCommandArgument("No config name specified.");
} }
Optional<Node> node = config.getNodes().findNode(configName); Option<Node> node = config.getNodes().findNode(configName);
if (node.isEmpty()) { if (node.isEmpty()) {
throw new InvalidCommandArgument("The config " + configName + " is not valid."); throw new InvalidCommandArgument("The config " + configName + " is not valid.");
} }

View File

@ -127,8 +127,8 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
*/ */
public Try<Object> get(@Nullable String name) { public Try<Object> get(@Nullable String name) {
return nodes.findNode(name, ValueNode.class) return nodes.findNode(name, ValueNode.class)
.map(node -> Try.of(() -> get(node))) .map(node -> get((ValueNode<Object>) node))
.orElse(Try.failure(new Exception("Node not found"))); .toTry(() -> new Exception("Node not found"));
} }
/** /**
@ -150,8 +150,8 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
*/ */
public Try<Boolean> set(@Nullable String name, Object value) { public Try<Boolean> set(@Nullable String name, Object value) {
return nodes.findNode(name, ValueNode.class) return nodes.findNode(name, ValueNode.class)
.map(node -> (Try<Boolean>) set(node, value)) .toTry(() -> new Exception("Node not found"))
.orElse(Try.failure(new Exception("Node not found"))); .flatMap(node -> set(node, value));
} }
/** /**

View File

@ -5,9 +5,9 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
import io.vavr.control.Option;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -53,10 +53,10 @@ public class NodeGroup implements Collection<Node> {
* Gets the node with the given name. * Gets the node with the given name.
* *
* @param name The name of the node to get. * @param name The name of the node to get.
* @return The node with the given name, or {@link Optional#empty()} if no node with the given name exists. * @return The node with the given name, or {@link Option.None} if no node with the given name exists.
*/ */
public Optional<Node> findNode(String name) { public Option<Node> findNode(String name) {
return Optional.ofNullable(nodesMap.get(name)); return Option.of(nodesMap.get(name));
} }
/** /**
@ -64,11 +64,10 @@ public class NodeGroup implements Collection<Node> {
* *
* @param name The name of the node to get. * @param name The name of the node to get.
* @param type The type of the node to get. * @param type The type of the node to get.
* @return The node with the given name, or {@link Optional#empty()} if no node with the given name exists. * @return The node with the given name, or {@link Option.None} if no node with the given name exists.
*/ */
public <T extends Node> Optional<T> findNode(String name, Class<T> type) { public <T extends Node> Option<T> findNode(String name, Class<T> type) {
return Optional.ofNullable(nodesMap.get(name)) return Option.of(nodesMap.get(name)).map(node -> type.isAssignableFrom(node.getClass()) ? (T) node : null);
.map(node -> type.isAssignableFrom(node.getClass()) ? (T) node : null);
} }
@Override @Override