[Japi-cvs] SF.net SVN: japi:[776] libs/argparser/trunk/src
Status: Beta
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2009-01-12 06:25:56
|
Revision: 776
http://japi.svn.sourceforge.net/japi/?rev=776&view=rev
Author: christianhujer
Date: 2009-01-12 06:25:53 +0000 (Mon, 12 Jan 2009)
Log Message:
-----------
[2500637] Converter for Enum.class missing
Modified Paths:
--------------
libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java
Added Paths:
-----------
libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/EnumConverter.java
libs/argparser/trunk/src/tst/test/net/sf/japi/io/args/converter/EnumConverterTest.java
Modified: libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java
===================================================================
--- libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java 2009-01-12 05:54:49 UTC (rev 775)
+++ libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java 2009-01-12 06:25:53 UTC (rev 776)
@@ -79,10 +79,13 @@
@Nullable Converter<T> converter = (Converter<T>) converters.get(clazz);
if (converter == null) {
converter = getConstructorConverter(clazz);
- if (converter != null) {
- register(converter);
- }
}
+ if (converter == null && Enum.class.isAssignableFrom(clazz)) {
+ converter = (Converter<T>) getEnumConverter((Class<? extends Enum>) clazz);
+ }
+ if (converter != null) {
+ register(converter);
+ }
return converter;
}
@@ -150,4 +153,16 @@
}
}
+ /** Returns an enum converter for the target type.
+ * @param targetType target type to convert to.
+ * @return EnumConverter for the target type.
+ */
+ @Nullable public static <T extends Enum> EnumConverter<T> getEnumConverter(@NotNull final Class<T> targetType) {
+ try {
+ return new EnumConverter<T>(targetType);
+ } catch (final Exception ignore) {
+ return null;
+ }
+ }
+
} // class ConverterRegistry
Added: libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/EnumConverter.java
===================================================================
--- libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/EnumConverter.java (rev 0)
+++ libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/EnumConverter.java 2009-01-12 06:25:53 UTC (rev 776)
@@ -0,0 +1,42 @@
+/*
+ * JAPI libs-argparser is a library for parsing command line arguments.
+ * Copyright (C) 2009 Christian Hujer.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package net.sf.japi.io.args.converter;
+
+import org.jetbrains.annotations.NotNull;
+import java.util.Locale;
+
+/** Generic converter for Enum classes.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class EnumConverter<T extends Enum> extends AbstractConverter<T> {
+
+ /** Create an AbstractConverter.
+ * @param targetClass Enum class to convert to.
+ */
+ public EnumConverter(@NotNull final Class<T> targetClass) {
+ super(targetClass);
+ }
+
+ /** {@inheritDoc} */
+ @NotNull
+ public T convert(@NotNull final Locale locale, @NotNull final String arg) throws Exception {
+ return (T) Enum.valueOf(getTargetClass(), arg);
+ }
+}
Property changes on: libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/EnumConverter.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ LF
Added: libs/argparser/trunk/src/tst/test/net/sf/japi/io/args/converter/EnumConverterTest.java
===================================================================
--- libs/argparser/trunk/src/tst/test/net/sf/japi/io/args/converter/EnumConverterTest.java (rev 0)
+++ libs/argparser/trunk/src/tst/test/net/sf/japi/io/args/converter/EnumConverterTest.java 2009-01-12 06:25:53 UTC (rev 776)
@@ -0,0 +1,54 @@
+/*
+ * JAPI libs-argparser is a library for parsing command line arguments.
+ * Copyright (C) 2009 Christian Hujer.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package test.net.sf.japi.io.args.converter;
+
+import net.sf.japi.io.args.converter.ConverterRegistry;
+import net.sf.japi.io.args.converter.EnumConverter;
+import org.junit.Assert;
+import org.junit.Test;
+
+/** Test for {@link EnumConverter}.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class EnumConverterTest {
+
+ /** Tests that existing enum constants are found by the EnumConverter.
+ * @throws Exception (unexpected).
+ */
+ @Test
+ public void testGetEnumConverter() throws Exception {
+ TestEnum v;
+
+ v = ConverterRegistry.convert(TestEnum.class, "EC1");
+ Assert.assertSame("Expecting \"EC1\" for TestEnum.class to be converted to TestEnum.EC1.", v, TestEnum.EC1);
+
+ v = ConverterRegistry.convert(TestEnum.class, "EC2");
+ Assert.assertSame("Expecting \"EC2\" for TestEnum.class to be converted to TestEnum.EC2.", v, TestEnum.EC2);
+ }
+
+ /** Test enum. */
+ enum TestEnum {
+ /** Test enum constant 1. */
+ EC1,
+
+ /** Test enum constant 2. */
+ EC2
+ }
+}
Property changes on: libs/argparser/trunk/src/tst/test/net/sf/japi/io/args/converter/EnumConverterTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|