This commit is contained in:
Keith Donald
2008-06-30 18:12:50 +00:00
parent e00446f746
commit ad0cee22bb
5 changed files with 21 additions and 30 deletions

View File

@@ -39,18 +39,4 @@ public interface ConversionService {
public ConversionExecutor getConversionExecutor(Class sourceClass, Class targetClass)
throws ConversionExecutorNotFoundException;
/**
* Return a custom conversion executor capable of converting source objects of the specified
* <code>sourceClass</code> to instances of the <code>targetClass</code>.
* <p>
* The returned ConversionExecutor is thread-safe and may safely be cached for use in client code.
* @param id the id of the custom conversion executor (should be unique among custom converters)
* @param sourceClass the source class to convert from
* @param targetClass the target class to convert to
* @return the executor that can execute instance type conversion, never null
* @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found
*/
public ConversionExecutor getConversionExecutor(String id, Class sourceClass, Class targetClass)
throws ConversionExecutorNotFoundException;
}

View File

@@ -2,7 +2,6 @@ package org.springframework.binding.convert.converters;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
@@ -65,18 +64,17 @@ public class ArrayToCollection implements TwoWayConverter {
converter = null;
}
Collection collection = (Collection) constructor.newInstance(null);
Method add = collectionImplClass.getMethod("add", new Class[] { Object.class });
int length = Array.getLength(source);
if (converter != null) {
for (int i = 0; i < length; i++) {
Object value = Array.get(source, i);
value = converter.execute(value);
add.invoke(collection, new Object[] { value });
collection.add(value);
}
} else {
for (int i = 0; i < length; i++) {
Object value = Array.get(source, i);
add.invoke(collection, new Object[] { value });
collection.add(value);
}
}
return collection;
@@ -91,9 +89,12 @@ public class ArrayToCollection implements TwoWayConverter {
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
Object value = it.next();
ConversionExecutor converter = conversionService.getConversionExecutor(value.getClass(), sourceClass
.getComponentType());
Array.set(array, i, converter.execute(value));
if (value != null) {
ConversionExecutor converter = conversionService.getConversionExecutor(value.getClass(), sourceClass
.getComponentType());
value = converter.execute(value);
}
Array.set(array, i, value);
}
return array;
}

View File

@@ -56,17 +56,19 @@ public class ObjectToArray implements TwoWayConverter {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
Object value = Array.get(target, i);
buffer.append(converter.execute(value));
if (i < length) {
if (value != null) {
buffer.append(converter.execute(value));
}
if (i < length - 1) {
buffer.append(",");
}
}
return buffer.toString();
} else {
Object value = Array.get(target, 0);
Class componentType = target.getClass().getComponentType();
ConversionExecutor converter = conversionService.getConversionExecutor(componentType, sourceClass);
return converter.execute(value);
}
return null;
}
}

View File

@@ -45,7 +45,7 @@ public class GenericConversionService implements ConversionService {
* is a map of target classes that can be converted to, ultimately mapping to a specific converter that can perform
* the source->target conversion.
*/
private Map sourceClassConverters = new HashMap();
private final Map sourceClassConverters = new HashMap();
/**
* An optional parent conversion service.
@@ -147,11 +147,6 @@ public class GenericConversionService implements ConversionService {
}
}
public ConversionExecutor getConversionExecutor(String id, Class sourceClass, Class targetClass)
throws ConversionExecutorNotFoundException {
throw new UnsupportedOperationException("Not yet implemented");
}
// subclassing support
/**