From cd0d699c77154818ab493c10bd9e7ea2fd16f2c1 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Fri, 25 Mar 2011 18:47:58 +0000 Subject: [PATCH] - Parse enums. - Rejigged some strange code in Color parsing. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2993 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ValueMarshaller.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/samskivert/util/ValueMarshaller.java b/src/main/java/com/samskivert/util/ValueMarshaller.java index 33ba396b..5b45d4d2 100644 --- a/src/main/java/com/samskivert/util/ValueMarshaller.java +++ b/src/main/java/com/samskivert/util/ValueMarshaller.java @@ -42,6 +42,13 @@ public class ValueMarshaller public static Object unmarshal (Class type, String source) throws Exception { + if (type.isEnum()) { + @SuppressWarnings("unchecked") // we just asked type if it was an enum... + Class etype = (Class)type; + @SuppressWarnings("unchecked") // silly compiler, we're assigning to Object + Object o = Enum.valueOf(etype, source); // may throw an exception + return o; + } // look up an argument parser for the field type Parser parser = _parsers.get(type); if (parser == null) { @@ -158,11 +165,10 @@ public class ValueMarshaller // and Color objects _parsers.put(Color.class, new Parser() { public Object parse (String source) throws Exception { - if (source.length() == 0 || source.charAt(0) != '#') { - return new Color(Integer.parseInt(source, 16)); - } else { - return new Color(Integer.parseInt(source.substring(1), 16)); + if (source.startsWith("#")) { + source = source.substring(1); } + return new Color(Integer.parseInt(source, 16)); } }); }