DATAMONGO-2200 - Polishing.

Tweak Javadoc. Simplify Fields creation from Stream. Remove final modifier from private static method. Iterate with loop over PersistentEntity.

Original pull request: #748.
This commit is contained in:
Mark Paluch
2019-07-02 14:24:02 +02:00
parent e24c5e0846
commit bb280bd59b
4 changed files with 20 additions and 13 deletions

View File

@@ -18,10 +18,9 @@ package org.springframework.data.mongodb.core.aggregation;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.lang.Nullable;
@@ -77,17 +76,20 @@ public interface AggregationOperationContext {
FieldReference getReference(String name);
/**
* Returns the {@link Fields} exposed by the type. May be a {@literal class} or an {@literal interface}.
* Returns the {@link Fields} exposed by the type. May be a {@literal class} or an {@literal interface}. The default
* implementation uses {@link BeanUtils#getPropertyDescriptors(Class) property descriptors} discover fields from a
* {@link Class}.
*
* @param type must not be {@literal null}.
* @return never {@literal null}.
* @since 2.2
* @see BeanUtils#getPropertyDescriptor(Class, String)
*/
default Fields getFields(Class<?> type) {
Assert.notNull(type, "Type must not be null!");
List<String> fields = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
return Fields.fields(Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
.filter(it -> { // object and default methods
Method method = it.getReadMethod();
if (method == null) {
@@ -99,8 +101,6 @@ public interface AggregationOperationContext {
return !method.isDefault();
}) //
.map(PropertyDescriptor::getName) //
.collect(Collectors.toList());
return Fields.fields(fields.toArray(new String[0]));
.toArray(String[]::new));
}
}

View File

@@ -112,7 +112,7 @@ public final class Fields implements Iterable<Field> {
this.fields = verify(fields);
}
private static final List<Field> verify(List<Field> fields) {
private static List<Field> verify(List<Field> fields) {
Map<String, Field> reference = new HashMap<String, Field>();

View File

@@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.List;
import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.IfNull;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
@@ -30,7 +31,6 @@ import org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Pro
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* Encapsulates the aggregation framework {@code $project}-operation.
@@ -1739,7 +1739,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
Document projections = new Document();
Fields fields = context.getFields(type);
fields.asList().forEach(it -> projections.append(it.getName(), 1));
fields.forEach(it -> projections.append(it.getName(), 1));
return context.getMappedObject(projections, type);
}
}

View File

@@ -21,8 +21,8 @@ import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
@@ -111,14 +111,21 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio
@Override
public Fields getFields(Class<?> type) {
Assert.notNull(type, "Type must not be null!");
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
if (entity == null) {
return AggregationOperationContext.super.getFields(type);
}
List<String> fields = new ArrayList<>();
entity.doWithProperties((SimplePropertyHandler) it -> fields.add(it.getName()));
return Fields.fields(fields.toArray(new String[fields.size()]));
for (MongoPersistentProperty property : entity) {
fields.add(property.getName());
}
return Fields.fields(fields.toArray(new String[0]));
}
private FieldReference getReferenceFor(Field field) {