From dfb5203ea7010516ae7aa02889936392a982ab3a Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Thu, 14 Aug 2014 10:39:13 +0200 Subject: [PATCH 1/4] Defines new interface to implement specialized internal converters --- .../xwork2/conversion/InternalConverter.java | 12 ++++++ .../xwork2/conversion/InternalConverters.java | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverter.java create mode 100644 xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverters.java diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverter.java b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverter.java new file mode 100644 index 0000000000..50cf52bc90 --- /dev/null +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverter.java @@ -0,0 +1,12 @@ +package com.opensymphony.xwork2.conversion; + +/** + * Represents specialized converters which can convert value to given type T + */ +public interface InternalConverter { + + boolean canConvert(Object value); + + T convert(Object value); + +} diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverters.java b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverters.java new file mode 100644 index 0000000000..849e8af862 --- /dev/null +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/InternalConverters.java @@ -0,0 +1,37 @@ +package com.opensymphony.xwork2.conversion; + +import java.util.ArrayList; +import java.util.List; + +/** + * Holds reference to all internal converters to simplify lookup and registration + */ +public class InternalConverters { + + private final InternalConverter NO_OP_CONVERTER = new InternalConverter() { + + public boolean canConvert(Object value) { + return false; + } + + public T convert(Object value) { + return null; + } + }; + + private List> converters = new ArrayList>(); + + public boolean register(InternalConverter converter) { + return converters.add(converter); + } + + public InternalConverter lookup(Object value) { + for (InternalConverter converter : converters) { + if (converter.canConvert(value)) { + return converter; + } + } + return NO_OP_CONVERTER; + } + +} From b195fb3af7d44409dccdb94e675c1a7ace99daab Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Thu, 14 Aug 2014 10:39:35 +0200 Subject: [PATCH 2/4] Adds new internal converter of Double to String --- .../string/StringDoubleConverter.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java new file mode 100644 index 0000000000..f32f518587 --- /dev/null +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java @@ -0,0 +1,26 @@ +package com.opensymphony.xwork2.conversion.string; + +import com.opensymphony.xwork2.LocaleProvider; +import com.opensymphony.xwork2.conversion.InternalConverter; + +import java.text.NumberFormat; + +public class StringDoubleConverter implements InternalConverter { + + private LocaleProvider provider; + + public StringDoubleConverter(LocaleProvider provider) { + this.provider = provider; + } + + public boolean canConvert(Object value) { + return value.getClass() == Double.class || value.getClass() == double.class; + } + + public String convert(Object value) { + Double doubleValue = (Double) value; + NumberFormat format = NumberFormat.getInstance(provider.getLocale()); + return format.format(doubleValue); + } + +} From fe329f50242b17f109d3e80c4213f012ce2d950a Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Thu, 14 Aug 2014 10:39:52 +0200 Subject: [PATCH 3/4] Uses the new internal converters and adds new test case --- .../conversion/impl/StringConverter.java | 25 ++++++++++++- .../conversion/impl/StringConverterTest.java | 36 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 xwork-core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java index e9c141737d..bed8169c4e 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java @@ -1,5 +1,10 @@ package com.opensymphony.xwork2.conversion.impl; +import com.opensymphony.xwork2.LocaleProvider; +import com.opensymphony.xwork2.conversion.InternalConverter; +import com.opensymphony.xwork2.conversion.InternalConverters; +import com.opensymphony.xwork2.conversion.string.StringDoubleConverter; +import com.opensymphony.xwork2.inject.Inject; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Member; @@ -12,9 +17,18 @@ public class StringConverter extends DefaultTypeConverter { + + private InternalConverters converters = new InternalConverters(); + @Override public Object convertValue(Map context, Object target, Member member, String propertyName, Object value, Class toType) { - String result = null; + String result; + + InternalConverter converter = converters.lookup(value); + result = converter.convert(value); + if (result != null) { + return result; + } if (value instanceof int[]) { int[] x = (int[]) value; @@ -70,4 +84,13 @@ public Object convertValue(Map context, Object target, Member me } return result; } + + @Inject + public void setLocaleProvider(LocaleProvider provider) { + super.setLocaleProvider(provider); + + converters = new InternalConverters(); + converters.register(new StringDoubleConverter(provider)); + } + } diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java new file mode 100644 index 0000000000..9060dbc856 --- /dev/null +++ b/xwork-core/src/test/java/com/opensymphony/xwork2/conversion/impl/StringConverterTest.java @@ -0,0 +1,36 @@ +package com.opensymphony.xwork2.conversion.impl; + +import com.opensymphony.xwork2.LocaleProvider; +import com.opensymphony.xwork2.XWorkTestCase; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class StringConverterTest extends XWorkTestCase { + + private Map context = new HashMap(); + + public void testDoubleToString() throws Exception { + // given + Double d = 10.01; + + StringConverter converter = new StringConverter(); + converter.setLocaleProvider(buildProvider("DE")); + + // when + Object actual = converter.convertValue(context, null, null, null, d, String.class); + + // then + assertEquals("10,01", actual); + } + + private LocaleProvider buildProvider(final String locale) { + return new LocaleProvider() { + public Locale getLocale() { + return new Locale(locale); + } + }; + } + +} From 118b57589eee1a42c8fd0b24b52d326104bb5af0 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Thu, 21 Aug 2014 09:22:22 +0200 Subject: [PATCH 4/4] Simplifies usage of NumberFormat --- .../xwork2/conversion/string/StringDoubleConverter.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java index f32f518587..c0b6ed4503 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/conversion/string/StringDoubleConverter.java @@ -18,9 +18,8 @@ public boolean canConvert(Object value) { } public String convert(Object value) { - Double doubleValue = (Double) value; - NumberFormat format = NumberFormat.getInstance(provider.getLocale()); - return format.format(doubleValue); + NumberFormat format = NumberFormat.getNumberInstance(provider.getLocale()); + return format.format(value); } }