From c6372fcd7e7e13a560e5e5adae2c35a62c809134 Mon Sep 17 00:00:00 2001 From: broccolai Date: Fri, 11 Sep 2020 02:54:40 +0000 Subject: [PATCH] Add boolean component --- .../components/standard/BooleanComponent.java | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 cloud-core/src/main/java/com/intellectualsites/commands/components/standard/BooleanComponent.java diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/components/standard/BooleanComponent.java b/cloud-core/src/main/java/com/intellectualsites/commands/components/standard/BooleanComponent.java new file mode 100644 index 00000000..3d803a2a --- /dev/null +++ b/cloud-core/src/main/java/com/intellectualsites/commands/components/standard/BooleanComponent.java @@ -0,0 +1,132 @@ +package com.intellectualsites.commands.components.standard; + +import com.intellectualsites.commands.components.CommandComponent; +import com.intellectualsites.commands.components.parser.ComponentParseResult; +import com.intellectualsites.commands.components.parser.ComponentParser; +import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.sender.CommandSender; + +import javax.annotation.Nonnull; +import java.util.Queue; + +@SuppressWarnings("unused") +public class BooleanComponent extends CommandComponent { + private final boolean liberal; + + public BooleanComponent(final boolean required, @Nonnull final String name, + final boolean liberal, @Nonnull final String defaultValue) { + super(required, name, new BooleanParser<>(liberal), defaultValue); + this.liberal = liberal; + } + + @Nonnull + public static Builder newBuilder(@Nonnull final String name) { + return new Builder<>(name); + } + + @Nonnull + public static CommandComponent required(@Nonnull final String name) { + return BooleanComponent.newBuilder(name).asRequired().build(); + } + + @Nonnull + public static CommandComponent optional(@Nonnull final String name) { + return BooleanComponent.newBuilder(name).asOptional().build(); + } + + @Nonnull + public static CommandComponent optional(@Nonnull final String name, + final String defaultNum) { + return BooleanComponent.newBuilder(name).asOptionalWithDefault(defaultNum).build(); + } + + public static final class Builder extends CommandComponent.Builder { + + private boolean liberal = false; + + protected Builder(@Nonnull final String name) { + super(name); + } + + @Nonnull + public Builder withLiberal(final boolean liberal) { + this.liberal = liberal; + return this; + } + + @Nonnull + @Override + public BooleanComponent build() { + return new BooleanComponent<>(this.required, this.name, this.liberal, this.defaultValue); + } + + } + + /** + * Get the greedy boolean + * + * @return Greedy boolean + */ + public boolean isLiberal() { + return liberal; + } + + private static final class BooleanParser implements ComponentParser { + + private final boolean liberal; + + public BooleanParser(final boolean liberal) { + this.liberal = liberal; + } + + @Nonnull + @Override + public ComponentParseResult parse(@Nonnull final CommandContext commandContext, + @Nonnull final Queue inputQueue) { + final String input = inputQueue.peek(); + if (input == null) { + return ComponentParseResult.failure(new NullPointerException("No input was provided")); + } + inputQueue.remove(); + + if (!liberal) { + if (input.equalsIgnoreCase("true")) { + return ComponentParseResult.success(true); + } + + if (input.equalsIgnoreCase("false")) { + return ComponentParseResult.success(false); + } + + return ComponentParseResult.failure(new BooleanParseException(input, false)); + } + + switch (input.toUpperCase()) { + case "TRUE": + case "YES": + case "ON": + return ComponentParseResult.success(true); + case "FALSE": + case "NO": + case "OFF": + return ComponentParseResult.success(false); + default: + return ComponentParseResult.failure(new BooleanParseException(input, true)); + } + } + } + + public static final class BooleanParseException extends IllegalArgumentException { + + private final boolean liberal; + + public BooleanParseException(@Nonnull final String input, final boolean liberal) { + super(input); + this.liberal = liberal; + } + + public boolean isLiberal() { + return liberal; + } + } +}