Example usage for org.springframework.core CollectionFactory createMap

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

Introduction

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

Prototype

public static <K, V> Map<K, V> createMap(Class<?> mapType, int capacity) 

Source Link

Document

Create the most appropriate map for the given map type.

Usage

From source file:org.gvnix.web.json.DataBinderMappingJackson2HttpMessageConverter.java

/**
 * Before call to {@link ObjectMapper#readValue(java.io.InputStream, Class)}
 * creates a {@link ServletRequestDataBinder} and put it to current Thread
 * in order to be used by the {@link DataBinderDeserializer}.
 * <p/>/*ww  w  .j  ava2  s .  co  m*/
 * Ref: <a href=
 * "http://java.dzone.com/articles/java-thread-local-%E2%80%93-how-use">When
 * to use Thread Local?</a>
 * 
 * @param javaType
 * @param inputMessage
 * @return
 */
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
    try {
        Object target = null;
        String objectName = null;

        // CRear el DataBinder con un target object en funcion del javaType,
        // ponerlo en el thread local
        Class<?> clazz = javaType.getRawClass();
        if (Collection.class.isAssignableFrom(clazz)) {
            Class<?> contentClazz = javaType.getContentType().getRawClass();
            target = new DataBinderList<Object>(contentClazz);
            objectName = "list";
        } else if (Map.class.isAssignableFrom(clazz)) {
            // TODO Class<?> contentClazz =
            // javaType.getContentType().getRawClass();
            target = CollectionFactory.createMap(clazz, 0);
            objectName = "map";
        } else {
            target = BeanUtils.instantiateClass(clazz);
            objectName = "bean";
        }

        WebDataBinder binder = new ServletRequestDataBinder(target, objectName);
        binder.setConversionService(this.conversionService);
        binder.setAutoGrowNestedPaths(true);
        binder.setValidator(validator);

        ThreadLocalUtil.setThreadVariable(BindingResult.MODEL_KEY_PREFIX.concat("JSON_DataBinder"), binder);

        Object value = getObjectMapper().readValue(inputMessage.getBody(), javaType);

        return value;
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: ".concat(ex.getMessage()), ex);
    }
}

From source file:org.springframework.beans.factory.support.ConstructorResolver.java

/**
 * Template method for resolving the specified argument which is supposed to be autowired.
 */// w  w  w . ja  v a2s .  co  m
@Nullable
protected Object resolveAutowiredArgument(MethodParameter param, String beanName,
        @Nullable Set<String> autowiredBeanNames, TypeConverter typeConverter, boolean fallback) {

    Class<?> paramType = param.getParameterType();
    if (InjectionPoint.class.isAssignableFrom(paramType)) {
        InjectionPoint injectionPoint = currentInjectionPoint.get();
        if (injectionPoint == null) {
            throw new IllegalStateException("No current InjectionPoint available for " + param);
        }
        return injectionPoint;
    }
    try {
        return this.beanFactory.resolveDependency(new DependencyDescriptor(param, true), beanName,
                autowiredBeanNames, typeConverter);
    } catch (NoUniqueBeanDefinitionException ex) {
        throw ex;
    } catch (NoSuchBeanDefinitionException ex) {
        if (fallback) {
            // Single constructor or factory method -> let's return an empty array/collection
            // for e.g. a vararg or a non-null List/Set/Map parameter.
            if (paramType.isArray()) {
                return Array.newInstance(paramType.getComponentType(), 0);
            } else if (CollectionFactory.isApproximableCollectionType(paramType)) {
                return CollectionFactory.createCollection(paramType, 0);
            } else if (CollectionFactory.isApproximableMapType(paramType)) {
                return CollectionFactory.createMap(paramType, 0);
            }
        }
        throw ex;
    }
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

/**
 * Reads the given {@link DBObject} into a {@link Map}. will recursively resolve nested {@link Map}s as well.
 * /*from   w ww.j a  v  a2s  .  c o m*/
 * @param type the {@link Map} {@link TypeInformation} to be used to unmarshall this {@link DBObject}.
 * @param dbObject
 * @return
 */
@SuppressWarnings("unchecked")
protected Map<Object, Object> readMap(TypeInformation<?> type, DBObject dbObject) {

    Assert.notNull(dbObject);

    Class<?> mapType = typeMapper.readType(dbObject, type).getType();
    Map<Object, Object> map = CollectionFactory.createMap(mapType, dbObject.keySet().size());
    Map<String, Object> sourceMap = dbObject.toMap();

    for (Entry<String, Object> entry : sourceMap.entrySet()) {
        if (typeMapper.isTypeKey(entry.getKey())) {
            continue;
        }

        Object key = potentiallyUnescapeMapKey(entry.getKey());

        TypeInformation<?> keyTypeInformation = type.getComponentType();
        if (keyTypeInformation != null) {
            Class<?> keyType = keyTypeInformation.getType();
            key = conversionService.convert(key, keyType);
        }

        Object value = entry.getValue();
        TypeInformation<?> valueType = type.getMapValueType();

        if (value instanceof DBObject) {
            map.put(key, read(valueType, (DBObject) value));
        } else {
            Class<?> valueClass = valueType == null ? null : valueType.getType();
            map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass));
        }
    }

    return map;
}