Update boolean and string components to comply to checkstyle

This commit is contained in:
broccolai 2020-09-11 22:11:14 +01:00 committed by Alexander Söderberg
parent 95e1a597bd
commit d95c35c1a8
2 changed files with 127 additions and 13 deletions

View file

@ -1,3 +1,26 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package com.intellectualsites.commands.components.standard;
import com.intellectualsites.commands.components.CommandComponent;
@ -12,30 +35,59 @@ import java.util.List;
import java.util.Queue;
@SuppressWarnings("unused")
public class BooleanComponent<C extends CommandSender> extends CommandComponent<C, Boolean> {
public final class BooleanComponent<C extends CommandSender> extends CommandComponent<C, Boolean> {
private final boolean liberal;
public BooleanComponent(final boolean required, @Nonnull final String name,
final boolean liberal, @Nonnull final String defaultValue) {
private 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;
}
/**
* Create a new builder
*
* @param name Name of the component
* @param <C> Command sender type
* @return Created builder
*/
@Nonnull
public static <C extends CommandSender> Builder<C> newBuilder(@Nonnull final String name) {
return new Builder<>(name);
}
/**
* Create a new required command component
*
* @param name Component name
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends CommandSender> CommandComponent<C, Boolean> required(@Nonnull final String name) {
return BooleanComponent.<C>newBuilder(name).asRequired().build();
}
/**
* Create a new optional command component
*
* @param name Component name
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends CommandSender> CommandComponent<C, Boolean> optional(@Nonnull final String name) {
return BooleanComponent.<C>newBuilder(name).asOptional().build();
}
/**
* Create a new required command component with a default value
*
* @param name Component name
* @param defaultNum Default num
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends CommandSender> CommandComponent<C, Boolean> optional(@Nonnull final String name,
final String defaultNum) {
@ -50,38 +102,49 @@ public class BooleanComponent<C extends CommandSender> extends CommandComponent<
super(name);
}
/**
* Set the liberal toggle
*
* @param liberal liberal value
* @return Builder instance
*/
@Nonnull
public Builder<C> withLiberal(final boolean liberal) {
this.liberal = liberal;
return this;
}
/**
* Builder a new boolean component
*
* @return Constructed component
*/
@Nonnull
@Override
public BooleanComponent<C> build() {
return new BooleanComponent<>(this.required, this.name, this.liberal, this.defaultValue);
return new BooleanComponent<>(this.isRequired(), this.getName(), this.liberal, this.getDefaultValue());
}
}
/**
* Get the greedy boolean
* Get the liberal boolean
*
* @return Greedy boolean
* @return Liberal boolean
*/
public boolean isLiberal() {
return liberal;
}
private static final class BooleanParser<C extends CommandSender> implements ComponentParser<C, Boolean> {
// todo
private static final List<String> LIBERAL = Arrays.asList("TRUE", "YES", "ON", "FALSE", "NO", "OFF");
private static final List<String> LIBERAL_TRUE = Arrays.asList("TRUE", "YES", "ON");
private static final List<String> LIBERAL_FALSE = Arrays.asList("FALSE", "NO", "OFF");
private final boolean liberal;
public BooleanParser(final boolean liberal) {
private BooleanParser(final boolean liberal) {
this.liberal = liberal;
}
@ -136,11 +199,22 @@ public class BooleanComponent<C extends CommandSender> extends CommandComponent<
private final boolean liberal;
/**
* Construct a new boolean parse exception
*
* @param input String input
* @param liberal Liberal value
*/
public BooleanParseException(@Nonnull final String input, final boolean liberal) {
super(input);
this.liberal = liberal;
}
/**
* Get the liberal boolean value
*
* @return Liberal value
*/
public boolean isLiberal() {
return liberal;
}

View file

@ -11,30 +11,59 @@ import java.util.StringJoiner;
import javax.annotation.Nonnull;
@SuppressWarnings("unused")
public class StringComponent<C extends CommandSender> extends CommandComponent<C, String> {
public final class StringComponent<C extends CommandSender> extends CommandComponent<C, String> {
private final boolean greedy;
public StringComponent(final boolean required, @Nonnull final String name,
final boolean greedy, @Nonnull final String defaultValue) {
private StringComponent(final boolean required, @Nonnull final String name,
final boolean greedy, @Nonnull final String defaultValue) {
super(required, name, new StringParser<>(greedy), defaultValue);
this.greedy = greedy;
}
/**
* Create a new builder
*
* @param name Name of the component
* @param <C> Command sender type
* @return Created builder
*/
@Nonnull
public static <C extends CommandSender> StringComponent.Builder<C> newBuilder(@Nonnull final String name) {
return new StringComponent.Builder<>(name);
}
/**
* Create a new required command component
*
* @param name Component name
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends CommandSender> CommandComponent<C, String> required(@Nonnull final String name) {
return StringComponent.<C>newBuilder(name).asRequired().build();
}
/**
* Create a new optional command component
*
* @param name Component name
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends CommandSender> CommandComponent<C, String> optional(@Nonnull final String name) {
return StringComponent.<C>newBuilder(name).asOptional().build();
}
/**
* Create a new required command component with a default value
*
* @param name Component name
* @param defaultNum Default num
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends CommandSender> CommandComponent<C, String> optional(@Nonnull final String name,
final String defaultNum) {
@ -49,16 +78,27 @@ public class StringComponent<C extends CommandSender> extends CommandComponent<C
super(name);
}
/**
* Set the greedy toggle
*
* @param greedy greedy value
* @return Builder instance
*/
@Nonnull
public Builder<C> withGreedy(final boolean greedy) {
this.greedy = greedy;
return this;
}
/**
* Builder a new string component
*
* @return Constructed component
*/
@Nonnull
@Override
public StringComponent<C> build() {
return new StringComponent<>(this.required, this.name, this.greedy, this.defaultValue);
return new StringComponent<>(this.isRequired(), this.getName(), this.greedy, this.getDefaultValue());
}
}
@ -76,7 +116,7 @@ public class StringComponent<C extends CommandSender> extends CommandComponent<C
private final boolean greedy;
public StringParser(final boolean greedy) {
private StringParser(final boolean greedy) {
this.greedy = greedy;
}