Java Code Examples for org.springframework.core.CollectionFactory#createCollection()

The following examples show how to use org.springframework.core.CollectionFactory#createCollection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObjectToCollectionConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}

	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), 1);

	if (elementDesc == null || elementDesc.isCollection()) {
		target.add(source);
	}
	else {
		Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
		target.add(singleElement);
	}
	return target;
}
 
Example 2
Source File: ObjectToCollectionConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}

	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), 1);

	if (elementDesc == null || elementDesc.isCollection()) {
		target.add(source);
	}
	else {
		Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
		target.add(singleElement);
	}
	return target;
}
 
Example 3
Source File: ObjectToCollectionConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}

	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), 1);

	if (elementDesc == null || elementDesc.isCollection()) {
		target.add(source);
	}
	else {
		Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
		target.add(singleElement);
	}
	return target;
}
 
Example 4
Source File: ObjectToCollectionConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}

	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), 1);

	if (elementDesc == null || elementDesc.isCollection()) {
		target.add(source);
	}
	else {
		Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
		target.add(singleElement);
	}
	return target;
}
 
Example 5
Source File: CollectionBinder.java    From spring-cloud-gray with Apache License 2.0 6 votes vote down vote up
@Override
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target,
                               AggregateElementBinder elementBinder) {
    Class<?> collectionType = (target.getValue() != null) ? List.class
            : target.getType().resolve(Object.class);
    ResolvableType aggregateType = ResolvableType.forClassWithGenerics(List.class,
            target.getType().asCollection().getGenerics());
    ResolvableType elementType = target.getType().asCollection().getGeneric();
    IndexedCollectionSupplier result = new IndexedCollectionSupplier(
            () -> CollectionFactory.createCollection(collectionType,
                    elementType.resolve(), 0));
    bindIndexed(name, target, elementBinder, aggregateType, elementType, result);
    if (result.wasSupplied()) {
        return result.get();
    }
    return null;
}
 
Example 6
Source File: CollectionToCollectionConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	Collection<?> sourceCollection = (Collection<?>) source;

	// Shortcut if possible...
	boolean copyRequired = !targetType.getType().isInstance(source);
	if (!copyRequired && sourceCollection.isEmpty()) {
		return source;
	}
	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	if (elementDesc == null && !copyRequired) {
		return source;
	}

	// At this point, we need a collection copy in any case, even if just for finding out about element copies...
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());

	if (elementDesc == null) {
		target.addAll(sourceCollection);
	}
	else {
		for (Object sourceElement : sourceCollection) {
			Object targetElement = this.conversionService.convert(sourceElement,
					sourceType.elementTypeDescriptor(sourceElement), elementDesc);
			target.add(targetElement);
			if (sourceElement != targetElement) {
				copyRequired = true;
			}
		}
	}

	return (copyRequired ? target : source);
}
 
Example 7
Source File: MBeanClientInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType, Class<?> elementType)
		throws NoSuchMethodException {

	Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType());
	Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array));
	for (int i = 0; i < array.length; i++) {
		resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
	}
	return resultColl;
}
 
Example 8
Source File: CollectionToCollectionConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	Collection<?> sourceCollection = (Collection<?>) source;

	// Shortcut if possible...
	boolean copyRequired = !targetType.getType().isInstance(source);
	if (!copyRequired && sourceCollection.isEmpty()) {
		return source;
	}
	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	if (elementDesc == null && !copyRequired) {
		return source;
	}

	// At this point, we need a collection copy in any case, even if just for finding out about element copies...
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());

	if (elementDesc == null) {
		target.addAll(sourceCollection);
	}
	else {
		for (Object sourceElement : sourceCollection) {
			Object targetElement = this.conversionService.convert(sourceElement,
					sourceType.elementTypeDescriptor(sourceElement), elementDesc);
			target.add(targetElement);
			if (sourceElement != targetElement) {
				copyRequired = true;
			}
		}
	}

	return (copyRequired ? target : source);
}
 
