Skip to content

Commit ff28789

Browse files
christophstroblmp911de
authored andcommitted
Polishing.
See #4139 Original pull request: #4182.
1 parent cdfdeaf commit ff28789

File tree

6 files changed

+134
-41
lines changed

6 files changed

+134
-41
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.util.LinkedHashMap;
2424
import java.util.List;
2525
import java.util.Map;
26-
import java.util.stream.Collectors;
2726

2827
import org.bson.Document;
28+
2929
import org.springframework.data.domain.Sort;
3030
import org.springframework.data.domain.Sort.Order;
3131
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
@@ -78,7 +78,14 @@ private Object unpack(Object value, AggregationOperationContext context) {
7878
}
7979

8080
if (value instanceof Fields fields) {
81-
return fields.asList().stream().map(it -> unpack(it, context)).collect(Collectors.toList());
81+
82+
List<Object> mapped = new ArrayList<>(fields.size());
83+
84+
for (Field field : fields) {
85+
mapped.add(unpack(field, context));
86+
}
87+
88+
return mapped;
8289
}
8390

8491
if (value instanceof Sort sort) {
@@ -98,7 +105,9 @@ private Object unpack(Object value, AggregationOperationContext context) {
98105
List<Object> sourceList = (List<Object>) value;
99106
List<Object> mappedList = new ArrayList<>(sourceList.size());
100107

101-
sourceList.stream().map((item) -> unpack(item, context)).forEach(mappedList::add);
108+
for (Object o : sourceList) {
109+
mappedList.add(unpack(o, context));
110+
}
102111

103112
return mappedList;
104113
}
@@ -150,7 +159,7 @@ protected List<Object> append(Object value) {
150159
return append(value, Expand.EXPAND_VALUES);
151160
}
152161

153-
@SuppressWarnings({ "unchecked", "rawtypes" })
162+
@SuppressWarnings({ "unchecked" })
154163
protected Map<String, Object> append(String key, Object value) {
155164

156165
Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map");
@@ -165,6 +174,7 @@ private Map<String, Object> append(Map<String, Object> existing, String key, Obj
165174
return clone;
166175
}
167176

177+
@SuppressWarnings("rawtypes")
168178
protected Map<String, Object> appendTo(String key, Object value) {
169179

170180
Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map");

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateOperators.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ public DateFromString fromString() {
826826
*/
827827
public TsIncrement tsIncrement() {
828828

829-
if(timezone != null && !Timezone.none().equals(timezone)) {
829+
if (timezone != null && !Timezone.none().equals(timezone)) {
830830
throw new IllegalArgumentException("$tsIncrement does not support timezones");
831831
}
832832

@@ -841,7 +841,7 @@ public TsIncrement tsIncrement() {
841841
*/
842842
public TsSecond tsSecond() {
843843

844-
if(timezone != null && !Timezone.none().equals(timezone)) {
844+
if (timezone != null && !Timezone.none().equals(timezone)) {
845845
throw new IllegalArgumentException("$tsSecond does not support timezones");
846846
}
847847

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DensifyOperation.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public DensifyRange incrementBy(Number step, DensifyUnit unit) {
190190

191191
/**
192192
* Set the {@link DensifyUnit unit} for the step field.
193-
*
193+
*
194194
* @param unit
195195
* @return this.
196196
*/
@@ -354,6 +354,8 @@ public DensifyOperationBuilder range(Range range) {
354354
*/
355355
public DensifyOperationBuilder fullRange(Consumer<DensifyRange> consumer) {
356356

357+
Assert.notNull(consumer, "Consumer must not be null");
358+
357359
DensifyRange range = Range.full();
358360
consumer.accept(range);
359361

@@ -374,7 +376,7 @@ public DensifyOperationBuilder partitionRange(Consumer<DensifyRange> consumer) {
374376
return range(range);
375377
}
376378

377-
DensifyOperation build() {
379+
public DensifyOperation build() {
378380
return new DensifyOperation(target.field, target.partitionBy, target.range);
379381
}
380382
}

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ObjectOperators.java

+95-18
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
import java.util.Arrays;
1919
import java.util.Collection;
2020
import java.util.Collections;
21-
import java.util.Set;
2221

2322
import org.bson.Document;
24-
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable;
2523
import org.springframework.util.Assert;
2624

2725
/**
@@ -135,7 +133,7 @@ public ObjectToArray toArray() {
135133
* @since 4.0
136134
*/
137135
public GetField getField(String fieldName) {
138-
return GetField.getField(fieldName).from(value);
136+
return GetField.getField(fieldName).of(value);
139137
}
140138

141139
/**
@@ -145,7 +143,7 @@ public GetField getField(String fieldName) {
145143
* @since 4.0
146144
*/
147145
public SetField setField(String fieldName) {
148-
return SetField.setField(fieldName).of(value);
146+
return SetField.field(fieldName).input(value);
149147
}
150148

151149
/**
@@ -155,7 +153,7 @@ public SetField setField(String fieldName) {
155153
* @since 4.0
156154
*/
157155
public AggregationExpression removeField(String fieldName) {
158-
return SetField.setField(fieldName).of(value).toValue(SystemVariable.REMOVE);
156+
return SetField.field(fieldName).input(value).toValue(SystemVariable.REMOVE);
159157
}
160158
}
161159

@@ -329,23 +327,49 @@ protected GetField(Object value) {
329327
super(value);
330328
}
331329

330+
/**
331+
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given {@code fieldName}.
332+
*
333+
* @param fieldName must not be {@literal null}.
334+
* @return new instance of {@link GetField}.
335+
*/
332336
public static GetField getField(String fieldName) {
333337
return new GetField(Collections.singletonMap("field", fieldName));
334338
}
335339

340+
/**
341+
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given {@link Field}.
342+
*
343+
* @param field must not be {@literal null}.
344+
* @return new instance of {@link GetField}.
345+
*/
336346
public static GetField getField(Field field) {
337347
return getField(field.getTarget());
338348
}
339349

340-
public GetField from(String fieldRef) {
341-
return from(Fields.field(fieldRef));
350+
/**
351+
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given
352+
* {@code field reference}.
353+
*
354+
* @param fieldRef must not be {@literal null}.
355+
* @return new instance of {@link GetField}.
356+
*/
357+
public GetField of(String fieldRef) {
358+
return of(Fields.field(fieldRef));
342359
}
343360

344-
public GetField from(AggregationExpression expression) {
345-
return from((Object) expression);
361+
/**
362+
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given
363+
* {@link AggregationExpression}.
364+
*
365+
* @param expression must not be {@literal null}.
366+
* @return new instance of {@link GetField}.
367+
*/
368+
public GetField of(AggregationExpression expression) {
369+
return of((Object) expression);
346370
}
347371

348-
private GetField from(Object fieldRef) {
372+
private GetField of(Object fieldRef) {
349373
return new GetField(append("input", fieldRef));
350374
}
351375

@@ -367,34 +391,87 @@ protected SetField(Object value) {
367391
super(value);
368392
}
369393

370-
public static SetField setField(String fieldName) {
394+
/**
395+
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
396+
* {@code fieldName}.
397+
*
398+
* @param fieldName must not be {@literal null}.
399+
* @return new instance of {@link SetField}.
400+
*/
401+
public static SetField field(String fieldName) {
371402
return new SetField(Collections.singletonMap("field", fieldName));
372403
}
373404

374-
public static SetField setField(Field field) {
375-
return setField(field.getTarget());
405+
/**
406+
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input {@link Field}.
407+
*
408+
* @param field must not be {@literal null}.
409+
* @return new instance of {@link SetField}.
410+
*/
411+
public static SetField field(Field field) {
412+
return field(field.getTarget());
376413
}
377414

378-
public SetField of(String fieldRef) {
379-
return of(Fields.field(fieldRef));
415+
/**
416+
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given input
417+
* {@code field reference}.
418+
*
419+
* @param fieldRef must not be {@literal null}.
420+
* @return new instance of {@link GetField}.
421+
*/
422+
public SetField input(String fieldRef) {
423+
return input(Fields.field(fieldRef));
380424
}
381425

382-
public SetField of(AggregationExpression expression) {
383-
return of((Object) expression);
426+
/**
427+
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
428+
* {@link AggregationExpression}.
429+
*
430+
* @param expression must not be {@literal null}.
431+
* @return new instance of {@link SetField}.
432+
*/
433+
public SetField input(AggregationExpression expression) {
434+
return input((Object) expression);
384435
}
385436

386-
private SetField of(Object fieldRef) {
437+
/**
438+
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
439+
* {@code field reference}.
440+
*
441+
* @param fieldRef must not be {@literal null}.
442+
* @return new instance of {@link SetField}.
443+
*/
444+
private SetField input(Object fieldRef) {
387445
return new SetField(append("input", fieldRef));
388446
}
389447

448+
/**
449+
* Creates new {@link SetField aggregation expression} providing the {@code value} using {@literal fieldReference}.
450+
*
451+
* @param fieldReference must not be {@literal null}.
452+
* @return new instance of {@link SetField}.
453+
*/
390454
public SetField toValueOf(String fieldReference) {
391455
return toValue(Fields.field(fieldReference));
392456
}
393457

458+
/**
459+
* Creates new {@link SetField aggregation expression} providing the {@code value} using
460+
* {@link AggregationExpression}.
461+
*
462+
* @param expression must not be {@literal null}.
463+
* @return new instance of {@link SetField}.
464+
*/
394465
public SetField toValueOf(AggregationExpression expression) {
395466
return toValue(expression);
396467
}
397468

469+
/**
470+
* Creates new {@link SetField aggregation expression} providing the {@code value}.
471+
*
472+
* @param value
473+
* @return new instance of {@link SetField}.
474+
*/
398475
public SetField toValue(Object value) {
399476
return new SetField(append("value", value));
400477
}

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SelectionOperators.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public static First first(int numberOfResults) {
261261
* Limits the number of returned elements to the given value.
262262
*
263263
* @param numberOfResults
264-
* @return new instance of {@link Bottom}.
264+
* @return new instance of {@link First}.
265265
*/
266266
public First limit(int numberOfResults) {
267267
return limit((Object) numberOfResults);
@@ -272,7 +272,7 @@ public First limit(int numberOfResults) {
272272
* expression}.
273273
*
274274
* @param expression must not be {@literal null}.
275-
* @return new instance of {@link Bottom}.
275+
* @return new instance of {@link First}.
276276
*/
277277
public First limit(AggregationExpression expression) {
278278
return limit((Object) expression);
@@ -286,7 +286,7 @@ private First limit(Object value) {
286286
* Define the field to serve as source.
287287
*
288288
* @param fieldName must not be {@literal null}.
289-
* @return new instance of {@link Bottom}.
289+
* @return new instance of {@link First}.
290290
*/
291291
public First of(String fieldName) {
292292
return input(fieldName);
@@ -296,7 +296,7 @@ public First of(String fieldName) {
296296
* Define the expression building the value to serve as source.
297297
*
298298
* @param expression must not be {@literal null}.
299-
* @return new instance of {@link Bottom}.
299+
* @return new instance of {@link First}.
300300
*/
301301
public First of(AggregationExpression expression) {
302302
return input(expression);
@@ -306,7 +306,7 @@ public First of(AggregationExpression expression) {
306306
* Define the field to serve as source.
307307
*
308308
* @param fieldName must not be {@literal null}.
309-
* @return new instance of {@link Bottom}.
309+
* @return new instance of {@link First}.
310310
*/
311311
public First input(String fieldName) {
312312
return new First(append("input", Fields.field(fieldName)));
@@ -316,7 +316,7 @@ public First input(String fieldName) {
316316
* Define the expression building the value to serve as source.
317317
*
318318
* @param expression must not be {@literal null}.
319-
* @return new instance of {@link Bottom}.
319+
* @return new instance of {@link First}.
320320
*/
321321
public First input(AggregationExpression expression) {
322322
return new First(append("input", expression));
@@ -355,7 +355,7 @@ public static Last last(int numberOfResults) {
355355
* Limits the number of returned elements to the given value.
356356
*
357357
* @param numberOfResults
358-
* @return new instance of {@link Bottom}.
358+
* @return new instance of {@link Last}.
359359
*/
360360
public Last limit(int numberOfResults) {
361361
return limit((Object) numberOfResults);
@@ -366,7 +366,7 @@ public Last limit(int numberOfResults) {
366366
* expression}.
367367
*
368368
* @param expression must not be {@literal null}.
369-
* @return new instance of {@link Bottom}.
369+
* @return new instance of {@link Last}.
370370
*/
371371
public Last limit(AggregationExpression expression) {
372372
return limit((Object) expression);
@@ -380,7 +380,7 @@ private Last limit(Object value) {
380380
* Define the field to serve as source.
381381
*
382382
* @param fieldName must not be {@literal null}.
383-
* @return new instance of {@link Bottom}.
383+
* @return new instance of {@link Last}.
384384
*/
385385
public Last of(String fieldName) {
386386
return input(fieldName);
@@ -390,7 +390,7 @@ public Last of(String fieldName) {
390390
* Define the expression building the value to serve as source.
391391
*
392392
* @param expression must not be {@literal null}.
393-
* @return new instance of {@link Bottom}.
393+
* @return new instance of {@link Last}.
394394
*/
395395
public Last of(AggregationExpression expression) {
396396
return input(expression);
@@ -400,7 +400,7 @@ public Last of(AggregationExpression expression) {
400400
* Define the field to serve as source.
401401
*
402402
* @param fieldName must not be {@literal null}.
403-
* @return new instance of {@link Bottom}.
403+
* @return new instance of {@link Last}.
404404
*/
405405
public Last input(String fieldName) {
406406
return new Last(append("input", Fields.field(fieldName)));
@@ -410,7 +410,7 @@ public Last input(String fieldName) {
410410
* Define the expression building the value to serve as source.
411411
*
412412
* @param expression must not be {@literal null}.
413-
* @return new instance of {@link Bottom}.
413+
* @return new instance of {@link Last}.
414414
*/
415415
public Last input(AggregationExpression expression) {
416416
return new Last(append("input", expression));

0 commit comments

Comments
 (0)