ArrayToCollectionConverter.java 2.5 KB
Newer Older
K
Keith Donald 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
16

K
Keith Donald 已提交
17 18
package org.springframework.core.convert.support;

19 20
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;

K
Keith Donald 已提交
21 22 23 24
import java.lang.reflect.Array;
import java.util.Collection;

import org.springframework.core.CollectionFactory;
K
Keith Donald 已提交
25
import org.springframework.core.convert.ConverterNotFoundException;
K
Keith Donald 已提交
26 27
import org.springframework.core.convert.TypeDescriptor;

28 29 30 31 32 33 34
/**
 * Converts from an array to a collection.
 *
 * @author Keith Donald
 * @since 3.0
 */
final class ArrayToCollectionConverter implements GenericConverter {
K
Keith Donald 已提交
35

K
Keith Donald 已提交
36
	private final GenericConversionService conversionService;
K
Keith Donald 已提交
37

38
	public ArrayToCollectionConverter(GenericConversionService conversionService) {
K
Keith Donald 已提交
39 40
		this.conversionService = conversionService;
	}
41

K
polish  
Keith Donald 已提交
42
	@SuppressWarnings("unchecked")
K
Keith Donald 已提交
43
	public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
44 45 46
		if (source == null) {
			return this.conversionService.convertNullSource(sourceType, targetType);
		}		
K
Keith Donald 已提交
47 48 49 50 51 52 53 54
		int length = Array.getLength(source);
		Collection collection = CollectionFactory.createCollection(targetType.getType(), length);
		TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
		TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
		if (targetElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetElementType)) {
			for (int i = 0; i < length; i++) {
				collection.add(Array.get(source, i));
			}
55 56
		}
		else {
K
Keith Donald 已提交
57
			GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
K
Keith Donald 已提交
58
			if (converter == null) {
K
Keith Donald 已提交
59
				throw new ConverterNotFoundException(sourceElementType, targetElementType);
60
			}
K
Keith Donald 已提交
61
			for (int i = 0; i < length; i++) {
62 63 64
				Object sourceElement = Array.get(source, i);
				Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetElementType);
				collection.add(targetElement);
K
Keith Donald 已提交
65 66 67 68 69 70
			}
		}
		return collection;
	}

}