Java Code Examples for org.springframework.data.util.TypeInformation#getMapValueType()

The following examples show how to use org.springframework.data.util.TypeInformation#getMapValueType() . 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: MappingVaultConverter.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given {@link Map} to the given {@link Map} considering the given
 * {@link TypeInformation}.
 * @param obj must not be {@literal null}.
 * @param bson must not be {@literal null}.
 * @param propertyType must not be {@literal null}.
 * @return the converted {@link Map}.
 */
protected Map<String, Object> writeMapInternal(Map<Object, Object> obj, Map<String, Object> bson,
		TypeInformation<?> propertyType) {

	for (Entry<Object, Object> entry : obj.entrySet()) {

		Object key = entry.getKey();
		Object val = entry.getValue();

		if (this.conversions.isSimpleType(key.getClass())) {

			String simpleKey = key.toString();
			if (val == null || this.conversions.isSimpleType(val.getClass())) {
				bson.put(simpleKey, val);
			}
			else if (val instanceof Collection || val.getClass().isArray()) {

				bson.put(simpleKey, writeCollectionInternal(asCollection(val), propertyType.getMapValueType(),
						new ArrayList<>()));
			}
			else {
				SecretDocumentAccessor nested = new SecretDocumentAccessor(new SecretDocument());
				TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType()
						: ClassTypeInformation.OBJECT;
				writeInternal(val, nested, valueTypeInfo);
				bson.put(simpleKey, nested.getBody());
			}
		}
		else {
			throw new MappingException("Cannot use a complex object as a key value.");
		}
	}

	return bson;
}
 
Example 2
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively parses the a map from the source document.
 *
 * @param type the type information for the document.
 * @param source the source document.
 * @param parent the optional parent.
 * @return the recursively parsed map.
 */
protected Map<Object, Object> readMap(final TypeInformation<?> type, final CrateDocument source, final Object parent) {
	
	notNull(source);
	
    Class<?> mapType = typeMapper.readType(source, type).getType();
    Map<Object, Object> map = createMap(mapType, source.keySet().size());
    
    for(Map.Entry<String, Object> entry : source.entrySet()) {
    	
    	Object key = entry.getKey();
    	Object value = entry.getValue();

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

	    TypeInformation<?> valueType = type.getMapValueType();
	    
	    if(value instanceof CrateDocument) {
	    	map.put(key, read(valueType, (CrateDocument) value, parent));
	    }else if(value instanceof CrateArray) {
	    	map.put(key, readCollection(valueType, (CrateArray) value, parent));
	    }else {
	    	Class<?> valueClass = valueType == null ? null : valueType.getType();
	        map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass));
	    }
    }
    
    return map;
}
 
Example 3
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to write the map into the crate document.
 * 
 * @param source the source object.
 * @param sink the target document.
 * @param type the type information for the document.
 * @return the written crate document.
 */
private CrateDocument writeMapInternal(final Map<Object, Object> source, final CrateDocument sink, final TypeInformation<?> type) {

	for(Map.Entry<Object, Object> entry : source.entrySet()) {
		
		Object key = entry.getKey();
		Object val = entry.getValue();
		
		if(conversions.isSimpleType(key.getClass())) {
			
			String simpleKey = key.toString();
			
			if(val == null || (conversions.isSimpleType(val.getClass()) && !val.getClass().isArray())) {
				writeSimpleInternal(val, sink, simpleKey);
			}else if(val instanceof Collection || val.getClass().isArray()) {
				sink.put(simpleKey, writeCollectionInternal(asCollection(val), new CrateArray(), type.getMapValueType()));
			}else {
				CrateDocument document = new CrateDocument();
				TypeInformation<?> valueTypeInfo = type.isMap() ? type.getMapValueType() : OBJECT;
				writeInternal(val, document, valueTypeInfo);
				sink.put(simpleKey, document);
			}
		} else {
			throw new MappingException("Cannot use a complex object as a key value.");
		}
	}
	
	return sink;
}
 
Example 4
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
private TypeInformation<?> getNonNullMapValueType(final TypeInformation<?> type) {
	final TypeInformation<?> valueType = type.getMapValueType();
	return valueType != null ? valueType : ClassTypeInformation.OBJECT;
}
 
Example 5
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the given {@link Map} into a {@link Map}. will recursively resolve nested
 * {@link Map}s as well.
 * @param type the {@link Map} {@link TypeInformation} to be used to unmarshal this
 * {@link Map}.
 * @param sourceMap must not be {@literal null}
 * @return the converted {@link Map}.
 */
protected Map<Object, Object> readMap(TypeInformation<?> type, Map<String, Object> sourceMap) {

	Assert.notNull(sourceMap, "Source map must not be null");

	Class<?> mapType = this.typeMapper.readType(sourceMap, type).getType();

	TypeInformation<?> keyType = type.getComponentType();
	TypeInformation<?> valueType = type.getMapValueType();

	Class<?> rawKeyType = keyType != null ? keyType.getType() : null;
	Class<?> rawValueType = valueType != null ? valueType.getType() : null;

	Map<Object, Object> map = CollectionFactory.createMap(mapType, rawKeyType, sourceMap.keySet().size());

	for (Entry<String, Object> entry : sourceMap.entrySet()) {

		if (this.typeMapper.isTypeKey(entry.getKey())) {
			continue;
		}

		Object key = entry.getKey();

		if (rawKeyType != null && !rawKeyType.isAssignableFrom(key.getClass())) {
			key = this.conversionService.convert(key, rawKeyType);
		}

		Object value = entry.getValue();
		TypeInformation<?> defaultedValueType = valueType != null ? valueType : ClassTypeInformation.OBJECT;

		if (value instanceof Map) {
			map.put(key, read(defaultedValueType, (Map) value));
		}
		else if (value instanceof List) {
			map.put(key,
					readCollectionOrArray(valueType != null ? valueType : ClassTypeInformation.LIST, (List) value));
		}
		else {
			map.put(key, getPotentiallyConvertedSimpleRead(value, rawValueType));
		}
	}

	return map;
}