Example usage for org.springframework.data.util TypeInformation getMapValueType

List of usage examples for org.springframework.data.util TypeInformation getMapValueType

Introduction

In this page you can find the example usage for org.springframework.data.util TypeInformation getMapValueType.

Prototype

@Nullable
TypeInformation<?> getMapValueType();

Source Link

Document

Will return the type of the value in case the underlying type is a java.util.Map .

Usage

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

/**
 * Writes the given {@link Map} to the given {@link DBObject} considering the given {@link TypeInformation}.
 * //from w  w w.j a  v a2 s. c om
 * @param obj must not be {@literal null}.
 * @param dbo must not be {@literal null}.
 * @param propertyType must not be {@literal null}.
 * @return
 */
protected DBObject writeMapInternal(Map<Object, Object> obj, DBObject dbo, TypeInformation<?> propertyType) {

    for (Map.Entry<Object, Object> entry : obj.entrySet()) {
        Object key = entry.getKey();
        Object val = entry.getValue();
        if (conversions.isSimpleType(key.getClass())) {
            // Don't use conversion service here as removal of ObjectToString converter results in some primitive types not
            // being convertable
            String simpleKey = potentiallyEscapeMapKey(key.toString());
            if (val == null || conversions.isSimpleType(val.getClass())) {
                writeSimpleInternal(val, dbo, simpleKey);
            } else if (val instanceof Collection || val.getClass().isArray()) {
                dbo.put(simpleKey, writeCollectionInternal(asCollection(val), propertyType.getMapValueType(),
                        new BasicDBList()));
            } else {
                DBObject newDbo = new BasicDBObject();
                TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType()
                        : ClassTypeInformation.OBJECT;
                writeInternal(val, newDbo, valueTypeInfo);
                dbo.put(simpleKey, newDbo);
            }
        } else {
            throw new MappingException("Cannot use a complex object as a key value.");
        }
    }

    return dbo;
}

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 w  w .  ja  va2 s .  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;
}