add javadocs to Command class

This commit is contained in:
Luck 2018-01-15 19:55:21 +00:00
parent 9ceacdb739
commit 8a374aed04
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 108 additions and 38 deletions

View File

@ -34,43 +34,57 @@ import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.locale.LocalizedSpec; import me.lucko.luckperms.common.locale.LocalizedSpec;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate; import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* An abstract command class * An abstract command class
* *
* @param <T> the type required by the {@link #execute(LuckPermsPlugin, Sender, Object, List, String)} method of this command * @param <T> the argument type required by the command
* @param <S> the type of any child commands * @param <S> the type of any child commands
*/ */
public abstract class Command<T, S> { public abstract class Command<T, S> {
/**
* The commands specification.
*
* Contains details about usage, description, etc
*/
@Nonnull
private final LocalizedSpec spec; private final LocalizedSpec spec;
/** /**
* The name of the command. Should be properly capitalised. * The name of the command. Should be properly capitalised.
*/ */
@Nonnull
private final String name; private final String name;
/** /**
* The permission required to use this command. Nullable. * The permission required to use this command. Nullable.
*/ */
@Nullable
private final CommandPermission permission; private final CommandPermission permission;
/** /**
* A predicate used for testing the size of the arguments list passed to this command * A predicate used for testing the size of the arguments list passed to this command
*/ */
private final Predicate<Integer> argumentCheck; @Nonnull
private Predicate<Integer> argumentCheck = Predicates.alwaysFalse();
/** /**
* Child commands. Nullable. * Child commands. Nullable.
*/ */
@Nullable
private final List<Command<S, ?>> children; private final List<Command<S, ?>> children;
public Command(LocalizedSpec spec, String name, CommandPermission permission, Predicate<Integer> argumentCheck, List<Command<S, ?>> children) { public Command(@Nonnull LocalizedSpec spec, @Nonnull String name, @Nullable CommandPermission permission, @Nonnull Predicate<Integer> argumentCheck, @Nullable List<Command<S, ?>> children) {
this.spec = spec; this.spec = spec;
this.name = name; this.name = name;
this.permission = permission; this.permission = permission;
@ -78,32 +92,104 @@ public abstract class Command<T, S> {
this.children = children == null ? null : ImmutableList.copyOf(children); this.children = children == null ? null : ImmutableList.copyOf(children);
} }
public Command(LocalizedSpec spec, String name, CommandPermission permission, Predicate<Integer> argumentCheck) { public Command(@Nonnull LocalizedSpec spec, @Nonnull String name, @Nullable CommandPermission permission, @Nonnull Predicate<Integer> argumentCheck) {
this(spec, name, permission, argumentCheck, null); this(spec, name, permission, argumentCheck, null);
} }
public Command(LocalizedSpec spec, String name, Predicate<Integer> argumentCheck) { public Command(@Nonnull LocalizedSpec spec, @Nonnull String name, @Nonnull Predicate<Integer> argumentCheck) {
this(spec, name, null, argumentCheck, null); this(spec, name, null, argumentCheck, null);
} }
public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, T t, List<String> args, String label) throws CommandException; /**
* Gets the commands spec.
public List<String> tabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) { *
return Collections.emptyList(); * @return the command spec
} */
@Nonnull
public LocalizedSpec getSpec() { public LocalizedSpec getSpec() {
return this.spec; return this.spec;
} }
/**
* Gets the short name of this command
*
* <p>The result should be appropriately capitalised.</p>
*
* @return the command name
*/
@Nonnull
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Gets the permission required by this command, if present
*
* @return the command permission
*/
@Nonnull
public Optional<CommandPermission> getPermission() {
return Optional.ofNullable(this.permission);
}
/**
* Gets the predicate used to validate the number of arguments provided to
* the command on execution
*
* @return the argument checking predicate
*/
@Nonnull
public Predicate<Integer> getArgumentCheck() { public Predicate<Integer> getArgumentCheck() {
return this.argumentCheck; return this.argumentCheck;
} }
/**
* Gets the commands children
*
* @return any child commands
*/
@Nonnull
public Optional<List<Command<S, ?>>> getChildren() {
return Optional.ofNullable(this.children);
}
/**
* Gets the commands description.
*
* @return the description
*/
public String getDescription() {
return getSpec().description();
}
/**
* Gets the usage of this command.
* Will only return a non empty result for main commands.
*
* @return the usage of this command.
*/
public String getUsage() {
String usage = getSpec().usage();
return usage == null ? "" : usage;
}
/**
* Gets the arguments required by this command
*
* @return the commands arguments
*/
public Optional<List<Arg>> getArgs() {
return Optional.ofNullable(getSpec().args());
}
// Main execution method for the command.
public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, T t, List<String> args, String label) throws CommandException;
// Tab completion method - default implementation is provided as some commands do not provide tab completions.
public List<String> tabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) {
return Collections.emptyList();
}
/** /**
* Sends a brief command usage message to the Sender. * Sends a brief command usage message to the Sender.
* If this command has child commands, the children are listed. Otherwise, a basic usage message is sent. * If this command has child commands, the children are listed. Otherwise, a basic usage message is sent.
@ -137,37 +223,12 @@ public abstract class Command<T, S> {
} }
/** /**
* Returns if this command should be displayed in command listings, or "hidden" * Gets if this command should be displayed in command listings, or "hidden"
* *
* @return if this command should be displayed in command listings, or "hidden" * @return if the command should be displayed
*/ */
public boolean shouldDisplay() { public boolean shouldDisplay() {
return true; return true;
} }
public String getDescription() {
return this.spec.description();
}
/**
* Returns the usage of this command. Will only return a non empty result for main commands.
*
* @return the usage of this command.
*/
public String getUsage() {
String usage = this.spec.usage();
return usage == null ? "" : usage;
}
public Optional<CommandPermission> getPermission() {
return Optional.ofNullable(this.permission);
}
public Optional<List<Arg>> getArgs() {
return Optional.ofNullable(this.spec.args());
}
public Optional<List<Command<S, ?>>> getChildren() {
return Optional.ofNullable(this.children);
}
} }

