From aab911e8ef39f56bdb90c4d893d4daa1b7b0cff0 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Mon, 17 Feb 2020 15:39:42 +0100 Subject: [PATCH] Create DoubleFlag + extract common parsing stuff --- .../plotsquared/plot/config/Captions.java | 3 +- .../plotsquared/plot/flag/Flags.java | 2 +- .../plot/flags/types/DoubleFlag.java | 38 +++++++++++++++++ .../plot/flags/types/IntegerFlag.java | 34 +++++---------- .../plot/flags/types/NumberFlag.java | 41 +++++++++++++++++++ 5 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/DoubleFlag.java create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java index e1e2749e1..6fc134e9f 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Captions.java @@ -533,6 +533,7 @@ public enum Captions implements Caption { // FLAG_CATEGORY_STRING("String Flags", "Flags"), FLAG_CATEGORY_INTEGERS("Integer Flags", "Flags"), + FLAG_CATEGORY_DOUBLES("Decimal Flags", "Flags"), FLAG_CATEGORY_TELEPORT_DENY("Teleport Deny Flag", "Flags"), FLAG_CATEGORY_STRING_LIST("String List Flags", "Flags"), FLAG_CATEGORY_WEATHER("Weather Flags", "Flags"), @@ -620,7 +621,7 @@ public enum Captions implements Caption { FLAG_ERROR_LONG("Flag value must be a whole number (large numbers allowed)", "Flags"), FLAG_ERROR_PLOTBLOCKLIST("Flag value must be a block list", "Flags"), FLAG_ERROR_INVALID_BLOCK("The provided value is not a valid block", "Flags"), - FLAG_ERROR_PRICE("Flag value must be a positive number.", "Flags"), + FLAG_ERROR_DOUBLE("Flag value must be a decimal number.", "Flags"), FLAG_ERROR_STRING("Flag value must be alphanumeric. Some special characters are allowed.", "Flags"), FLAG_ERROR_STRINGLIST("Flag value must be a string list", "Flags"), FLAG_ERROR_WEATHER("Flag must be a weather: 'rain' or 'sun'", "Flags"), diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java index e7d8c72eb..0d060d815 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java @@ -18,7 +18,7 @@ public final class Flags { } @Override public String getValueDescription() { - return Captions.FLAG_ERROR_PRICE.getTranslated(); + return Captions.FLAG_ERROR_DOUBLE.getTranslated(); } }; public static final StringListFlag BLOCKED_CMDS = new StringListFlag("blocked-cmds"); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/DoubleFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/DoubleFlag.java new file mode 100644 index 000000000..db0d0f0f1 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/DoubleFlag.java @@ -0,0 +1,38 @@ +package com.github.intellectualsites.plotsquared.plot.flags.types; + +import com.github.intellectualsites.plotsquared.plot.config.Caption; +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; +import org.jetbrains.annotations.NotNull; + +public abstract class DoubleFlag> extends NumberFlag { + + + protected DoubleFlag(@NotNull Double value, Double minimum, Double maximum, @NotNull Caption flagDescription) { + super(value, minimum, maximum, Captions.FLAG_CATEGORY_DOUBLES, flagDescription); + } + + protected DoubleFlag(@NotNull Double value, @NotNull Caption flagDescription) { + this(value, Double.MIN_VALUE, Double.MAX_VALUE, flagDescription); + } + + @Override public F merge(@NotNull Double newValue) { + return flagOf(getValue() + newValue); + } + + @Override public String getExample() { + return "12.175"; + } + + @Override public String toString() { + return getValue().toString(); + } + + @NotNull @Override protected Double parseNumber(String input) throws FlagParseException { + try { + return Double.parseDouble(input); + } catch (Throwable throwable) { + throw new FlagParseException(this, input, Captions.FLAG_ERROR_DOUBLE); + } + } +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java index 8de201d63..e1508ff79 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/IntegerFlag.java @@ -3,40 +3,18 @@ package com.github.intellectualsites.plotsquared.plot.flags.types; import com.github.intellectualsites.plotsquared.plot.config.Caption; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; -import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; import org.jetbrains.annotations.NotNull; -public abstract class IntegerFlag> extends PlotFlag { - private final int minimum; - private final int maximum; +public abstract class IntegerFlag> extends NumberFlag { protected IntegerFlag(final int value, int minimum, int maximum, @NotNull Caption flagDescription) { - super(value, Captions.FLAG_CATEGORY_INTEGERS, flagDescription); - if (maximum < minimum) { - throw new IllegalArgumentException("Maximum may not be less than minimum:" + maximum + " < " + minimum); - } - this.minimum = minimum; - this.maximum = maximum; + super(value, minimum, maximum, Captions.FLAG_CATEGORY_INTEGERS, flagDescription); } protected IntegerFlag(@NotNull Caption flagDescription) { this(0, Integer.MIN_VALUE, Integer.MAX_VALUE, flagDescription); } - @Override public F parse(@NotNull String input) throws FlagParseException { - int parsed; - try { - parsed = Integer.parseInt(input); - } catch (final Throwable throwable) { - throw new FlagParseException(this, input, Captions.FLAG_ERROR_INTEGER); - } - if (parsed < minimum || parsed > maximum) { - throw new FlagParseException(this, input, Captions.NOT_VALID_NUMBER); // TODO format Caption, provide valid range - } - return flagOf(parsed); - - } - @Override public F merge(@NotNull Integer newValue) { return flagOf(getValue() + newValue); } @@ -48,4 +26,12 @@ public abstract class IntegerFlag> extends PlotFl @Override public String getExample() { return "10"; } + + @NotNull @Override protected Integer parseNumber(String input) throws FlagParseException { + try { + return Integer.parseInt(input); + } catch (Throwable throwable) { + throw new FlagParseException(this, input, Captions.FLAG_ERROR_INTEGER); + } + } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java new file mode 100644 index 000000000..fd79cf773 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flags/types/NumberFlag.java @@ -0,0 +1,41 @@ +package com.github.intellectualsites.plotsquared.plot.flags.types; + +import com.github.intellectualsites.plotsquared.plot.config.Caption; +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.flags.FlagParseException; +import com.github.intellectualsites.plotsquared.plot.flags.PlotFlag; +import org.jetbrains.annotations.NotNull; + +public abstract class NumberFlag, F extends PlotFlag> extends PlotFlag { + protected final N minimum; + protected final N maximum; + + protected NumberFlag(@NotNull N value, N minimum, N maximum, @NotNull Caption flagCategory, + @NotNull Caption flagDescription) { + super(value, flagCategory, flagDescription); + if (maximum.compareTo(minimum) < 0) { + throw new IllegalArgumentException("Maximum may not be less than minimum:" + maximum + " < " + minimum); + } + this.minimum = minimum; + this.maximum = maximum; + } + + @Override public F parse(@NotNull String input) throws FlagParseException { + final N parsed = parseNumber(input); + if (parsed.compareTo(minimum) < 0 || parsed.compareTo(maximum) > 0) { + throw new FlagParseException(this, input, + Captions.NOT_VALID_NUMBER); // TODO format Caption, provide valid range + } + return flagOf(parsed); + + } + + /** + * Parse the raw string input to the number type. + * Throw a {@link FlagParseException} if the number couldn't be parsed. + * + * @param input the string to parse the number from. + * @return the parsed number. + */ + @NotNull protected abstract N parseNumber(String input) throws FlagParseException; +}