renamed internal *GenericConverters to *Converters
This commit is contained in:
parent
0543a294ee
commit
b465f204bd
|
|
@ -13,18 +13,25 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.asList;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class ArrayToMapGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a source array to a target array type.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class ArrayToArrayConverter implements GenericConverter {
|
||||
|
||||
private final GenericConverter helperConverter;
|
||||
|
||||
public ArrayToMapGenericConverter(GenericConversionService conversionService) {
|
||||
this.helperConverter = new CollectionToMapGenericConverter(conversionService);
|
||||
public ArrayToArrayConverter(GenericConversionService conversionService) {
|
||||
this.helperConverter = new CollectionToArrayConverter(conversionService);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
|
||||
|
|
@ -24,11 +25,17 @@ import org.springframework.core.CollectionFactory;
|
|||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class ArrayToCollectionGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from an array to a collection.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class ArrayToCollectionConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public ArrayToCollectionGenericConverter(GenericConversionService conversionService) {
|
||||
public ArrayToCollectionConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +48,8 @@ final class ArrayToCollectionGenericConverter implements GenericConverter {
|
|||
for (int i = 0; i < length; i++) {
|
||||
collection.add(Array.get(source, i));
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
|
||||
if (converter == null) {
|
||||
throw new ConverterNotFoundException(sourceElementType, targetElementType);
|
||||
|
|
@ -13,18 +13,24 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.asList;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import static org.springframework.core.convert.support.ConversionUtils.*;
|
||||
|
||||
final class ArrayToArrayGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from an array to a Map.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class ArrayToMapConverter implements GenericConverter {
|
||||
|
||||
private final GenericConverter helperConverter;
|
||||
|
||||
public ArrayToArrayGenericConverter(GenericConversionService conversionService) {
|
||||
this.helperConverter = new CollectionToArrayGenericConverter(conversionService);
|
||||
public ArrayToMapConverter(GenericConversionService conversionService) {
|
||||
this.helperConverter = new CollectionToMapConverter(conversionService);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
|
@ -13,18 +13,25 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.asList;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class ArrayToObjectGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from an array to a single Object.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class ArrayToObjectConverter implements GenericConverter {
|
||||
|
||||
private CollectionToObjectGenericConverter helperConverter;
|
||||
private final CollectionToObjectConverter helperConverter;
|
||||
|
||||
public ArrayToObjectGenericConverter(GenericConversionService conversionService) {
|
||||
this.helperConverter = new CollectionToObjectGenericConverter(conversionService);
|
||||
public ArrayToObjectConverter(GenericConversionService conversionService) {
|
||||
this.helperConverter = new CollectionToObjectConverter(conversionService);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
|
||||
|
|
@ -25,11 +26,17 @@ import java.util.Iterator;
|
|||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class CollectionToArrayGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a Collection to an array.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class CollectionToArrayConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public CollectionToArrayGenericConverter(GenericConversionService conversionService) {
|
||||
public CollectionToArrayConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +53,8 @@ final class CollectionToArrayGenericConverter implements GenericConverter {
|
|||
for (Iterator it = sourceCollection.iterator(); it.hasNext(); i++) {
|
||||
Array.set(array, i, it.next());
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
|
||||
if (converter == null) {
|
||||
throw new ConverterNotFoundException(sourceElementType, targetElementType);
|
||||
|
|
@ -13,25 +13,31 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
|
||||
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import static org.springframework.core.convert.support.ConversionUtils.*;
|
||||
|
||||
final class CollectionToCollectionGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a source Collection to target Collection type.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class CollectionToCollectionConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public CollectionToCollectionGenericConverter(GenericConversionService conversionService) {
|
||||
public CollectionToCollectionConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Collection sourceCollection = (Collection) source;
|
||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||
|
|
@ -42,7 +48,8 @@ final class CollectionToCollectionGenericConverter implements GenericConverter {
|
|||
if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetElementType)) {
|
||||
if (sourceType.isAssignableTo(targetType)) {
|
||||
return sourceCollection;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Collection target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
|
||||
target.addAll(sourceCollection);
|
||||
return target;
|
||||
|
|
@ -59,4 +66,4 @@ final class CollectionToCollectionGenericConverter implements GenericConverter {
|
|||
return target;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -13,24 +13,31 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import static org.springframework.core.convert.support.ConversionUtils.*;
|
||||
|
||||
final class CollectionToMapGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a Collection to a Map.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class CollectionToMapConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public CollectionToMapGenericConverter(GenericConversionService conversionService) {
|
||||
public CollectionToMapConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Collection sourceCollection = (Collection) source;
|
||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||
|
|
@ -56,13 +63,15 @@ final class CollectionToMapGenericConverter implements GenericConverter {
|
|||
String[] property = parseProperty((String) element);
|
||||
target.put(property[0], property[1]);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
for (Object element : sourceCollection) {
|
||||
target.put(element, element);
|
||||
}
|
||||
}
|
||||
return target;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Map target = CollectionFactory.createMap(targetType.getType(), sourceCollection.size());
|
||||
MapEntryConverter converter = new MapEntryConverter(sourceElementType, sourceElementType, targetKeyType,
|
||||
targetValueType, keysCompatible, valuesCompatible, conversionService);
|
||||
|
|
@ -73,7 +82,8 @@ final class CollectionToMapGenericConverter implements GenericConverter {
|
|||
Object targetValue = converter.convertValue(property[1]);
|
||||
target.put(targetKey, targetValue);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
for (Object element : sourceCollection) {
|
||||
Object targetKey = converter.convertKey(element);
|
||||
Object targetValue = converter.convertValue(element);
|
||||
|
|
@ -87,10 +97,10 @@ final class CollectionToMapGenericConverter implements GenericConverter {
|
|||
private String[] parseProperty(String string) {
|
||||
String[] property = string.split("=");
|
||||
if (property.length < 2) {
|
||||
throw new IllegalArgumentException("Invalid String property '" + property
|
||||
+ "'; properties should be in the format name=value");
|
||||
throw new IllegalArgumentException("Invalid String property '" + string +
|
||||
"'; properties should be in the format name=value");
|
||||
}
|
||||
return property;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
|
||||
|
|
@ -23,13 +24,19 @@ import java.util.Collection;
|
|||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class CollectionToObjectGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a Collection to a single Object.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class CollectionToObjectConverter implements GenericConverter {
|
||||
|
||||
private static final String DELIMITER = ",";
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public CollectionToObjectGenericConverter(GenericConversionService conversionService) {
|
||||
public CollectionToObjectConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
|
@ -38,10 +45,12 @@ final class CollectionToObjectGenericConverter implements GenericConverter {
|
|||
if (sourceCollection.size() == 0) {
|
||||
if (targetType.typeEquals(String.class)) {
|
||||
return "";
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (targetType.typeEquals(String.class)) {
|
||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||
if (sourceElementType == TypeDescriptor.NULL) {
|
||||
|
|
@ -58,7 +67,8 @@ final class CollectionToObjectGenericConverter implements GenericConverter {
|
|||
i++;
|
||||
}
|
||||
return string.toString();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetType);
|
||||
if (converter == null) {
|
||||
throw new ConverterNotFoundException(sourceElementType, targetType);
|
||||
|
|
@ -75,7 +85,8 @@ final class CollectionToObjectGenericConverter implements GenericConverter {
|
|||
}
|
||||
return string.toString();
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Object firstElement = sourceCollection.iterator().next();
|
||||
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
|
||||
if (sourceElementType == TypeDescriptor.NULL && firstElement != null) {
|
||||
|
|
@ -83,7 +94,8 @@ final class CollectionToObjectGenericConverter implements GenericConverter {
|
|||
}
|
||||
if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetType)) {
|
||||
return firstElement;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetType);
|
||||
if (converter == null) {
|
||||
throw new ConverterNotFoundException(sourceElementType, targetType);
|
||||
|
|
@ -93,4 +105,5 @@ final class CollectionToObjectGenericConverter implements GenericConverter {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -65,21 +65,21 @@ public class GenericConversionService implements ConversionService, ConverterReg
|
|||
|
||||
|
||||
public GenericConversionService() {
|
||||
addGenericConverter(Object[].class, Object[].class, new ArrayToArrayGenericConverter(this));
|
||||
addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionGenericConverter(this));
|
||||
addGenericConverter(Object[].class, Map.class, new ArrayToMapGenericConverter(this));
|
||||
addGenericConverter(Object[].class, Object.class, new ArrayToObjectGenericConverter(this));
|
||||
addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionGenericConverter(this));
|
||||
addGenericConverter(Collection.class, Object[].class, new CollectionToArrayGenericConverter(this));
|
||||
addGenericConverter(Collection.class, Map.class, new CollectionToMapGenericConverter(this));
|
||||
addGenericConverter(Collection.class, Object.class, new CollectionToObjectGenericConverter(this));
|
||||
addGenericConverter(Map.class, Map.class, new MapToMapGenericConverter(this));
|
||||
addGenericConverter(Map.class, Object[].class, new MapToArrayGenericConverter(this));
|
||||
addGenericConverter(Map.class, Collection.class, new MapToCollectionGenericConverter(this));
|
||||
addGenericConverter(Map.class, Object.class, new MapToObjectGenericConverter(this));
|
||||
addGenericConverter(Object.class, Object[].class, new ObjectToArrayGenericConverter(this));
|
||||
addGenericConverter(Object.class, Collection.class, new ObjectToCollectionGenericConverter(this));
|
||||
addGenericConverter(Object.class, Map.class, new ObjectToMapGenericConverter(this));
|
||||
addGenericConverter(Object[].class, Object[].class, new ArrayToArrayConverter(this));
|
||||
addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(this));
|
||||
addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(this));
|
||||
addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(this));
|
||||
addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(this));
|
||||
addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter(this));
|
||||
addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter(this));
|
||||
addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(this));
|
||||
addGenericConverter(Map.class, Map.class, new MapToMapConverter(this));
|
||||
addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(this));
|
||||
addGenericConverter(Map.class, Collection.class, new MapToCollectionConverter(this));
|
||||
addGenericConverter(Map.class, Object.class, new MapToObjectConverter(this));
|
||||
addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(this));
|
||||
addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(this));
|
||||
addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(this));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,31 @@
|
|||
/**
|
||||
*
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import org.springframework.core.convert.ConverterNotFoundException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import static org.springframework.core.convert.support.ConversionUtils.*;
|
||||
|
||||
/**
|
||||
* Helper for converting map entries.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
class MapEntryConverter {
|
||||
|
||||
private GenericConverter keyConverter;
|
||||
|
|
@ -22,9 +40,11 @@ class MapEntryConverter {
|
|||
|
||||
private TypeDescriptor targetValueType;
|
||||
|
||||
|
||||
public MapEntryConverter(TypeDescriptor sourceKeyType, TypeDescriptor sourceValueType, TypeDescriptor targetKeyType,
|
||||
TypeDescriptor targetValueType, boolean keysCompatible, boolean valuesCompatible,
|
||||
GenericConversionService conversionService) {
|
||||
|
||||
if (sourceKeyType != TypeDescriptor.NULL && targetKeyType != TypeDescriptor.NULL && !keysCompatible) {
|
||||
this.keyConverter = conversionService.getConverter(sourceKeyType, targetKeyType);
|
||||
if (this.keyConverter == null) {
|
||||
|
|
@ -43,10 +63,12 @@ class MapEntryConverter {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public Object convertKey(Object sourceKey) {
|
||||
if (sourceKey != null && this.keyConverter != null) {
|
||||
return invokeConverter(this.keyConverter, sourceKey, this.sourceKeyType, this.targetKeyType);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return sourceKey;
|
||||
}
|
||||
}
|
||||
|
|
@ -54,9 +76,10 @@ class MapEntryConverter {
|
|||
public Object convertValue(Object sourceValue) {
|
||||
if (sourceValue != null && this.valueConverter != null) {
|
||||
return invokeConverter(this.valueConverter, sourceValue, this.sourceValueType, this.targetValueType);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return sourceValue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,21 +13,28 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class MapToArrayGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a Map to an array.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class MapToArrayConverter implements GenericConverter {
|
||||
|
||||
private final GenericConverter mapToCollectionHelperConverter;
|
||||
|
||||
private final GenericConverter collectionToArrayHelperConverter;
|
||||
|
||||
public MapToArrayGenericConverter(GenericConversionService conversionService) {
|
||||
this.mapToCollectionHelperConverter = new MapToCollectionGenericConverter(conversionService);
|
||||
this.collectionToArrayHelperConverter = new CollectionToArrayGenericConverter(conversionService);
|
||||
public MapToArrayConverter(GenericConversionService conversionService) {
|
||||
this.mapToCollectionHelperConverter = new MapToCollectionConverter(conversionService);
|
||||
this.collectionToArrayHelperConverter = new CollectionToArrayConverter(conversionService);
|
||||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Collection;
|
||||
|
|
@ -21,14 +22,21 @@ import java.util.Map;
|
|||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class MapToCollectionGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a Map to a Collection.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class MapToCollectionConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public MapToCollectionGenericConverter(GenericConversionService conversionService) {
|
||||
public MapToCollectionConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Map sourceMap = (Map) source;
|
||||
TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor();
|
||||
|
|
@ -52,7 +60,8 @@ final class MapToCollectionGenericConverter implements GenericConverter {
|
|||
+ converter.convertValue(mapEntry.getValue());
|
||||
target.add(property);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
for (Object value : sourceMap.values()) {
|
||||
target.add(value);
|
||||
}
|
||||
|
|
@ -1,3 +1,19 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Map;
|
||||
|
|
@ -5,14 +21,21 @@ import java.util.Map;
|
|||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class MapToMapGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a source Map to a target Map type.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class MapToMapConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public MapToMapGenericConverter(GenericConversionService conversionService) {
|
||||
public MapToMapConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
Map sourceMap = (Map) source;
|
||||
TypeDescriptor targetKeyType = targetType.getMapKeyTypeDescriptor();
|
||||
|
|
@ -71,10 +94,12 @@ final class MapToMapGenericConverter implements GenericConverter {
|
|||
return new TypeDescriptor[] { TypeDescriptor.valueOf(keyType), TypeDescriptor.valueOf(valueType) };
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map compatibleMapWithoutEntryConversion(Map source, TypeDescriptor targetType) {
|
||||
if (targetType.getType().isAssignableFrom(source.getClass())) {
|
||||
return source;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Map target = CollectionFactory.createMap(targetType.getType(), source.size());
|
||||
target.putAll(source);
|
||||
return target;
|
||||
|
|
@ -13,19 +13,26 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class MapToObjectGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a Ma to a single Object.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class MapToObjectConverter implements GenericConverter {
|
||||
|
||||
private static final String DELIMITER = " ";
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public MapToObjectGenericConverter(GenericConversionService conversionService) {
|
||||
public MapToObjectConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
|
@ -34,10 +41,12 @@ final class MapToObjectGenericConverter implements GenericConverter {
|
|||
if (sourceMap.size() == 0) {
|
||||
if (targetType.typeEquals(String.class)) {
|
||||
return "";
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (targetType.typeEquals(String.class)) {
|
||||
TypeDescriptor sourceKeyType = sourceType.getMapKeyTypeDescriptor();
|
||||
TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor();
|
||||
|
|
@ -62,7 +71,8 @@ final class MapToObjectGenericConverter implements GenericConverter {
|
|||
i++;
|
||||
}
|
||||
return string.toString();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
MapEntryConverter converter = new MapEntryConverter(sourceKeyType, sourceValueType, targetType, targetType,
|
||||
keysCompatible, valuesCompatible, conversionService);
|
||||
StringBuilder string = new StringBuilder();
|
||||
|
|
@ -80,7 +90,8 @@ final class MapToObjectGenericConverter implements GenericConverter {
|
|||
}
|
||||
return string.toString();
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
TypeDescriptor sourceValueType = sourceType.getMapValueTypeDescriptor();
|
||||
boolean valuesCompatible = false;
|
||||
if (sourceValueType == TypeDescriptor.NULL || sourceValueType.isAssignableTo(targetType)) {
|
||||
|
|
@ -88,7 +99,8 @@ final class MapToObjectGenericConverter implements GenericConverter {
|
|||
}
|
||||
if (valuesCompatible) {
|
||||
return sourceMap.values().iterator().next();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
MapEntryConverter converter = new MapEntryConverter(sourceValueType, sourceValueType, targetType, targetType,
|
||||
true, valuesCompatible, conversionService);
|
||||
Object value = sourceMap.values().iterator().next();
|
||||
|
|
@ -97,4 +109,5 @@ final class MapToObjectGenericConverter implements GenericConverter {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
|
||||
|
|
@ -23,11 +24,17 @@ import org.springframework.core.convert.ConverterNotFoundException;
|
|||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
final class ObjectToArrayGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a single Object to an array.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class ObjectToArrayConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public ObjectToArrayGenericConverter(GenericConversionService conversionService) {
|
||||
public ObjectToArrayConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
|
@ -38,7 +45,8 @@ final class ObjectToArrayGenericConverter implements GenericConverter {
|
|||
String[] fields = StringUtils.commaDelimitedListToStringArray(string);
|
||||
if (sourceType.isAssignableTo(targetElementType)) {
|
||||
return fields;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Object target = Array.newInstance(targetElementType.getType(), fields.length);
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType);
|
||||
if (converter == null) {
|
||||
|
|
@ -49,11 +57,13 @@ final class ObjectToArrayGenericConverter implements GenericConverter {
|
|||
}
|
||||
return target;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Object target = Array.newInstance(targetElementType.getType(), 1);
|
||||
if (sourceType.isAssignableTo(targetElementType)) {
|
||||
Array.set(target, 0, source);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType);
|
||||
if (converter == null) {
|
||||
throw new ConverterNotFoundException(sourceType, targetElementType);
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
|
||||
|
|
@ -24,11 +25,17 @@ import org.springframework.core.convert.ConverterNotFoundException;
|
|||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
final class ObjectToCollectionGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a single Object to a Collection.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class ObjectToCollectionConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
public ObjectToCollectionGenericConverter(GenericConversionService conversionService) {
|
||||
public ObjectToCollectionConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
|
|
@ -42,7 +49,8 @@ final class ObjectToCollectionGenericConverter implements GenericConverter {
|
|||
for (int i = 0; i < fields.length; i++) {
|
||||
target.add(fields[i]);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType);
|
||||
if (converter == null) {
|
||||
throw new ConverterNotFoundException(sourceType, targetElementType);
|
||||
|
|
@ -55,11 +63,13 @@ final class ObjectToCollectionGenericConverter implements GenericConverter {
|
|||
}
|
||||
return target;
|
||||
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Collection target = CollectionFactory.createCollection(targetType.getType(), 1);
|
||||
if (targetElementType == TypeDescriptor.NULL || sourceType.isAssignableTo(targetElementType)) {
|
||||
target.add(source);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType);
|
||||
if (converter == null) {
|
||||
throw new ConverterNotFoundException(sourceType, targetElementType);
|
||||
|
|
@ -70,4 +80,4 @@ final class ObjectToCollectionGenericConverter implements GenericConverter {
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Map;
|
||||
|
|
@ -20,23 +21,31 @@ import java.util.Map;
|
|||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
final class ObjectToMapGenericConverter implements GenericConverter {
|
||||
/**
|
||||
* Converts from a single Object to a Map.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class ObjectToMapConverter implements GenericConverter {
|
||||
|
||||
private final GenericConversionService conversionService;
|
||||
|
||||
private final ArrayToMapGenericConverter helperConverter;
|
||||
private final ArrayToMapConverter helperConverter;
|
||||
|
||||
public ObjectToMapGenericConverter(GenericConversionService conversionService) {
|
||||
public ObjectToMapConverter(GenericConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
this.helperConverter = new ArrayToMapGenericConverter(conversionService);
|
||||
this.helperConverter = new ArrayToMapConverter(conversionService);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
if (sourceType.typeEquals(String.class)) {
|
||||
String string = (String) source;
|
||||
String[] properties = string.split(" ");
|
||||
return this.helperConverter.convert(properties, TypeDescriptor.valueOf(String[].class), targetType);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Map target = CollectionFactory.createMap(targetType.getType(), 1);
|
||||
TypeDescriptor targetKeyType = targetType.getMapKeyTypeDescriptor();
|
||||
TypeDescriptor targetValueType = targetType.getMapValueTypeDescriptor();
|
||||
|
|
@ -50,7 +59,8 @@ final class ObjectToMapGenericConverter implements GenericConverter {
|
|||
}
|
||||
if (keysCompatible && valuesCompatible) {
|
||||
target.put(source, source);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
MapEntryConverter converter = new MapEntryConverter(sourceType, sourceType, targetKeyType,
|
||||
targetValueType, keysCompatible, valuesCompatible, conversionService);
|
||||
Object key = converter.convertKey(source);
|
||||
|
|
@ -61,4 +71,4 @@ final class ObjectToMapGenericConverter implements GenericConverter {
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue