docs: Add docstrings to methods implemented

This commit is contained in:
Ben Woo 2023-03-24 22:09:10 +08:00
parent d126b3d31a
commit cb5877b206
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
10 changed files with 205 additions and 13 deletions

View File

@ -3,7 +3,6 @@ package com.onarandombox.MultiverseCore.utils.settings;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.logging.Logger;
import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator;
@ -16,6 +15,9 @@ import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A class that makes use of CommentedConfiguration to provide a simple way to load and save with node objects.
*/
public class MVSettings {
public static Builder builder(String configPath) {
@ -122,6 +124,11 @@ public class MVSettings {
config.save();
}
/**
* Checks if the configuration is loaded.
*
* @return True if the configuration is loaded, false otherwise.
*/
public boolean isLoaded() {
return config != null;
}
@ -224,6 +231,9 @@ public class MVSettings {
return config;
}
/**
* Gets the path of the configuration file.
*/
public static class Builder {
private final Path configPath;
@ -232,33 +242,72 @@ public class MVSettings {
private ConfigMigrator migrator;
/**
* Creates a new builder.
*
* @param configPath The path of the configuration file.
*/
public Builder(String configPath) {
this.configPath = Path.of(configPath);
}
/**
* Creates a new builder.
*
* @param configPath The path of the configuration file.
*/
public Builder(Path configPath) {
this.configPath = configPath;
}
/**
* Sets the logger to use.
*
* @param plugin The plugin to get the logger from.
* @return The builder.
*/
public Builder logger(@NotNull Plugin plugin) {
return logger(plugin.getLogger());
}
/**
* Sets the logger to use.
*
* @param logger The logger to use.
* @return The builder.
*/
public Builder logger(@Nullable Logger logger) {
this.logger = logger;
return this;
}
/**
* Sets the nodes to use.
*
* @param nodes The nodes to use.
* @return The builder.
*/
public Builder nodes(@Nullable NodeGroup nodes) {
this.nodes = nodes;
return this;
}
/**
* Sets the migrator to use.
*
* @param migrator The migrator to use.
* @return The builder.
*/
public Builder migrator(@Nullable ConfigMigrator migrator) {
this.migrator = migrator;
return this;
}
/**
* Builds the settings.
*
* @return The built settings.
*/
public MVSettings build() {
return new MVSettings(configPath, logger, nodes, migrator);
}

View File

@ -7,7 +7,18 @@ import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.utils.settings.MVSettings;
import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode;
/**
* Helper class for migrating configs to the latest config version.
*/
public class ConfigMigrator {
/**
* Creates a new builder for a ConfigMigrator.
*
* @param versionNode The node that stores the version number of the config.
* Default value should be the current latest version number.
* @return The builder instance.
*/
public static Builder builder(TypedValueNode<Double> versionNode) {
return new Builder(versionNode);
}
@ -15,11 +26,16 @@ public class ConfigMigrator {
private final TypedValueNode<Double> versionNode;
private final List<VersionMigrator> versionMigrators;
public ConfigMigrator(TypedValueNode<Double> versionNode, List<VersionMigrator> versionMigrators) {
protected ConfigMigrator(TypedValueNode<Double> versionNode, List<VersionMigrator> versionMigrators) {
this.versionNode = versionNode;
this.versionMigrators = versionMigrators;
}
/**
* Migrates the config to the latest version if necessary.
*
* @param settings The target settings instance to migrate.
*/
public void migrate(MVSettings settings) {
double versionNumber = settings.get(versionNode);
for (VersionMigrator versionMigrator : versionMigrators) {
@ -28,10 +44,13 @@ public class ConfigMigrator {
versionMigrator.migrate(settings);
}
}
// Set the version number to the latest version number
settings.setDefault(versionNode);
}
/**
* A builder for a ConfigMigrator.
*/
public static class Builder {
private final TypedValueNode<Double> versionNode;
private final List<VersionMigrator> versionMigrators;
@ -47,11 +66,22 @@ public class ConfigMigrator {
this.versionMigrators = new ArrayList<>();
}
/**
* Adds a version migrator to the list of migrators.
*
* @param versionMigrator The migrator to add.
* @return The builder instance.
*/
public Builder addVersionMigrator(VersionMigrator versionMigrator) {
versionMigrators.add(versionMigrator);
return this;
}
/**
* Builds the ConfigMigrator.
*
* @return The built ConfigMigrator.
*/
public ConfigMigrator build() {
return new ConfigMigrator(versionNode, versionMigrators);
}

View File

@ -2,17 +2,30 @@ package com.onarandombox.MultiverseCore.utils.settings.migration;
import com.onarandombox.MultiverseCore.utils.settings.MVSettings;
/**
* Single migrator action that inverts a boolean value for a given path.
*/
public class InvertBoolMigratorAction implements MigratorAction {
/**
* Creates a new migrator action that inverts a boolean value for a given path.
*
* @param path The path to invert value of.
* @return The new migrator action.
*/
public static InvertBoolMigratorAction of(String path) {
return new InvertBoolMigratorAction(path);
}
private final String path;
public InvertBoolMigratorAction(String path) {
protected InvertBoolMigratorAction(String path) {
this.path = path;
}
/**
* {@InheritDoc}
*/
@Override
public void migrate(MVSettings settings) {
settings.getConfig().set(path, !settings.getConfig().getBoolean(path));

View File

@ -2,6 +2,15 @@ package com.onarandombox.MultiverseCore.utils.settings.migration;
import com.onarandombox.MultiverseCore.utils.settings.MVSettings;
/**
* A migrator action is a single action that is performed when migrating a config.
*/
public interface MigratorAction {
/**
* Performs the migration action.
*
* @param settings The target settings instance to migrate.
*/
void migrate(MVSettings settings);
}

View File

@ -5,7 +5,18 @@ import java.util.Optional;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.utils.settings.MVSettings;
/**
* Single migrator action that moves a value from one path to another.
*/
public class MoveMigratorAction implements MigratorAction {
/**
* Creates a new migrator action that moves a value from one path to another.
*
* @param fromPath The path to move value from.
* @param toPath The path to move value to.
* @return The new migrator action.
*/
public static MoveMigratorAction of(String fromPath, String toPath) {
return new MoveMigratorAction(fromPath, toPath);
}
@ -13,11 +24,14 @@ public class MoveMigratorAction implements MigratorAction {
private final String fromPath;
private final String toPath;
public MoveMigratorAction(String fromPath, String toPath) {
protected MoveMigratorAction(String fromPath, String toPath) {
this.fromPath = fromPath;
this.toPath = toPath;
}
/**
* {@InheritDoc}
*/
@Override
public void migrate(MVSettings settings) {
Optional.ofNullable(settings.getConfig().get(fromPath))

View File

@ -5,7 +5,17 @@ import java.util.List;
import com.onarandombox.MultiverseCore.utils.settings.MVSettings;
/**
* A version migrator is a collection of migrator actions that are performed when migrating a config to a specific version.
*/
public class VersionMigrator {
/**
* Creates a new builder for a VersionMigrator.
*
* @param version The version number of the config that this migrator migrates to.
* @return The builder instance.
*/
public static Builder builder(double version) {
return new Builder(version);
}
@ -18,27 +28,56 @@ public class VersionMigrator {
this.actions = actions;
}
/**
* Performs all the migrator actions.
*
* @param settings The target settings instance to migrate.
*/
public void migrate(MVSettings settings) {
actions.forEach(action -> action.migrate(settings));
}
/**
* Gets the version number of the config that this migrator migrates to.
*
* @return The version number.
*/
public double getVersion() {
return version;
}
/**
* A builder for a VersionMigrator.
*/
public static class Builder {
private final double version;
private final List<MigratorAction> actions = new ArrayList<>();
/**
* Creates a new builder for a VersionMigrator.
*
* @param version The version number of the config that this migrator migrates to.
*/
public Builder(double version) {
this.version = version;
}
/**
* Adds a migrator action to the list of actions.
*
* @param action The action to add.
* @return The builder instance.
*/
public Builder addAction(MigratorAction action) {
actions.add(action);
return this;
}
/**
* Builds the VersionMigrator.
*
* @return The built VersionMigrator.
*/
public VersionMigrator build() {
return new VersionMigrator(version, actions);
}

View File

@ -6,6 +6,9 @@ import java.util.List;
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
import org.jetbrains.annotations.NotNull;
/**
* Implementation of {@link CommentedNode} that allows for comments to be added to the node.
*/
public class MVCommentedNode implements CommentedNode {
public static <T> Builder<Builder> builder(String path) {
@ -20,11 +23,17 @@ public class MVCommentedNode implements CommentedNode {
this.comments = comments;
}
/**
* {@InheritDoc}
*/
@Override
public @NotNull String getPath() {
return path;
}
/**
* {@InheritDoc}
*/
@Override
public @NotNull String[] getComments() {
return comments;

View File

@ -3,6 +3,10 @@ package com.onarandombox.MultiverseCore.utils.settings.node;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Implementation of {@link NamedValueNode}.
* @param <T> The type of the value.
*/
public class MVValueNode<T> extends MVCommentedNode implements NamedValueNode<T> {
public static <T> Builder<T, ? extends Builder> builder(String path, Class<T> type) {
@ -20,18 +24,27 @@ public class MVValueNode<T> extends MVCommentedNode implements NamedValueNode<T>
this.name = name;
}
/**
* {@InheritDoc}
*/
@Override
public @NotNull Class<T> getType() {
return type;
}
/**
* {@InheritDoc}
*/
@Override
public @Nullable T getDefaultValue() {
return defaultValue;
}
/**
* {@InheritDoc}
*/
@Override
public String getName() {
public @Nullable String getName() {
return name;
}

View File

@ -1,7 +1,13 @@
package com.onarandombox.MultiverseCore.utils.settings.node;
import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode;
import org.jetbrains.annotations.Nullable;
public interface NamedValueNode<T> extends TypedValueNode<T> {
String getName();
/**
* Gets the name of this node. Used for identifying the node from user input.
*
* @return The name of this node.
*/
@Nullable String getName();
}

View File

@ -6,12 +6,13 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import com.dumptruckman.minecraft.util.Logging;
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
import org.jetbrains.annotations.NotNull;
/**
* A collection of {@link CommentedNode}s, with mappings to nodes by name.
*/
public class NodeGroup implements Collection<CommentedNode> {
private final Collection<CommentedNode> nodes;
private final Map<String, CommentedNode> nodesMap;
@ -39,10 +40,21 @@ public class NodeGroup implements Collection<CommentedNode> {
}
}
/**
* Gets the names of all nodes in this group.
*
* @return The names of all nodes in this group.
*/
public Collection<String> getNames() {
return nodesMap.keySet();
}
/**
* 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 Optional#empty()} if no node with the given name exists.
*/
public Optional<CommentedNode> findNode(String name) {
return Optional.ofNullable(nodesMap.get(name));
}
@ -68,15 +80,13 @@ public class NodeGroup implements Collection<CommentedNode> {
return nodes.iterator();
}
@NotNull
@Override
public Object[] toArray() {
public Object @NotNull [] toArray() {
return nodes.toArray();
}
@NotNull
@Override
public <T> T[] toArray(@NotNull T[] ts) {
public <T> T @NotNull [] toArray(@NotNull T[] ts) {
return nodes.toArray(ts);
}