summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsnowleo <schneeleo@gmail.com>2013-01-01 22:53:03 +0100
committersnowleo <schneeleo@gmail.com>2013-01-01 22:53:03 +0100
commit32c4950059fdd8e5c3243b1e350e75e7fd4d7b55 (patch)
tree12141421d91ddb20ce15a9e3c04d258421ccf709
parent72c5ab8fd36b0c4a0d0b50e2dd1cb6dacc20c9ea (diff)
downloadEssentials-32c4950059fdd8e5c3243b1e350e75e7fd4d7b55.tar
Essentials-32c4950059fdd8e5c3243b1e350e75e7fd4d7b55.tar.gz
Essentials-32c4950059fdd8e5c3243b1e350e75e7fd4d7b55.tar.lz
Essentials-32c4950059fdd8e5c3243b1e350e75e7fd4d7b55.tar.xz
Essentials-32c4950059fdd8e5c3243b1e350e75e7fd4d7b55.zip
WIP Converters
Only Integer and Enum for now, open for comments
-rw-r--r--Essentials/src/net/ess3/commands/Commandbigtree.java41
-rw-r--r--Essentials/src/net/ess3/converter/ArgumentsParser.java7
-rw-r--r--Essentials/src/net/ess3/converter/EnumConverter.java57
-rw-r--r--Essentials/src/net/ess3/converter/FirstEntryParserResult.java33
-rw-r--r--Essentials/src/net/ess3/converter/IntegerConverter.java39
-rw-r--r--Essentials/src/net/ess3/converter/ParserResult.java9
-rw-r--r--Essentials/src/net/ess3/converter/Serializer.java7
7 files changed, 175 insertions, 18 deletions
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> 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<T>
+{
+ ParserResult<T> 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<E extends Enum<E>> implements ArgumentsParser<E>, Serializer<E>
+{
+ private static final Map<Class, EnumConverter> converterMap = new HashMap<Class, EnumConverter>();
+ private final Map<String, E> enumMap = new HashMap<String, E>();
+ private final Map<E, String> serializedMap;
+
+ private EnumConverter(final Class<E> enumClass)
+ {
+ serializedMap = new EnumMap<E, String>(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 <T extends Enum<T>> EnumConverter<T> getInstance(final Class<T> enumClass)
+ {
+ synchronized (converterMap)
+ {
+ EnumConverter<T> converter = converterMap.get(enumClass);
+ if (converter == null)
+ {
+ converter = new EnumConverter<T>(enumClass);
+ converterMap.put(enumClass, converter);
+ }
+ return converter;
+ }
+ }
+
+ @Override
+ public ParserResult<E> 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>(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<T> implements ParserResult<T>
+{
+ 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<Integer>, Serializer<Integer>
+{
+ private static final IntegerConverter INSTANCE = new IntegerConverter();
+
+ private IntegerConverter()
+ {
+ }
+
+ public static IntegerConverter getInstance()
+ {
+ return INSTANCE;
+ }
+
+ @Override
+ public ParserResult<Integer> parse(final String... args)
+ {
+ Validate.notEmpty(args);
+ try
+ {
+ final int number = Integer.parseInt(args[0]);
+ return new FirstEntryParserResult<Integer>(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>
+{
+ 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<T>
+{
+ String serialize(T input);
+}