use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.
the class MongoConverter method mapValueToTargetType.
/**
* Mapping function capable of converting values into a desired target type by eg. extracting the actual java type
* from a given {@link BsonValue}.
*
* @param targetType must not be {@literal null}.
* @param dbRefResolver must not be {@literal null}.
* @param <S>
* @param <T>
* @return new typed {@link java.util.function.Function}.
* @throws IllegalArgumentException if {@literal targetType} is {@literal null}.
* @since 2.1
*/
@SuppressWarnings("unchecked")
@Nullable
default <S, T> T mapValueToTargetType(S source, Class<T> targetType, DbRefResolver dbRefResolver) {
Assert.notNull(targetType, "TargetType must not be null!");
Assert.notNull(dbRefResolver, "DbRefResolver must not be null!");
if (targetType != Object.class && ClassUtils.isAssignable(targetType, source.getClass())) {
return (T) source;
}
if (source instanceof BsonValue) {
Object value = BsonUtils.toJavaType((BsonValue) source);
if (value instanceof Document) {
Document sourceDocument = (Document) value;
if (sourceDocument.containsKey("$ref") && sourceDocument.containsKey("$id")) {
sourceDocument = dbRefResolver.fetch(new DBRef(sourceDocument.getString("$ref"), sourceDocument.get("$id")));
if (sourceDocument == null) {
return null;
}
}
return read(targetType, sourceDocument);
} else {
if (!ClassUtils.isAssignable(targetType, value.getClass())) {
if (getConversionService().canConvert(value.getClass(), targetType)) {
return getConversionService().convert(value, targetType);
}
}
}
return (T) value;
}
return getConversionService().convert(source, targetType);
}
use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.
the class QueryMapper method convertAssociation.
/**
* Converts the given source assuming it's actually an association to another object.
*
* @param source
* @param property
* @return
*/
@Nullable
protected Object convertAssociation(@Nullable Object source, @Nullable MongoPersistentProperty property) {
if (property == null || source == null || source instanceof Document || source instanceof DBObject) {
return source;
}
if (source instanceof DBRef) {
DBRef ref = (DBRef) source;
return new DBRef(ref.getCollectionName(), convertId(ref.getId()));
}
if (source instanceof Iterable) {
BasicDBList result = new BasicDBList();
for (Object element : (Iterable<?>) source) {
result.add(createDbRefFor(element, property));
}
return result;
}
if (property.isMap()) {
Document result = new Document();
Document dbObject = (Document) source;
for (String key : dbObject.keySet()) {
result.put(key, createDbRefFor(dbObject.get(key), property));
}
return result;
}
return createDbRefFor(source, property);
}
use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.
the class BasicMongoPersistentEntity method detectExpression.
/**
* Returns a SpEL {@link Expression} frór the collection String expressed in the given {@link Document} annotation if
* present or {@literal null} otherwise. Will also return {@literal null} it the collection {@link String} evaluates
* to a {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
*
* @param document can be {@literal null}
* @return
*/
@Nullable
private static Expression detectExpression(Document document) {
String collection = document.collection();
if (!StringUtils.hasText(collection)) {
return null;
}
Expression expression = PARSER.parseExpression(document.collection(), ParserContext.TEMPLATE_EXPRESSION);
return expression instanceof LiteralExpression ? null : expression;
}
use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.
the class ExpressionEvaluatingParameterBinder method evaluateExpression.
/**
* Evaluates the given {@code expressionString}.
*
* @param expressionString must not be {@literal null} or empty.
* @param parameters must not be {@literal null}.
* @param parameterValues must not be {@literal null}.
* @return
*/
@Nullable
private Object evaluateExpression(String expressionString, MongoParameters parameters, Object[] parameterValues) {
EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(parameters, parameterValues);
Expression expression = expressionParser.parseExpression(expressionString);
return expression.getValue(evaluationContext, Object.class);
}
use of org.springframework.lang.Nullable in project spring-data-jdbc by spring-projects.
the class IterableOfEntryToMapConverter method convert.
@Nullable
@Override
public Map convert(Iterable source) {
Map result = new HashMap();
source.forEach(element -> {
if (!(element instanceof Entry)) {
throw new IllegalArgumentException(String.format("Cannot convert %s to Map.Entry", element.getClass()));
}
Entry entry = (Entry) element;
result.put(entry.getKey(), entry.getValue());
});
return result;
}
Aggregations