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

The following examples show how to use org.springframework.data.util.TypeInformation#isCollectionLike() . 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: CrateDocumentConverter.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param root container for the converted payload
 * @param payload value to be converted to {@link CrateDocument}
 */
@SuppressWarnings("unchecked")
private void toCrateDocument(CrateDocument root, Object payload) {
	
	Map<String, Object> map = (Map<String, Object>)payload;
	
	for(Entry<String, Object> entry : map.entrySet()) {
		
		TypeInformation<?> type = getTypeInformation(entry.getValue().getClass());
		
		if(type.isMap()) {
			CrateDocument document = new CrateDocument();
			toCrateDocument(document, entry.getValue());
			logger.debug("converted '{}' to CrateDocument", entry.getKey());
			root.put(entry.getKey(), document);
		}else if(type.isCollectionLike()) {
			CrateArray array = new CrateArray();
			toCrateArray(array, entry.getValue());
			logger.debug("converted '{}' to CrateArray", entry.getKey());
			root.put(entry.getKey(), array);
		}else {
			// simple type
			root.put(entry.getKey(), entry.getValue());
		}
	}
}
 
Example 2
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void writeProperty(final Object source, final VPackBuilder sink, final ArangoPersistentProperty property) {
	if (source == null) {
		return;
	}

	final TypeInformation<?> sourceType = ClassTypeInformation.from(source.getClass());
	final String fieldName = property.getFieldName();

	if (property.getRef().isPresent()) {
		if (sourceType.isCollectionLike()) {
			writeReferences(fieldName, source, sink);
		} else {
			writeReference(fieldName, source, sink);
		}
	}

	else if (property.getRelations().isPresent()) {
		// nothing to store
	}

	else if (property.getFrom().isPresent() || property.getTo().isPresent()) {
		if (!sourceType.isCollectionLike()) {
			writeReference(fieldName, source, sink);
		}
	}

	else {
		final Object entity = source instanceof LazyLoadingProxy ? ((LazyLoadingProxy) source).getEntity() : source;
		writeInternal(fieldName, entity, sink, property.getTypeInformation());
	}
}
 
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: EntityColumnMapper.java    From spring-data-crate with Apache License 2.0 5 votes vote down vote up
private void checkNestedCollection(CratePersistentProperty property) {
	
	TypeInformation<?> componentType = from(property.getComponentType());
	
	if(componentType.isCollectionLike()) {
		throw new InvalidCrateApiUsageException("currently crate does not support nested arrays/collections of arrays/collections");
	}
}
 
Example 5
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
private Object readInternal(final TypeInformation<?> type, final VPackSlice source) {
	if (source == null) {
		return null;
	}

	if (VPackSlice.class.isAssignableFrom(type.getType())) {
		return source;
	}

	final TypeInformation<?> typeToUse = (source.isArray() || source.isObject()) ? typeMapper.readType(source, type)
			: type;
	final Class<?> rawTypeToUse = typeToUse.getType();

	if (conversions.hasCustomReadTarget(VPackSlice.class, typeToUse.getType())) {
		return conversionService.convert(source, rawTypeToUse);
	}

	if (conversions.hasCustomReadTarget(DBDocumentEntity.class, typeToUse.getType())) {
		return conversionService.convert(readSimple(DBDocumentEntity.class, source), rawTypeToUse);
	}

	if (!source.isArray() && !source.isObject()) {
		return convertIfNecessary(readSimple(rawTypeToUse, source), rawTypeToUse);
	}

	if (DBDocumentEntity.class.isAssignableFrom(rawTypeToUse)) {
		return readSimple(rawTypeToUse, source);
	}

	if (BaseDocument.class.isAssignableFrom(rawTypeToUse)) {
		return readBaseDocument(rawTypeToUse, source);
	}

	if (typeToUse.isMap()) {
		return readMap(typeToUse, source);
	}

	if (!source.isArray() && ClassTypeInformation.OBJECT.equals(typeToUse)) {
		return readMap(ClassTypeInformation.MAP, source);
	}

	if (typeToUse.getType().isArray()) {
		return readArray(typeToUse, source);
	}

	if (typeToUse.isCollectionLike()) {
		return readCollection(typeToUse, source);
	}

	if (ClassTypeInformation.OBJECT.equals(typeToUse)) {
		return readCollection(ClassTypeInformation.COLLECTION, source);
	}

	final ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(rawTypeToUse);
	return readEntity(typeToUse, source, entity);
}
 
Example 6
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeInternal(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	final Class<?> rawType = source.getClass();
	final TypeInformation<?> type = ClassTypeInformation.from(rawType);

	if (conversions.isSimpleType(rawType)) {
		final Optional<Class<?>> customWriteTarget = conversions.getCustomWriteTarget(rawType);
		final Class<?> targetType = customWriteTarget.orElse(rawType);
		writeSimple(attribute, conversionService.convert(source, targetType), sink);
	}

	else if (BaseDocument.class.equals(rawType)) {
		writeBaseDocument(attribute, (BaseDocument) source, sink, definedType);
	}

	else if (BaseEdgeDocument.class.equals(rawType)) {
		writeBaseEdgeDocument(attribute, (BaseEdgeDocument) source, sink, definedType);
	}

	else if (type.isMap()) {
		writeMap(attribute, (Map<Object, Object>) source, sink, definedType);
	}

	else if (type.getType().isArray()) {
		writeArray(attribute, source, sink, definedType);
	}

	else if (type.isCollectionLike()) {
		writeCollection(attribute, source, sink, definedType);
	}

	else {
		final ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(source.getClass());
		writeEntity(attribute, source, sink, entity, definedType);
	}
}
 
Example 7
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(@Nullable Object obj, SecretDocumentAccessor accessor,
		VaultPersistentProperty prop) {

	if (obj == null) {
		return;
	}

	TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
	TypeInformation<?> type = prop.getTypeInformation();

	if (valueType.isCollectionLike()) {
		List<Object> collectionInternal = createCollection(asCollection(obj), prop);
		accessor.put(prop, collectionInternal);
		return;
	}

	if (valueType.isMap()) {
		Map<String, Object> mapDbObj = createMap((Map<Object, Object>) obj, prop);
		accessor.put(prop, mapDbObj);
		return;
	}

	// Lookup potential custom target type
	Optional<Class<?>> basicTargetType = this.conversions.getCustomWriteTarget(obj.getClass());

	if (basicTargetType.isPresent()) {

		accessor.put(prop, this.conversionService.convert(obj, basicTargetType.get()));
		return;
	}

	VaultPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
			? this.mappingContext.getRequiredPersistentEntity(obj.getClass())
			: this.mappingContext.getRequiredPersistentEntity(type);

	SecretDocumentAccessor nested = accessor.writeNested(prop);

	writeInternal(obj, nested, entity);
	addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, nested);
}