From cb548697260605b690a58933ee460bb686ee3d7b Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Mon, 21 Sep 2009 05:30:54 +0000 Subject: [PATCH] collection to object --- .../ArrayToObjectGenericConverter.java | 15 +++++++++++++- .../CollectionToObjectGenericConverter.java | 20 +++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java index 7cb909c92ec..dde7f928ac6 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java @@ -15,6 +15,8 @@ */ package org.springframework.core.convert.support; +import java.lang.reflect.Array; + import org.springframework.core.convert.TypeDescriptor; class ArrayToObjectGenericConverter implements GenericConverter { @@ -26,7 +28,18 @@ class ArrayToObjectGenericConverter implements GenericConverter { } public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - throw new UnsupportedOperationException("Not yet implemented"); + int length = Array.getLength(source); + if (length == 0) { + return null; + } else { + TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); + if (sourceElementType.isAssignableTo(targetType)) { + return Array.get(source, 0); + } else { + GenericConverter converter = conversionService.getConverter(sourceElementType, targetType); + return converter.convert(Array.get(source, 0), sourceElementType, targetType); + } + } } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java index 071434972bc..125ef28cc4e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java @@ -15,6 +15,8 @@ */ package org.springframework.core.convert.support; +import java.util.Collection; + import org.springframework.core.convert.TypeDescriptor; class CollectionToObjectGenericConverter implements GenericConverter { @@ -24,9 +26,19 @@ class CollectionToObjectGenericConverter implements GenericConverter { public CollectionToObjectGenericConverter(GenericConversionService conversionService) { this.conversionService = conversionService; } - - public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - throw new UnsupportedOperationException("Not yet implemented"); - } + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + Collection sourceCollection = (Collection) source; + if (sourceCollection.size() == 0) { + return null; + } else { + TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor(); + if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetType)) { + return sourceCollection.iterator().next(); + } else { + GenericConverter converter = conversionService.getConverter(sourceElementType, targetType); + return converter.convert(sourceCollection.iterator().next(), sourceElementType, targetType); + } + } + } }