Author: doogie
Date: Mon Mar 1 03:13:37 2010 New Revision: 917365 URL: http://svn.apache.org/viewvc?rev=917365&view=rev Log: BUG FIX: Add array->List converter. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java?rev=917365&r1=917364&r2=917365&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java Mon Mar 1 03:13:37 2010 @@ -18,6 +18,7 @@ *******************************************************************************/ package org.ofbiz.base.conversion; +import java.lang.reflect.Array; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -28,9 +29,39 @@ import org.ofbiz.base.util.ObjectType; import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilGenerics; +import org.ofbiz.base.util.UtilValidate; /** Collection Converter classes. */ public class CollectionConverters implements ConverterLoader { + public static class ArrayCreator implements ConverterCreator, ConverterLoader { + public void loadConverters() { + Converters.registerCreator(this); + } + + public <S, T> Converter<S, T> createConverter(Class<S> sourceClass, Class<T> targetClass) { + if (!sourceClass.isArray() || targetClass != List.class) { + return null; + } + return UtilGenerics.cast(new ArrayClassToList<S, T>(sourceClass, targetClass)); + } + } + + private static class ArrayClassToList<S, T> extends AbstractConverter<S, T> { + public ArrayClassToList(Class<S> sourceClass, Class<T> targetClass) { + super(sourceClass, targetClass); + } + + public T convert(S obj) throws ConversionException { + List<Object> list = FastList.newInstance(); + int len = Array.getLength(obj); + for (int i = 0; i < len; i++) { + list.add(Array.get(obj, i)); + } + return UtilGenerics.<T>cast(list); + } + } + public static class ArrayToList<T> extends AbstractConverter<T[], List<T>> { public ArrayToList() { super(Object[].class, List.class); Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java?rev=917365&r1=917364&r2=917365&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/ObjectTypeTests.java Mon Mar 1 03:13:37 2010 @@ -192,7 +192,7 @@ } public void testArray() throws GeneralException { - //BUG simpleTypeConvertTestSingleMulti("array->List", new Object[] {"one", "two", "three"}, new String[] {"List", "java.util.List"}, list); + simpleTypeConvertTestSingleMulti("array->List", new Object[] {"one", "two", "three"}, new String[] {"List", "java.util.List"}, list); } public void testString() throws GeneralException, Exception { |
Free forum by Nabble | Edit this page |