From 32c4950059fdd8e5c3243b1e350e75e7fd4d7b55 Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 1 Jan 2013 22:53:03 +0100 Subject: WIP Converters Only Integer and Enum for now, open for comments --- .../src/net/ess3/commands/Commandbigtree.java | 41 +++++++++------- .../src/net/ess3/converter/ArgumentsParser.java | 7 +++ .../src/net/ess3/converter/EnumConverter.java | 57 ++++++++++++++++++++++ .../net/ess3/converter/FirstEntryParserResult.java | 33 +++++++++++++ .../src/net/ess3/converter/IntegerConverter.java | 39 +++++++++++++++ .../src/net/ess3/converter/ParserResult.java | 9 ++++ Essentials/src/net/ess3/converter/Serializer.java | 7 +++ 7 files changed, 175 insertions(+), 18 deletions(-) create mode 100644 Essentials/src/net/ess3/converter/ArgumentsParser.java create mode 100644 Essentials/src/net/ess3/converter/EnumConverter.java create mode 100644 Essentials/src/net/ess3/converter/FirstEntryParserResult.java create mode 100644 Essentials/src/net/ess3/converter/IntegerConverter.java create mode 100644 Essentials/src/net/ess3/converter/ParserResult.java create mode 100644 Essentials/src/net/ess3/converter/Serializer.java diff --git a/Essentials/src/net/ess3/commands/Commandbigtree.java b/Essentials/src/net/ess3/commands/Commandbigtree.java index 612c19008..c9440008f 100644 --- a/Essentials/src/net/ess3/commands/Commandbigtree.java +++ b/Essentials/src/net/ess3/commands/Commandbigtree.java @@ -1,34 +1,39 @@ package net.ess3.commands; import static net.ess3.I18n._; -import org.bukkit.Location; -import org.bukkit.TreeType; import net.ess3.api.IUser; +import net.ess3.converter.EnumConverter; import net.ess3.utils.LocationUtil; +import org.bukkit.Location; +import org.bukkit.TreeType; public class Commandbigtree extends EssentialsCommand { - @Override - public void run(final IUser user, final String commandLabel, final String[] args) throws Exception - { - final TreeType tree; - if (args.length > 0 && args[0].equalsIgnoreCase("redwood")) - { - tree = TreeType.TALL_REDWOOD; - } - else if (args.length > 0 && args[0].equalsIgnoreCase("tree")) - { - tree = TreeType.BIG_TREE; - } - else if (args.length > 0 && args[0].equalsIgnoreCase("jungle")) + private static enum BigTree { + REDWOOD(TreeType.TALL_REDWOOD), + TREE(TreeType.BIG_TREE), + JUNGLE(TreeType.JUNGLE); + + private final TreeType bukkitType; + + private BigTree(final TreeType bukkitType) { - tree = TreeType.JUNGLE; + this.bukkitType = bukkitType; } - else + + public TreeType getBukkitType() { - throw new NotEnoughArgumentsException(); + return bukkitType; } + } + + private final static EnumConverter BIGTREE_PARSER = EnumConverter.getInstance(BigTree.class); + + @Override + public void run(final IUser user, final String commandLabel, final String[] args) throws Exception + { + final TreeType tree = BIGTREE_PARSER.parse(args).getValue().getBukkitType(); final Location loc = LocationUtil.getTarget(user.getPlayer()); final Location safeLocation = LocationUtil.getSafeDestination(loc); diff --git a/Essentials/src/net/ess3/converter/ArgumentsParser.java b/Essentials/src/net/ess3/converter/ArgumentsParser.java new file mode 100644 index 000000000..d5b7cb4cb --- /dev/null +++ b/Essentials/src/net/ess3/converter/ArgumentsParser.java @@ -0,0 +1,7 @@ +package net.ess3.converter; + + +public interface ArgumentsParser +{ + ParserResult parse(String... args); +} diff --git a/Essentials/src/net/ess3/converter/EnumConverter.java b/Essentials/src/net/ess3/converter/EnumConverter.java new file mode 100644 index 000000000..22d0d4d46 --- /dev/null +++ b/Essentials/src/net/ess3/converter/EnumConverter.java @@ -0,0 +1,57 @@ +package net.ess3.converter; + +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import org.apache.commons.lang.Validate; + + +public class EnumConverter> implements ArgumentsParser, Serializer +{ + private static final Map converterMap = new HashMap(); + private final Map enumMap = new HashMap(); + private final Map serializedMap; + + private EnumConverter(final Class enumClass) + { + serializedMap = new EnumMap(enumClass); + for (E t : enumClass.getEnumConstants()) + { + enumMap.put(t.name().replaceAll("[_-]", "").toLowerCase(Locale.ENGLISH), t); + serializedMap.put(t, t.name().replace("_", "-").toLowerCase(Locale.ENGLISH)); + } + } + + public static > EnumConverter getInstance(final Class enumClass) + { + synchronized (converterMap) + { + EnumConverter converter = converterMap.get(enumClass); + if (converter == null) + { + converter = new EnumConverter(enumClass); + converterMap.put(enumClass, converter); + } + return converter; + } + } + + @Override + public ParserResult parse(final String... args) + { + Validate.notEmpty(args); + final E e = enumMap.get(args[0].replaceAll("[_-]", "").toLowerCase(Locale.ENGLISH)); + if (e == null) + { + throw new IllegalArgumentException(); + } + return new FirstEntryParserResult(e, args); + } + + @Override + public String serialize(final E input) + { + return serializedMap.get(input); + } +} diff --git a/Essentials/src/net/ess3/converter/FirstEntryParserResult.java b/Essentials/src/net/ess3/converter/FirstEntryParserResult.java new file mode 100644 index 000000000..1e51453b5 --- /dev/null +++ b/Essentials/src/net/ess3/converter/FirstEntryParserResult.java @@ -0,0 +1,33 @@ +package net.ess3.converter; + +import java.util.Arrays; + + +class FirstEntryParserResult implements ParserResult +{ + private final T value; + private final String[] input; + private String[] unusedInput = null; + + FirstEntryParserResult(T value, String... input) + { + this.value = value; + this.input = input; + } + + @Override + public T getValue() + { + return value; + } + + @Override + public String[] getUnusedInput() + { + if (unusedInput == null) + { + unusedInput = Arrays.copyOfRange(input, input.length > 0 ? 1 : 0, input.length); + } + return unusedInput; + } +} diff --git a/Essentials/src/net/ess3/converter/IntegerConverter.java b/Essentials/src/net/ess3/converter/IntegerConverter.java new file mode 100644 index 000000000..835a0d293 --- /dev/null +++ b/Essentials/src/net/ess3/converter/IntegerConverter.java @@ -0,0 +1,39 @@ +package net.ess3.converter; + +import org.apache.commons.lang.Validate; + + +public class IntegerConverter implements ArgumentsParser, Serializer +{ + private static final IntegerConverter INSTANCE = new IntegerConverter(); + + private IntegerConverter() + { + } + + public static IntegerConverter getInstance() + { + return INSTANCE; + } + + @Override + public ParserResult parse(final String... args) + { + Validate.notEmpty(args); + try + { + final int number = Integer.parseInt(args[0]); + return new FirstEntryParserResult(number, args); + } + catch (NumberFormatException ex) + { + throw new IllegalArgumentException(ex); + } + } + + @Override + public String serialize(final Integer input) + { + return input.toString(); + } +} diff --git a/Essentials/src/net/ess3/converter/ParserResult.java b/Essentials/src/net/ess3/converter/ParserResult.java new file mode 100644 index 000000000..0d178c98b --- /dev/null +++ b/Essentials/src/net/ess3/converter/ParserResult.java @@ -0,0 +1,9 @@ +package net.ess3.converter; + + +public interface ParserResult +{ + T getValue(); + + String[] getUnusedInput(); +} diff --git a/Essentials/src/net/ess3/converter/Serializer.java b/Essentials/src/net/ess3/converter/Serializer.java new file mode 100644 index 000000000..da06915f0 --- /dev/null +++ b/Essentials/src/net/ess3/converter/Serializer.java @@ -0,0 +1,7 @@ +package net.ess3.converter; + + +public interface Serializer +{ + String serialize(T input); +} -- cgit v1.2.3