Skip to content

Commit eb1392c

Browse files
DATAMONGO-861 - Polishing.
Favor usage of List over BasicDBList. Rename ProjectionOperation.transform to applyCondition. Add missing author and since tags, remove trailing white spaces and fix reference documentation headline clash. Original Pull Request: #385
1 parent ace01e4 commit eb1392c

File tree

11 files changed

+196
-167
lines changed

11 files changed

+196
-167
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.data.domain.Sort.Direction;
2626
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
2727
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
28-
import org.springframework.data.mongodb.core.aggregation.Fields.*;
28+
import org.springframework.data.mongodb.core.aggregation.Fields.AggregationField;
2929
import org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation.InheritsFieldsAggregationOperation;
3030
import org.springframework.data.mongodb.core.query.Criteria;
3131
import org.springframework.data.mongodb.core.query.NearQuery;
@@ -331,13 +331,13 @@ public static MatchOperation match(Criteria criteria) {
331331
}
332332

333333
/**
334-
* Creates a new {@link OutOperation} using the given collection name. This operation must be the last operation
335-
* in the pipeline.
334+
* Creates a new {@link OutOperation} using the given collection name. This operation must be the last operation in
335+
* the pipeline.
336336
*
337337
* @param outCollectionName collection name to export aggregation results. The {@link OutOperation} creates a new
338-
* collection in the current database if one does not already exist. The collection is
339-
* not visible until the aggregation completes. If the aggregation fails, MongoDB does
340-
* not create the collection. Must not be {@literal null}.
338+
* collection in the current database if one does not already exist. The collection is not visible until the
339+
* aggregation completes. If the aggregation fails, MongoDB does not create the collection. Must not be
340+
* {@literal null}.
341341
* @return
342342
*/
343343
public static OutOperation out(String outCollectionName) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ConditionalOperator.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package org.springframework.data.mongodb.core.aggregation;
1717

18+
import java.util.ArrayList;
19+
import java.util.List;
20+
1821
import org.springframework.dao.InvalidDataAccessApiUsageException;
1922
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
2023
import org.springframework.util.Assert;
2124
import org.springframework.util.ClassUtils;
2225

23-
import com.mongodb.BasicDBList;
2426
import com.mongodb.BasicDBObject;
2527
import com.mongodb.DBObject;
2628

@@ -32,6 +34,7 @@
3234
*
3335
* @see http://docs.mongodb.com/manual/reference/operator/aggregation/cond/
3436
* @author Mark Paluch
37+
* @author Christoph Strobl
3538
* @since 1.10
3639
*/
3740
public class ConditionalOperator implements AggregationExpression {
@@ -42,7 +45,7 @@ public class ConditionalOperator implements AggregationExpression {
4245

4346
/**
4447
* Creates a new {@link ConditionalOperator} for a given {@link Field} and {@code then}/{@code otherwise} values.
45-
*
48+
*
4649
* @param condition must not be {@literal null}.
4750
* @param thenValue must not be {@literal null}.
4851
* @param otherwiseValue must not be {@literal null}.
@@ -116,8 +119,7 @@ private Object resolveValue(AggregationOperationContext context, Object value) {
116119
return ((ConditionalOperator) value).toDbObject(context);
117120
}
118121

119-
DBObject toMap = context.getMappedObject(new BasicDBObject("$set", value));
120-
return toMap.get("$set");
122+
return context.getMappedObject(new BasicDBObject("$set", value)).get("$set");
121123
}
122124

123125
private Object resolveCriteria(AggregationOperationContext context, Object value) {
@@ -129,7 +131,7 @@ private Object resolveCriteria(AggregationOperationContext context, Object value
129131
if (value instanceof CriteriaDefinition) {
130132

131133
DBObject mappedObject = context.getMappedObject(((CriteriaDefinition) value).getCriteriaObject());
132-
BasicDBList clauses = new BasicDBList();
134+
List<Object> clauses = new ArrayList<Object>();
133135

134136
clauses.addAll(getClauses(context, mappedObject));
135137

@@ -144,9 +146,9 @@ private Object resolveCriteria(AggregationOperationContext context, Object value
144146
String.format("Invalid value in condition. Supported: DBObject, Field references, Criteria, got: %s", value));
145147
}
146148

147-
private BasicDBList getClauses(AggregationOperationContext context, DBObject mappedObject) {
149+
private List<Object> getClauses(AggregationOperationContext context, DBObject mappedObject) {
148150

149-
BasicDBList clauses = new BasicDBList();
151+
List<Object> clauses = new ArrayList<Object>();
150152

151153
for (String key : mappedObject.keySet()) {
152154

@@ -157,15 +159,17 @@ private BasicDBList getClauses(AggregationOperationContext context, DBObject map
157159
return clauses;
158160
}
159161

160-
private BasicDBList getClauses(AggregationOperationContext context, String key, Object predicate) {
162+
private List<Object> getClauses(AggregationOperationContext context, String key, Object predicate) {
161163

162-
BasicDBList clauses = new BasicDBList();
164+
List<Object> clauses = new ArrayList<Object>();
163165

164-
if (predicate instanceof BasicDBList) {
166+
if (predicate instanceof List) {
165167

166-
BasicDBList args = new BasicDBList();
167-
for (Object clause : (BasicDBList) predicate) {
168-
args.addAll(getClauses(context, (BasicDBObject) clause));
168+
List<Object> args = new ArrayList<Object>();
169+
for (Object clause : (List<?>) predicate) {
170+
if (clause instanceof DBObject) {
171+
args.addAll(getClauses(context, (DBObject) clause));
172+
}
169173
}
170174

171175
clauses.add(new BasicDBObject(key, args));
@@ -180,15 +184,15 @@ private BasicDBList getClauses(AggregationOperationContext context, String key,
180184
continue;
181185
}
182186

183-
BasicDBList args = new BasicDBList();
187+
List<Object> args = new ArrayList<Object>();
184188
args.add("$" + key);
185189
args.add(nested.get(s));
186190
clauses.add(new BasicDBObject(s, args));
187191
}
188192

189193
} else if (!isKeyword(key)) {
190194

191-
BasicDBList args = new BasicDBList();
195+
List<Object> args = new ArrayList<Object>();
192196
args.add("$" + key);
193197
args.add(predicate);
194198
clauses.add(new BasicDBObject("$eq", args));
@@ -230,6 +234,9 @@ public static ConditionalExpressionBuilder newBuilder() {
230234
return ConditionalExpressionBuilder.newBuilder();
231235
}
232236

237+
/**
238+
* @since 1.10
239+
*/
233240
public static interface WhenBuilder {
234241

235242
/**
@@ -257,6 +264,9 @@ public static interface WhenBuilder {
257264
ThenBuilder when(CriteriaDefinition criteria);
258265
}
259266

267+
/**
268+
* @since 1.10
269+
*/
260270
public static interface ThenBuilder {
261271

262272
/**
@@ -268,6 +278,9 @@ public static interface ThenBuilder {
268278
OtherwiseBuilder then(Object value);
269279
}
270280

281+
/**
282+
* @since 1.10
283+
*/
271284
public static interface OtherwiseBuilder {
272285

273286
/**
@@ -314,7 +327,7 @@ public ConditionalExpressionBuilder when(DBObject booleanExpression) {
314327
return this;
315328
}
316329

317-
/*
330+
/*
318331
* (non-Javadoc)
319332
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(org.springframework.data.mongodb.core.query.CriteriaDefinition)
320333
*/
@@ -327,7 +340,7 @@ public ThenBuilder when(CriteriaDefinition criteria) {
327340
return this;
328341
}
329342

330-
/*
343+
/*
331344
* (non-Javadoc)
332345
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(org.springframework.data.mongodb.core.aggregation.Field)
333346
*/
@@ -340,7 +353,7 @@ public ThenBuilder when(Field booleanField) {
340353
return this;
341354
}
342355

343-
/*
356+
/*
344357
* (non-Javadoc)
345358
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(java.lang.String)
346359
*/
@@ -353,7 +366,7 @@ public ThenBuilder when(String booleanField) {
353366
return this;
354367
}
355368

356-
/*
369+
/*
357370
* (non-Javadoc)
358371
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.ThenBuilder#then(java.lang.Object)
359372
*/
@@ -366,7 +379,7 @@ public OtherwiseBuilder then(Object thenValue) {
366379
return this;
367380
}
368381

369-
/*
382+
/*
370383
* (non-Javadoc)
371384
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.OtherwiseBuilder#otherwise(java.lang.Object)
372385
*/

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/IfNullOperator.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616

1717
package org.springframework.data.mongodb.core.aggregation;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
1922
import org.springframework.util.Assert;
2023

21-
import com.mongodb.BasicDBList;
2224
import com.mongodb.BasicDBObject;
2325
import com.mongodb.DBObject;
2426

2527
/**
2628
* Encapsulates the aggregation framework {@code $ifNull} operator. Replacement values can be either {@link Field field
2729
* references}, values of simple MongoDB types or values that can be converted to a simple MongoDB type.
28-
*
30+
*
2931
* @see http://docs.mongodb.com/manual/reference/operator/aggregation/ifNull/
3032
* @author Mark Paluch
3133
* @since 1.10
@@ -37,7 +39,7 @@ public class IfNullOperator implements AggregationExpression {
3739

3840
/**
3941
* Creates a new {@link IfNullOperator} for the given {@link Field} and replacement {@code value}.
40-
*
42+
*
4143
* @param field must not be {@literal null}.
4244
* @param value must not be {@literal null}.
4345
*/
@@ -50,26 +52,30 @@ public IfNullOperator(Field field, Object value) {
5052
this.value = value;
5153
}
5254

53-
/*
55+
/*
5456
* (non-Javadoc)
5557
* @see org.springframework.data.mongodb.core.aggregation.AggregationExpression#toDbObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
5658
*/
5759
@Override
5860
public DBObject toDbObject(AggregationOperationContext context) {
5961

60-
BasicDBList list = new BasicDBList();
62+
List<Object> list = new ArrayList<Object>();
6163

6264
list.add(context.getReference(field).toString());
65+
list.add(resolve(value, context));
6366

64-
if (value instanceof Field) {
65-
list.add(context.getReference((Field) value).toString());
66-
} else {
67+
return new BasicDBObject("$ifNull", list);
68+
}
6769

68-
DBObject toMap = context.getMappedObject(new BasicDBObject("$set", value));
69-
list.add(toMap.get("$set"));
70+
private Object resolve(Object value, AggregationOperationContext context) {
71+
72+
if (value instanceof Field) {
73+
return context.getReference((Field) value).toString();
74+
} else if (value instanceof DBObject) {
75+
return value;
7076
}
7177

72-
return new BasicDBObject("$ifNull", list);
78+
return context.getMappedObject(new BasicDBObject("$set", value)).get("$set");
7379
}
7480

7581
/**
@@ -81,6 +87,9 @@ public static IfNullBuilder newBuilder() {
8187
return IfNullOperatorBuilder.newBuilder();
8288
}
8389

90+
/**
91+
* @since 1.10
92+
*/
8493
public static interface IfNullBuilder {
8594

8695
/**
@@ -96,6 +105,9 @@ public static interface IfNullBuilder {
96105
ThenBuilder ifNull(String field);
97106
}
98107

108+
/**
109+
* @since 1.10
110+
*/
99111
public static interface ThenBuilder {
100112

101113
/**
@@ -134,11 +146,10 @@ public static IfNullOperatorBuilder newBuilder() {
134146
return new IfNullOperatorBuilder();
135147
}
136148

137-
/*
149+
/*
138150
* (non-Javadoc)
139151
* @see org.springframework.data.mongodb.core.aggregation.IfNullOperator.IfNullBuilder#ifNull(org.springframework.data.mongodb.core.aggregation.Field)
140152
*/
141-
142153
public ThenBuilder ifNull(Field field) {
143154

144155
Assert.notNull(field, "Field must not be null!");
@@ -147,7 +158,7 @@ public ThenBuilder ifNull(Field field) {
147158
return this;
148159
}
149160

150-
/*
161+
/*
151162
* (non-Javadoc)
152163
* @see org.springframework.data.mongodb.core.aggregation.IfNullOperator.IfNullBuilder#ifNull(java.lang.String)
153164
*/
@@ -159,7 +170,7 @@ public ThenBuilder ifNull(String name) {
159170
return this;
160171
}
161172

162-
/*
173+
/*
163174
* (non-Javadoc)
164175
* @see org.springframework.data.mongodb.core.aggregation.IfNullOperator.ThenReplaceBuilder#thenReplaceWith(org.springframework.data.mongodb.core.aggregation.Field)
165176
*/
@@ -171,7 +182,7 @@ public IfNullOperator thenReplaceWith(Field replacementField) {
171182
return new IfNullOperator(this.field, replacementField);
172183
}
173184

174-
/*
185+
/*
175186
* (non-Javadoc)
176187
* @see org.springframework.data.mongodb.core.aggregation.IfNullOperator.ThenReplaceBuilder#thenReplaceWith(java.lang.Object)
177188
*/

0 commit comments

Comments
 (0)