Example usage for org.springframework.core CollectionFactory createApproximateCollection

List of usage examples for org.springframework.core CollectionFactory createApproximateCollection

Introduction

In this page you can find the example usage for org.springframework.core CollectionFactory createApproximateCollection.

Prototype

@SuppressWarnings({ "rawtypes", "unchecked", "cast" })
public static <E> Collection<E> createApproximateCollection(@Nullable Object collection, int capacity) 

Source Link

Document

Create the most approximate collection for the given collection.

Usage

From source file:net.sf.juffrou.reflect.JuffrouTypeConverterDelegate.java

@SuppressWarnings("unchecked")
private Collection convertToTypedCollection(Collection original, String propertyName, Class requiredType,
        TypeDescriptor typeDescriptor) {

    if (!Collection.class.isAssignableFrom(requiredType)) {
        return original;
    }//from   w  ww  . j  a v  a2  s.co m

    boolean approximable = CollectionFactory.isApproximableCollectionType(requiredType);
    if (!approximable && !canCreateCopy(requiredType)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Custom Collection type [" + original.getClass().getName()
                    + "] does not allow for creating a copy - injecting original Collection as-is");
        }
        return original;
    }

    boolean originalAllowed = requiredType.isInstance(original);
    typeDescriptor = typeDescriptor.narrow(original);
    TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor();
    if (elementType == null && originalAllowed
            && !this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
        return original;
    }

    Iterator it;
    try {
        it = original.iterator();
        if (it == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Collection of type [" + original.getClass().getName()
                        + "] returned null Iterator - injecting original Collection as-is");
            }
            return original;
        }
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot access Collection of type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    Collection convertedCopy;
    try {
        if (approximable) {
            convertedCopy = CollectionFactory.createApproximateCollection(original, original.size());
        } else {
            convertedCopy = (Collection) requiredType.newInstance();
        }
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot create copy of Collection type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    int i = 0;
    for (; it.hasNext(); i++) {
        Object element = it.next();
        String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
        Object convertedElement = convertIfNecessary(indexedPropertyName, null, element,
                (elementType != null ? elementType.getType() : null), elementType);
        try {
            convertedCopy.add(convertedElement);
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Collection type [" + original.getClass().getName()
                        + "] seems to be read-only - injecting original Collection as-is: " + ex);
            }
            return original;
        }
        originalAllowed = originalAllowed && (element == convertedElement);
    }
    return (originalAllowed ? original : convertedCopy);
}

From source file:org.springframework.beans.TypeConverterDelegate.java

@SuppressWarnings("unchecked")
private Collection<?> convertToTypedCollection(Collection<?> original, @Nullable String propertyName,
        Class<?> requiredType, @Nullable TypeDescriptor typeDescriptor) {

    if (!Collection.class.isAssignableFrom(requiredType)) {
        return original;
    }/* w  w w . j a  v a2  s  . c  o m*/

    boolean approximable = CollectionFactory.isApproximableCollectionType(requiredType);
    if (!approximable && !canCreateCopy(requiredType)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Custom Collection type [" + original.getClass().getName()
                    + "] does not allow for creating a copy - injecting original Collection as-is");
        }
        return original;
    }

    boolean originalAllowed = requiredType.isInstance(original);
    TypeDescriptor elementType = (typeDescriptor != null ? typeDescriptor.getElementTypeDescriptor() : null);
    if (elementType == null && originalAllowed
            && !this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
        return original;
    }

    Iterator<?> it;
    try {
        it = original.iterator();
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot access Collection of type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    Collection<Object> convertedCopy;
    try {
        if (approximable) {
            convertedCopy = CollectionFactory.createApproximateCollection(original, original.size());
        } else {
            convertedCopy = (Collection<Object>) ReflectionUtils.accessibleConstructor(requiredType)
                    .newInstance();
        }
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot create copy of Collection type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    int i = 0;
    for (; it.hasNext(); i++) {
        Object element = it.next();
        String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
        Object convertedElement = convertIfNecessary(indexedPropertyName, null, element,
                (elementType != null ? elementType.getType() : null), elementType);
        try {
            convertedCopy.add(convertedElement);
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Collection type [" + original.getClass().getName()
                        + "] seems to be read-only - injecting original Collection as-is: " + ex);
            }
            return original;
        }
        originalAllowed = originalAllowed && (element == convertedElement);
    }
    return (originalAllowed ? original : convertedCopy);
}

From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java

protected Collection convertToTypedCollection(Collection original, String propertyName,
        MethodParameter methodParam) {/*from   w  w w.  ja v a  2 s.  c  om*/

    Class elementType = null;
    if (methodParam != null && JdkVersion.isAtLeastJava15()) {
        elementType = GenericCollectionTypeResolver.getCollectionParameterType(methodParam);
    }
    if (elementType == null && !this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
        return original;
    }

    Collection convertedCopy = null;
    Iterator it = null;
    try {
        it = original.iterator();
        if (it == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Collection of type [" + original.getClass().getName()
                        + "] returned null Iterator - injecting original Collection as-is");
            }
            return original;
        }
        convertedCopy = CollectionFactory.createApproximateCollection(original, original.size());
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot access Collection of type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is", ex);
        }
        return original;
    }
    boolean actuallyConverted = false;
    int i = 0;
    for (; it.hasNext(); i++) {
        Object element = it.next();
        String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
        if (methodParam != null) {
            methodParam.increaseNestingLevel();
        }
        Object convertedElement = convertIfNecessary(indexedPropertyName, null, element, elementType, null,
                methodParam);
        if (methodParam != null) {
            methodParam.decreaseNestingLevel();
        }
        convertedCopy.add(convertedElement);
        actuallyConverted = actuallyConverted || (element != convertedElement);
    }
    return (actuallyConverted ? convertedCopy : original);
}