Skip to content

Commit 743ac49

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.
1 parent f1b3f23 commit 743ac49

File tree

11 files changed

+190
-159
lines changed

11 files changed

+190
-159
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: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
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;
@@ -32,6 +35,7 @@
3235
*
3336
* @see http://docs.mongodb.com/manual/reference/operator/aggregation/cond/
3437
* @author Mark Paluch
38+
* @author Christoph Strobl
3539
* @since 1.10
3640
*/
3741
public class ConditionalOperator implements AggregationExpression {
@@ -42,7 +46,7 @@ public class ConditionalOperator implements AggregationExpression {
4246

4347
/**
4448
* Creates a new {@link ConditionalOperator} for a given {@link Field} and {@code then}/{@code otherwise} values.
45-
*
49+
*
4650
* @param condition must not be {@literal null}.
4751
* @param thenValue must not be {@literal null}.
4852
* @param otherwiseValue must not be {@literal null}.
@@ -116,8 +120,7 @@ private Object resolveValue(AggregationOperationContext context, Object value) {
116120
return ((ConditionalOperator) value).toDbObject(context);
117121
}
118122

119-
DBObject toMap = context.getMappedObject(new BasicDBObject("$set", value));
120-
return toMap.get("$set");
123+
return context.getMappedObject(new BasicDBObject("$set", value)).get("$set");
121124
}
122125

123126
private Object resolveCriteria(AggregationOperationContext context, Object value) {
@@ -144,9 +147,9 @@ private Object resolveCriteria(AggregationOperationContext context, Object value
144147
String.format("Invalid value in condition. Supported: DBObject, Field references, Criteria, got: %s", value));
145148
}
146149

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

149-
BasicDBList clauses = new BasicDBList();
152+
List<Object> clauses = new ArrayList<Object>();
150153

151154
for (String key : mappedObject.keySet()) {
152155

@@ -157,15 +160,17 @@ private BasicDBList getClauses(AggregationOperationContext context, DBObject map
157160
return clauses;
158161
}
159162

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

162-
BasicDBList clauses = new BasicDBList();
165+
List<Object> clauses = new ArrayList<Object>();
163166

164-
if (predicate instanceof BasicDBList) {
167+
if (predicate instanceof List) {
165168

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

171176
clauses.add(new BasicDBObject(key, args));
@@ -180,15 +185,15 @@ private BasicDBList getClauses(AggregationOperationContext context, String key,
180185
continue;
181186
}
182187

183-
BasicDBList args = new BasicDBList();
188+
List<Object> args = new ArrayList<Object>();
184189
args.add("$" + key);
185190
args.add(nested.get(s));
186191
clauses.add(new BasicDBObject(s, args));
187192
}
188193

189194
} else if (!isKeyword(key)) {
190195

191-
BasicDBList args = new BasicDBList();
196+
List<Object> args = new ArrayList<Object>();
192197
args.add("$" + key);
193198
args.add(predicate);
194199
clauses.add(new BasicDBObject("$eq", args));
@@ -230,6 +235,9 @@ public static ConditionalExpressionBuilder newBuilder() {
230235
return ConditionalExpressionBuilder.newBuilder();
231236
}
232237

238+
/**
239+
* @since 1.10
240+
*/
233241
public static interface WhenBuilder {
234242

235243
/**
@@ -257,6 +265,9 @@ public static interface WhenBuilder {
257265
ThenBuilder when(CriteriaDefinition criteria);
258266
}
259267

268+
/**
269+
* @since 1.10
270+
*/
260271
public static interface ThenBuilder {
261272

262273
/**
@@ -268,6 +279,9 @@ public static interface ThenBuilder {
268279
OtherwiseBuilder then(Object value);
269280
}
270281

282+
/**
283+
* @since 1.10
284+
*/
271285
public static interface OtherwiseBuilder {
272286

273287
/**
@@ -314,7 +328,7 @@ public ConditionalExpressionBuilder when(DBObject booleanExpression) {
314328
return this;
315329
}
316330

317-
/*
331+
/*
318332
* (non-Javadoc)
319333
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(org.springframework.data.mongodb.core.query.CriteriaDefinition)
320334
*/
@@ -327,7 +341,7 @@ public ThenBuilder when(CriteriaDefinition criteria) {
327341
return this;
328342
}
329343

330-
/*
344+
/*
331345
* (non-Javadoc)
332346
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(org.springframework.data.mongodb.core.aggregation.Field)
333347
*/
@@ -340,7 +354,7 @@ public ThenBuilder when(Field booleanField) {
340354
return this;
341355
}
342356

343-
/*
357+
/*
344358
* (non-Javadoc)
345359
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.WhenBuilder#when(java.lang.String)
346360
*/
@@ -353,7 +367,7 @@ public ThenBuilder when(String booleanField) {
353367
return this;
354368
}
355369

356-
/*
370+
/*
357371
* (non-Javadoc)
358372
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.ThenBuilder#then(java.lang.Object)
359373
*/
@@ -366,7 +380,7 @@ public OtherwiseBuilder then(Object thenValue) {
366380
return this;
367381
}
368382

369-
/*
383+
/*
370384
* (non-Javadoc)
371385
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperator.OtherwiseBuilder#otherwise(java.lang.Object)
372386
*/

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
*/

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Thomas Darimont
4343
* @author Oliver Gierke
4444
* @author Christoph Strobl
45+
* @author Mark Paluch
4546
* @since 1.3
4647
*/
4748
public class ProjectionOperation implements FieldsExposingAggregationOperation {
@@ -244,20 +245,20 @@ public DBObject toDBObject(AggregationOperationContext context) {
244245
/**
245246
* Apply a conditional projection using {@link ConditionalOperator}.
246247
*
247-
* @param conditional must not be {@literal null}.
248+
* @param conditionalOperator must not be {@literal null}.
248249
* @return never {@literal null}.
249250
* @since 1.10
250251
*/
251-
public abstract ProjectionOperation transform(ConditionalOperator conditional);
252+
public abstract ProjectionOperation applyCondition(ConditionalOperator conditionalOperator);
252253

253254
/**
254255
* Apply a conditional value replacement for {@literal null} values using {@link IfNullOperator}.
255256
*
256-
* @param ifNull must not be {@literal null}.
257+
* @param ifNullOperator must not be {@literal null}.
257258
* @return never {@literal null}.
258259
* @since 1.10
259260
*/
260-
public abstract ProjectionOperation transform(IfNullOperator ifNull);
261+
public abstract ProjectionOperation applyCondition(IfNullOperator ifNullOperator);
261262
}
262263

263264
/**
@@ -359,7 +360,8 @@ public DBObject toDBObject(AggregationOperationContext context) {
359360
return new BasicDBObject(getExposedField().getName(), toMongoExpression(context, expression, params));
360361
}
361362

362-
protected static Object toMongoExpression(AggregationOperationContext context, String expression, Object[] params) {
363+
protected static Object toMongoExpression(AggregationOperationContext context, String expression,
364+
Object[] params) {
363365
return TRANSFORMER.transform(expression, context, params);
364366
}
365367
}
@@ -455,22 +457,26 @@ public ProjectionOperation as(String alias) {
455457
return this.operation.and(new FieldProjection(Fields.field(alias, name), null));
456458
}
457459

458-
/*
460+
/*
459461
* (non-Javadoc)
460462
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.AbstractProjectionOperationBuilder#transform(org.springframework.data.mongodb.core.aggregation.ConditionalOperator)
461463
*/
462464
@Override
463-
public ProjectionOperation transform(ConditionalOperator conditional) {
464-
return this.operation.and(new ExpressionProjection(Fields.field(name), conditional));
465+
public ProjectionOperation applyCondition(ConditionalOperator conditionalOperator) {
466+
467+
Assert.notNull(conditionalOperator, "ConditionalOperator must not be null!");
468+
return this.operation.and(new ExpressionProjection(Fields.field(name), conditionalOperator));
465469
}
466470

467-
/*
471+
/*
468472
* (non-Javadoc)
469473
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.AbstractProjectionOperationBuilder#transform(org.springframework.data.mongodb.core.aggregation.IfNullOperator)
470474
*/
471475
@Override
472-
public ProjectionOperation transform(IfNullOperator ifNull) {
473-
return this.operation.and(new ExpressionProjection(Fields.field(name), ifNull));
476+
public ProjectionOperation applyCondition(IfNullOperator ifNullOperator) {
477+
478+
Assert.notNull(ifNullOperator, "IfNullOperator must not be null!");
479+
return this.operation.and(new ExpressionProjection(Fields.field(name), ifNullOperator));
474480
}
475481

476482
/**

0 commit comments

Comments
 (0)