Example usage for org.springframework.core.convert TypeDescriptor isCollection

List of usage examples for org.springframework.core.convert TypeDescriptor isCollection

Introduction

In this page you can find the example usage for org.springframework.core.convert TypeDescriptor isCollection.

Prototype

public boolean isCollection() 

Source Link

Document

Is this type a Collection type?

Usage

From source file:ch.rasc.wampspring.method.MethodParameterConverter.java

@SuppressWarnings("unchecked")
private Object convertListElements(TypeDescriptor td, Object convertedValue) {
    if (List.class.isAssignableFrom(convertedValue.getClass()) && td.isCollection()
            && td.getElementTypeDescriptor() != null) {
        Class<?> elementType = td.getElementTypeDescriptor().getType();

        Collection<Object> convertedList = new ArrayList<>();
        for (Object record : (List<Object>) convertedValue) {
            Object convertedObject = this.objectMapper.convertValue(record, elementType);
            convertedList.add(convertedObject);
        }//from w ww. j a  v a  2s.c  o  m
        return convertedList;

    }
    return convertedValue;
}

From source file:cz.jirutka.validator.spring.support.RelaxedBooleanTypeConverterDecorator.java

public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
    return targetType.isAssignableTo(BOOLEAN_TYPE)
            && (sourceType.isAssignableTo(NUMBER_TYPE) || sourceType.isCollection() || sourceType.isArray())
            || decorated.canConvert(sourceType, targetType);
}

From source file:cz.jirutka.validator.spring.support.RelaxedBooleanTypeConverterDecorator.java

public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (targetType.isAssignableTo(BOOLEAN_TYPE)) {
        if (value instanceof Number) {
            return ((Number) value).intValue() != 0;
        }//from w ww.j  av a2 s.  c om
        if (sourceType.isCollection()) {
            return !((Collection) value).isEmpty();
        }
        if (sourceType.isArray()) {
            return ((Object[]) value).length != 0;
        }
        return value;

    } else {
        return decorated.convertValue(value, sourceType, targetType);
    }
}

From source file:ch.rasc.wampspring.method.MethodParameterConverter.java

public Object convert(MethodParameter parameter, Object argument) {
    if (argument == null) {
        if (parameter.getParameterType().getName().equals("java.util.Optional")) {
            return OptionalUnwrapper.empty();
        }//w w  w  .j ava  2  s  .com

        return null;
    }

    Class<?> sourceClass = argument.getClass();
    Class<?> targetClass = parameter.getParameterType();

    TypeDescriptor td = new TypeDescriptor(parameter);

    if (targetClass.isAssignableFrom(sourceClass)) {
        return convertListElements(td, argument);
    }

    if (this.conversionService.canConvert(sourceClass, targetClass)) {
        try {
            return convertListElements(td, this.conversionService.convert(argument, targetClass));
        } catch (Exception e) {

            TypeFactory typeFactory = this.objectMapper.getTypeFactory();
            if (td.isCollection()) {
                JavaType type = CollectionType.construct(td.getType(),
                        typeFactory.constructType(td.getElementTypeDescriptor().getType()));
                return this.objectMapper.convertValue(argument, type);
            } else if (td.isArray()) {
                JavaType type = typeFactory.constructArrayType(td.getElementTypeDescriptor().getType());
                return this.objectMapper.convertValue(argument, type);
            }

            throw e;
        }
    }
    return this.objectMapper.convertValue(argument, targetClass);
}