Example 9
Source File: AbstractNestablePropertyAccessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
	try {
		if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			// TODO - only handles 2-dimensional arrays
			if (componentType.isArray()) {
				Object array = Array.newInstance(componentType, 1);
				Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
				return array;
			}
			else {
				return Array.newInstance(componentType, 0);
			}
		}
		else if (Collection.class.isAssignableFrom(type)) {
			TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
			return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
		}
		else if (Map.class.isAssignableFrom(type)) {
			TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
			return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
		}
		else {
			return BeanUtils.instantiate(type);
		}
	}
	catch (Throwable ex) {
		throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
				"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path", ex);
	}
}
 
Example 10
Source File: MBeanClientInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType, Class<?> elementType)
		throws NoSuchMethodException {

	Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType());
	Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array));
	for (int i = 0; i < array.length; i++) {
		resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
	}
	return resultColl;
}
 
Example 11
Source File: WebDataBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine an empty value for the specified field.
 * <p>The default implementation returns:
 * <ul>
 * <li>{@code Boolean.FALSE} for boolean fields
 * <li>an empty array for array types
 * <li>Collection implementations for Collection types
 * <li>Map implementations for Map types
 * <li>else, {@code null} is used as default
 * </ul>
 * @param field the name of the field
 * @param fieldType the type of the field
 * @return the empty value (for most fields: null)
 */
protected Object getEmptyValue(String field, Class<?> fieldType) {
	if (fieldType != null) {
		try {
			if (boolean.class == fieldType || Boolean.class == fieldType) {
				// Special handling of boolean property.
				return Boolean.FALSE;
			}
			else if (fieldType.isArray()) {
				// Special handling of array property.
				return Array.newInstance(fieldType.getComponentType(), 0);
			}
			else if (Collection.class.isAssignableFrom(fieldType)) {
				return CollectionFactory.createCollection(fieldType, 0);
			}
			else if (Map.class.isAssignableFrom(fieldType)) {
				return CollectionFactory.createMap(fieldType, 0);
			}
		}
		catch (IllegalArgumentException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Failed to create default value - falling back to null: " + ex.getMessage());
			}
		}
	}
	// Default value: null.
	return null;
}
 
Example 12
Source File: AbstractNestablePropertyAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
	try {
		if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			// TODO - only handles 2-dimensional arrays
			if (componentType.isArray()) {
				Object array = Array.newInstance(componentType, 1);
				Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
				return array;
			}
			else {
				return Array.newInstance(componentType, 0);
			}
		}
		else if (Collection.class.isAssignableFrom(type)) {
			TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
			return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
		}
		else if (Map.class.isAssignableFrom(type)) {
			TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
			return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
		}
		else {
			return BeanUtils.instantiate(type);
		}
	}
	catch (Exception ex) {
		// TODO: Root cause exception context is lost here; just exception message preserved.
		// Should we throw another exception type that preserves context instead?
		throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
				"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex);
	}
}
 
Example 13
Source File: AbstractNestablePropertyAccessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
	try {
		if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			// TODO - only handles 2-dimensional arrays
			if (componentType.isArray()) {
				Object array = Array.newInstance(componentType, 1);
				Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
				return array;
			}
			else {
				return Array.newInstance(componentType, 0);
			}
		}
		else if (Collection.class.isAssignableFrom(type)) {
			TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
			return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
		}
		else if (Map.class.isAssignableFrom(type)) {
			TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
			return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
		}
		else {
			Constructor<?> ctor = type.getDeclaredConstructor();
			if (Modifier.isPrivate(ctor.getModifiers())) {
				throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor);
			}
			return BeanUtils.instantiateClass(ctor);
		}
	}
	catch (Throwable ex) {
		throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
				"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path", ex);
	}
}
 
Example 14
Source File: CollectionToCollectionConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	Collection<?> sourceCollection = (Collection<?>) source;

	// Shortcut if possible...
	boolean copyRequired = !targetType.getType().isInstance(source);
	if (!copyRequired && sourceCollection.isEmpty()) {
		return source;
	}
	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	if (elementDesc == null && !copyRequired) {
		return source;
	}

	// At this point, we need a collection copy in any case, even if just for finding out about element copies...
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());

	if (elementDesc == null) {
		target.addAll(sourceCollection);
	}
	else {
		for (Object sourceElement : sourceCollection) {
			Object targetElement = this.conversionService.convert(sourceElement,
					sourceType.elementTypeDescriptor(sourceElement), elementDesc);
			target.add(targetElement);
			if (sourceElement != targetElement) {
				copyRequired = true;
			}
		}
	}

	return (copyRequired ? target : source);
}
 
