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

The following examples show how to use org.springframework.data.util.TypeInformation#getComponentType() . 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: MappingSolrConverter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private Object readCollection(Collection<?> source, TypeInformation<?> type, Object parent) {
	Assert.notNull(type);

	Class<?> collectionType = type.getType();
	if (CollectionUtils.isEmpty(source)) {
		return source;
	}

	collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;

	Collection<Object> items;
	if (type.getType().isArray()) {
		items = new ArrayList<Object>();
	} else {
		items = CollectionFactory.createCollection(collectionType, source.size());
	}

	TypeInformation<?> componentType = type.getComponentType();

	Iterator<?> it = source.iterator();
	while (it.hasNext()) {
		items.add(readValue(it.next(), componentType, parent));
	}

	return type.getType().isArray() ? convertItemsToArrayOfType(type, items) : items;
}
 
Example 2
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
   * Helper method to write the internal collection.
   *
   * @param source the source object.
   * @param target the target document.
   * @param type the type information for the document.
   * @return the created crate list.
   */
private CrateArray writeCollectionInternal(final Collection<?> source, final CrateArray target, final TypeInformation<?> type) {
	
	TypeInformation<?> componentType = type == null ? null : type.getComponentType();

    for(Object element : source) {
    	
    	validateCollectionLikeElement(element);
    	
    	Class<?> elementType = element == null ? null : element.getClass();
    	
    	if(elementType == null || conversions.isSimpleType(elementType)) {
    		target.add(element);
    	}else {
    		CrateDocument document = new CrateDocument();
    		writeInternal(element, document, componentType);
    		target.add(document);
    	}
    }
    
    return target;
}
 
Example 3
Source File: TwoStepsConversions.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T convertOnRead(Object val, EmbeddedType embeddedType, TypeInformation targetTypeInformation) {
	TypeInformation componentTypeInformation;
	Class collectionType = null;
	if (targetTypeInformation.isCollectionLike()) {
		componentTypeInformation = targetTypeInformation.getComponentType();
		collectionType = targetTypeInformation.getType();
	}
	else {
		componentTypeInformation = targetTypeInformation;
	}
	return convertOnRead(val, embeddedType, collectionType, componentTypeInformation);
}
 
Example 4
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the given {@link List} into a collection of the given {@link TypeInformation}
 * .
 * @param targetType must not be {@literal null}.
 * @param sourceValue must not be {@literal null}.
 * @return the converted {@link Collection} or array, will never be {@literal null}.
 */
@Nullable
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object readCollectionOrArray(TypeInformation<?> targetType, List sourceValue) {

	Assert.notNull(targetType, "Target type must not be null");

	Class<?> collectionType = targetType.getType();

	TypeInformation<?> componentType = targetType.getComponentType() != null ? targetType.getComponentType()
			: ClassTypeInformation.OBJECT;
	Class<?> rawComponentType = componentType.getType();

	collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
	Collection<Object> items = targetType.getType().isArray() ? new ArrayList<>(sourceValue.size())
			: CollectionFactory.createCollection(collectionType, rawComponentType, sourceValue.size());

	if (sourceValue.isEmpty()) {
		return getPotentiallyConvertedSimpleRead(items, collectionType);
	}

	for (Object obj : sourceValue) {

		if (obj instanceof Map) {
			items.add(read(componentType, (Map) obj));
		}
		else if (obj instanceof List) {
			items.add(readCollectionOrArray(ClassTypeInformation.OBJECT, (List) obj));
		}
		else {
			items.add(getPotentiallyConvertedSimpleRead(obj, rawComponentType));
		}
	}

	return getPotentiallyConvertedSimpleRead(items, targetType.getType());
}
 
Example 5
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
/**
 * Populates the given {@link List} with values from the given {@link Collection}.
 * @param source the collection to create a {@link List} for, must not be
 * {@literal null}.
 * @param type the {@link TypeInformation} to consider or {@literal null} if unknown.
 * @param sink the {@link List} to write to.
 * @return the converted {@link List}.
 */
private List<Object> writeCollectionInternal(Collection<?> source, @Nullable TypeInformation<?> type,
		List<Object> sink) {

	TypeInformation<?> componentType = null;

	if (type != null) {
		componentType = type.getComponentType();
	}

	for (Object element : source) {

		Class<?> elementType = element == null ? null : element.getClass();

		if (elementType == null || this.conversions.isSimpleType(elementType)) {
			sink.add(getPotentiallyConvertedSimpleWrite(element));
		}
		else if (element instanceof Collection || elementType.isArray()) {
			sink.add(writeCollectionInternal(asCollection(element), componentType, new ArrayList<>()));
		}
		else {
			SecretDocumentAccessor accessor = new SecretDocumentAccessor(new SecretDocument());
			writeInternal(element, accessor, componentType);
			sink.add(accessor.getBody());
		}
	}

	return sink;
}
 
Example 6
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 7
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 5 votes vote down vote up
/**
 * Read a collection from the source object.
 *
 * @param targetType the target type.
 * @param source the list as source.
 * @param parent the optional parent.
 * @return the converted {@link Collection} or array, will never be {@literal null}.
 */
private Object readCollection(final TypeInformation<?> targetType, final CrateArray source, final Object parent) {
	
	notNull(targetType);

    Class<?> collectionType = targetType.getType();
    
    if(source.isEmpty()) {
      return getPotentiallyConvertedSimpleRead(new HashSet<Object>(), collectionType);
    }

    collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
    
    Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>(source.size()) :
    															createCollection(collectionType, source.size());
    
    TypeInformation<?> componentType = targetType.getComponentType();
    
    Class<?> rawComponentType = componentType == null ? null : componentType.getType();

    for(Object object : source) {
    	if(object instanceof CrateDocument) {
    		items.add(read(componentType, (CrateDocument) object, parent));
    	}else {
    		items.add(getPotentiallyConvertedSimpleRead(object, rawComponentType));
    	}
    }
    
    return getPotentiallyConvertedSimpleRead(items, targetType.getType());
  }
 
Example 8
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
private TypeInformation<?> getNonNullComponentType(final TypeInformation<?> type) {
	final TypeInformation<?> compType = type.getComponentType();
	return compType != null ? compType : ClassTypeInformation.OBJECT;
}
 
Example 9
Source File: AbstractResolver.java    From spring-data with Apache License 2.0 4 votes vote down vote up
protected static TypeInformation<?> getNonNullComponentType(final TypeInformation<?> type) {
	final TypeInformation<?> compType = type.getComponentType();
	return compType != null ? compType : ClassTypeInformation.OBJECT;
}
 
Example 10
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;
}