diff options
author | snowleo <schneeleo@gmail.com> | 2013-01-01 23:00:35 +0100 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2013-01-01 23:00:35 +0100 |
commit | 3c39ee58e0b2fb824a17cfbb43e38e01445ef76a (patch) | |
tree | 7e58a29579af0a93f81f66776339645fb60be48a | |
parent | 32c4950059fdd8e5c3243b1e350e75e7fd4d7b55 (diff) | |
download | Essentials-3c39ee58e0b2fb824a17cfbb43e38e01445ef76a.tar Essentials-3c39ee58e0b2fb824a17cfbb43e38e01445ef76a.tar.gz Essentials-3c39ee58e0b2fb824a17cfbb43e38e01445ef76a.tar.lz Essentials-3c39ee58e0b2fb824a17cfbb43e38e01445ef76a.tar.xz Essentials-3c39ee58e0b2fb824a17cfbb43e38e01445ef76a.zip |
clean EnumConverter
-rw-r--r-- | Essentials/src/net/ess3/converter/EnumConverter.java | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/Essentials/src/net/ess3/converter/EnumConverter.java b/Essentials/src/net/ess3/converter/EnumConverter.java index 22d0d4d46..309ff3b78 100644 --- a/Essentials/src/net/ess3/converter/EnumConverter.java +++ b/Essentials/src/net/ess3/converter/EnumConverter.java @@ -4,12 +4,14 @@ import java.util.EnumMap; import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.regex.Pattern; 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 static final Pattern REPLACE = Pattern.compile("[_-]"); private final Map<String, E> enumMap = new HashMap<String, E>(); private final Map<E, String> serializedMap; @@ -18,8 +20,8 @@ public class EnumConverter<E extends Enum<E>> implements ArgumentsParser<E>, Ser 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)); + enumMap.put(cleanString(t.name()), t); + serializedMap.put(t, prettifyString(t.name())); } } @@ -41,7 +43,8 @@ public class EnumConverter<E extends Enum<E>> implements ArgumentsParser<E>, Ser public ParserResult<E> parse(final String... args) { Validate.notEmpty(args); - final E e = enumMap.get(args[0].replaceAll("[_-]", "").toLowerCase(Locale.ENGLISH)); + Validate.notEmpty(args[0]); + final E e = enumMap.get(cleanString(args[0])); if (e == null) { throw new IllegalArgumentException(); @@ -54,4 +57,12 @@ public class EnumConverter<E extends Enum<E>> implements ArgumentsParser<E>, Ser { return serializedMap.get(input); } + + private String cleanString(final String input) { + return REPLACE.matcher(input).replaceAll("").toLowerCase(Locale.ENGLISH); + } + + private String prettifyString(final String input) { + return input.replace("_", "-").toLowerCase(Locale.ENGLISH); + } } |