Cleanup NodeGroup with docs and annotation

This commit is contained in:
Ben Woo 2023-09-20 12:31:34 +08:00
parent bbfc1a05e8
commit cce041877f
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
1 changed files with 18 additions and 12 deletions

View File

@ -9,6 +9,7 @@ import java.util.Map;
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
import io.vavr.control.Option;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A collection of {@link CommentedNode}s, with mappings to nodes by name.
@ -17,24 +18,32 @@ public class NodeGroup implements Collection<Node> {
private final Collection<Node> nodes;
private final Map<String, Node> nodesMap;
/**
* Creates a new empty node group.
*/
public NodeGroup() {
this.nodes = new ArrayList<>();
this.nodesMap = new HashMap<>();
}
public NodeGroup(Collection<Node> nodes) {
/**
* Creates a new node group with the given nodes.
*
* @param nodes The nodes to make up the group.
*/
public NodeGroup(@NotNull Collection<Node> nodes) {
this.nodes = nodes;
this.nodesMap = new HashMap<>(nodes.size());
nodes.forEach(this::addNodeIndex);
}
private void addNodeIndex(Node node) {
private void addNodeIndex(@NotNull Node node) {
if (node instanceof ValueNode) {
((ValueNode<?>) node).getName().peek(name -> nodesMap.put(name, node));
}
}
private void removeNodeIndex(Node node) {
private void removeNodeIndex(@NotNull Node node) {
if (node instanceof ValueNode) {
((ValueNode<?>) node).getName().peek(nodesMap::remove);
}
@ -45,32 +54,29 @@ public class NodeGroup implements Collection<Node> {
*
* @return The names of all nodes in this group.
*/
public Collection<String> getNames() {
public @NotNull Collection<String> getNames() {
return nodesMap.keySet();
}
public Map<String, Node> getNodesMap() {
return nodesMap;
}
/**
* Gets the node with the given name.
*
* @param name The name of the node to get.
* @return The node with the given name, or {@link Option.None} if no node with the given name exists.
*/
public Option<Node> findNode(String name) {
public @NotNull Option<Node> findNode(@Nullable String name) {
return Option.of(nodesMap.get(name));
}
/**
* Gets the node with the given name.
*
* @param name The name of the node to get.
* @param type The type of the node to get.
* @param name The name of node to get.
* @param type The type of node to get.
* @param <T> The type of node.
* @return The node with the given name, or {@link Option.None} if no node with the given name exists.
*/
public <T extends Node> Option<T> findNode(String name, Class<T> type) {
public <T extends Node> @NotNull Option<T> findNode(@Nullable String name, @NotNull Class<T> type) {
return Option.of(nodesMap.get(name)).map(node -> type.isAssignableFrom(node.getClass()) ? (T) node : null);
}