Example 15
Source File: MBeanClientInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Collection<?> convertDataArrayToTargetCollection(Object[] array, Class<?> collectionType, Class<?> elementType)
		throws NoSuchMethodException {

	Method fromMethod = elementType.getMethod("from", array.getClass().getComponentType());
	Collection<Object> resultColl = CollectionFactory.createCollection(collectionType, Array.getLength(array));
	for (int i = 0; i < array.length; i++) {
		resultColl.add(ReflectionUtils.invokeMethod(fromMethod, null, array[i]));
	}
	return resultColl;
}
 
Example 16
Source File: AbstractNestablePropertyAccessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
	try {
		if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			// TODO - only handles 2-dimensional arrays
			if (componentType.isArray()) {
				Object array = Array.newInstance(componentType, 1);
				Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
				return array;
			}
			else {
				return Array.newInstance(componentType, 0);
			}
		}
		else if (Collection.class.isAssignableFrom(type)) {
			TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
			return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
		}
		else if (Map.class.isAssignableFrom(type)) {
			TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
			return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
		}
		else {
			Constructor<?> ctor = type.getDeclaredConstructor();
			if (Modifier.isPrivate(ctor.getModifiers())) {
				throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor);
			}
			return BeanUtils.instantiateClass(ctor);
		}
	}
	catch (Throwable ex) {
		throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
				"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path", ex);
	}
}
 
Example 17
Source File: WebDataBinder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine an empty value for the specified field.
 * <p>The default implementation returns:
 * <ul>
 * <li>{@code Boolean.FALSE} for boolean fields
 * <li>an empty array for array types
 * <li>Collection implementations for Collection types
 * <li>Map implementations for Map types
 * <li>else, {@code null} is used as default
 * </ul>
 * @param fieldType the type of the field
 * @return the empty value (for most fields: {@code null})
 * @since 5.0
 */
@Nullable
public Object getEmptyValue(Class<?> fieldType) {
	try {
		if (boolean.class == fieldType || Boolean.class == fieldType) {
			// Special handling of boolean property.
			return Boolean.FALSE;
		}
		else if (fieldType.isArray()) {
			// Special handling of array property.
			return Array.newInstance(fieldType.getComponentType(), 0);
		}
		else if (Collection.class.isAssignableFrom(fieldType)) {
			return CollectionFactory.createCollection(fieldType, 0);
		}
		else if (Map.class.isAssignableFrom(fieldType)) {
			return CollectionFactory.createMap(fieldType, 0);
		}
	}
	catch (IllegalArgumentException ex) {
		if (logger.isDebugEnabled()) {
			logger.debug("Failed to create default value - falling back to null: " + ex.getMessage());
		}
	}
	// Default value: null.
	return null;
}
 
Example 18
Source File: CollectionToCollectionConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	Collection<?> sourceCollection = (Collection<?>) source;

	// Shortcut if possible...
	boolean copyRequired = !targetType.getType().isInstance(source);
	if (!copyRequired && sourceCollection.isEmpty()) {
		return source;
	}
	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	if (elementDesc == null && !copyRequired) {
		return source;
	}

	// At this point, we need a collection copy in any case, even if just for finding out about element copies...
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());

	if (elementDesc == null) {
		target.addAll(sourceCollection);
	}
	else {
		for (Object sourceElement : sourceCollection) {
			Object targetElement = this.conversionService.convert(sourceElement,
					sourceType.elementTypeDescriptor(sourceElement), elementDesc);
			target.add(targetElement);
			if (sourceElement != targetElement) {
				copyRequired = true;
			}
		}
	}

	return (copyRequired ? target : source);
}
 
Example 19
Source File: CollectionBinder.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private Collection<Object> createNewCollection(Collection<Object> collection) {
    Collection<Object> result = CollectionFactory
            .createCollection(collection.getClass(), collection.size());
    result.addAll(collection);
    return result;
}
 
Example 20
Source File: DelimitedStringToCollectionConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private Collection<Object> createCollection(TypeDescriptor targetType,
                                            TypeDescriptor elementDescriptor, int length) {
    return CollectionFactory.createCollection(targetType.getType(),
            (elementDescriptor != null) ? elementDescriptor.getType() : null, length);
}