View File

@ -42,6 +42,8 @@ import java.util.Optional;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
public abstract class MainCommand<T, I> extends Command<Void, T> { public abstract class MainCommand<T, I> extends Command<Void, T> {
// equals 1 if the command doesn't take a mid argument, e.g. /lp log sub-command.... // equals 1 if the command doesn't take a mid argument, e.g. /lp log sub-command....
@ -191,6 +193,7 @@ public abstract class MainCommand<T, I> extends Command<Void, T> {
return getChildren().get().stream().anyMatch(sc -> sc.isAuthorized(sender)); return getChildren().get().stream().anyMatch(sc -> sc.isAuthorized(sender));
} }
@Nonnull
@Override @Override
public Optional<List<Command<T, ?>>> getChildren() { public Optional<List<Command<T, ?>>> getChildren() {
return super.getChildren(); return super.getChildren();

View File

@ -45,6 +45,8 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.Nonnull;
public class MigrationMainCommand extends MainCommand<Object, Object> { public class MigrationMainCommand extends MainCommand<Object, Object> {
private static final Map<String, String> PLUGINS = ImmutableBiMap.<String, String>builder() private static final Map<String, String> PLUGINS = ImmutableBiMap.<String, String>builder()
// bukkit // bukkit
@ -69,6 +71,7 @@ public class MigrationMainCommand extends MainCommand<Object, Object> {
super(CommandSpec.MIGRATION.spec(locale), "Migration", 1, null); super(CommandSpec.MIGRATION.spec(locale), "Migration", 1, null);
} }
@Nonnull
@Override @Override
public synchronized Optional<List<Command<Object, ?>>> getChildren() { public synchronized Optional<List<Command<Object, ?>>> getChildren() {
if (this.commands == null) { if (this.commands == null) {

View File

@ -53,6 +53,8 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
public class SpongeMainCommand extends Command<Void, LPSubjectData> { public class SpongeMainCommand extends Command<Void, LPSubjectData> {
private final LPSpongePlugin plugin; private final LPSpongePlugin plugin;
@ -225,6 +227,7 @@ public class SpongeMainCommand extends Command<Void, LPSubjectData> {
return this.subCommands.values().stream().flatMap(List::stream).collect(ImmutableCollectors.toList()); return this.subCommands.values().stream().flatMap(List::stream).collect(ImmutableCollectors.toList());
} }
@Nonnull
@Override @Override
public Optional<List<Command<LPSubjectData, ?>>> getChildren() { public Optional<List<Command<LPSubjectData, ?>>> getChildren() {
return Optional.of(getSubCommands()); return Optional.of(getSubCommands());