Skip to content

Commit deed191

Browse files
committed
DATAMONGO-1563 - Polishing.
Rename TerminatingAggregationOperation.get() to TerminatingAggregationOperation.all() to name methods consistently. Extract collection name retrieval to method. Javadoc, formatting, add generics where required/use diamond syntax where applicable. Original pull request: #466.
1 parent c5f2abe commit deed191

17 files changed

+136
-124
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* </pre>
3737
*
3838
* @author Christoph Strobl
39+
* @author Mark Paluch
3940
* @since 2.0
4041
*/
4142
public interface ExecutableAggregationOperation {
@@ -54,7 +55,6 @@ public interface ExecutableAggregationOperation {
5455
/**
5556
* Collection override (Optional).
5657
*
57-
* @param <T>
5858
* @author Christoph Strobl
5959
* @since 2.0
6060
*/
@@ -74,19 +74,20 @@ interface AggregationOperationWithCollection<T> {
7474
/**
7575
* Trigger execution by calling one of the terminating methods.
7676
*
77-
* @param <T>
77+
* @author Christoph Strobl
78+
* @since 2.0
7879
*/
7980
interface TerminatingAggregationOperation<T> {
8081

8182
/**
82-
* Apply pipeline operations as specified.
83+
* Apply pipeline operations as specified and get all matching elements.
8384
*
8485
* @return never {@literal null}.
8586
*/
86-
AggregationResults<T> get();
87+
AggregationResults<T> all();
8788

8889
/**
89-
* Apply pipeline operations as specified. <br />
90+
* Apply pipeline operations as specified and stream all matching elements. <br />
9091
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.Cursor}
9192
*
9293
* @return a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.Cursor} that needs to be closed.
@@ -98,7 +99,6 @@ interface TerminatingAggregationOperation<T> {
9899
/**
99100
* Define the aggregation with pipeline stages.
100101
*
101-
* @param <T>
102102
* @author Christoph Strobl
103103
* @since 2.0
104104
*/
@@ -115,7 +115,6 @@ interface AggregationOperationWithAggregation<T> {
115115
}
116116

117117
/**
118-
* @param <T>
119118
* @author Christoph Strobl
120119
* @since 2.0
121120
*/

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,27 @@ class ExecutableAggregationOperationSupport implements ExecutableAggregationOper
3535
private final MongoTemplate template;
3636

3737
/**
38-
* Create new instance of ExecutableAggregationOperationSupport.
38+
* Create new instance of {@link ExecutableAggregationOperationSupport}.
3939
*
4040
* @param template must not be {@literal null}.
4141
* @throws IllegalArgumentException if template is {@literal null}.
4242
*/
4343
ExecutableAggregationOperationSupport(MongoTemplate template) {
4444

4545
Assert.notNull(template, "Template must not be null!");
46+
4647
this.template = template;
4748
}
4849

4950
@Override
5051
public <T> AggregationOperation<T> aggregateAndReturn(Class<T> domainType) {
5152

5253
Assert.notNull(domainType, "DomainType must not be null!");
53-
return new AggregationOperationSupport<T>(template, null, domainType, null);
54+
55+
return new AggregationOperationSupport<>(template, null, domainType, null);
5456
}
5557

5658
/**
57-
* @param <T>
5859
* @author Christoph Strobl
5960
* @since 2.0
6061
*/
@@ -71,18 +72,20 @@ static class AggregationOperationSupport<T>
7172
public AggregationOperationWithAggregation<T> inCollection(String collection) {
7273

7374
Assert.hasText(collection, "Collection must not be null nor empty!");
74-
return new AggregationOperationSupport<T>(template, aggregation, domainType, collection);
75+
76+
return new AggregationOperationSupport<>(template, aggregation, domainType, collection);
7577
}
7678

7779
@Override
7880
public TerminatingAggregationOperation<T> by(Aggregation aggregation) {
7981

8082
Assert.notNull(aggregation, "Aggregation must not be null!");
81-
return new AggregationOperationSupport<T>(template, aggregation, domainType, collection);
83+
84+
return new AggregationOperationSupport<>(template, aggregation, domainType, collection);
8285
}
8386

8487
@Override
85-
public AggregationResults<T> get() {
88+
public AggregationResults<T> all() {
8689
return template.aggregate(aggregation, getCollectionName(aggregation), domainType);
8790
}
8891

@@ -99,8 +102,10 @@ private String getCollectionName(Aggregation aggregation) {
99102

100103
if (aggregation instanceof TypedAggregation) {
101104

102-
if (((TypedAggregation<?>) aggregation).getInputType() != null) {
103-
return template.determineCollectionName(((TypedAggregation<?>) aggregation).getInputType());
105+
TypedAggregation<?> typedAggregation = (TypedAggregation<?>) aggregation;
106+
107+
if (typedAggregation.getInputType() != null) {
108+
return template.determineCollectionName(typedAggregation.getInputType());
104109
}
105110
}
106111

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
* {@link ExecutableFindOperation} allows creation and execution of MongoDB find operations in a fluent API style.
2828
* <br />
2929
* The starting {@literal domainType} is used for mapping the {@link Query} provided via {@code matching} into the
30-
* MongoDB specific representation. By default this originating {@literal domainType} is also used for mapping back the
31-
* result from the {@link org.bson.Document}. However it is possible to define an different {@literal returnType} via
32-
* {@code as} that is then used for mapping the result mapping. <br />
30+
* MongoDB specific representation. By default, the originating {@literal domainType} is also used for mapping back the
31+
* result from the {@link org.bson.Document}. However, it is possible to define an different {@literal returnType} via
32+
* {@code as} to mapping the result.<br />
3333
* The collection to operate on is by default derived from the initial {@literal domainType} and can be defined there
3434
* via {@link org.springframework.data.mongodb.core.mapping.Document}. Using {@code inCollection} allows to override the
3535
* collection name for the execution.
@@ -45,6 +45,7 @@
4545
* </pre>
4646
*
4747
* @author Christoph Strobl
48+
* @author Mark Paluch
4849
* @since 2.0
4950
*/
5051
public interface ExecutableFindOperation {
@@ -61,7 +62,6 @@ public interface ExecutableFindOperation {
6162
/**
6263
* Trigger find execution by calling one of the terminating methods.
6364
*
64-
* @param <T>
6565
* @author Christoph Strobl
6666
* @since 2.0
6767
*/
@@ -85,7 +85,7 @@ interface TerminatingFindOperation<T> {
8585
/**
8686
* Get all matching elements.
8787
*
88-
* @return never {@literal}.
88+
* @return never {@literal null}.
8989
*/
9090
List<T> all();
9191

@@ -101,7 +101,6 @@ interface TerminatingFindOperation<T> {
101101
/**
102102
* Trigger geonear execution by calling one of the terminating methods.
103103
*
104-
* @param <T>
105104
* @author Christoph Strobl
106105
* @since 2.0
107106
*/
@@ -118,7 +117,6 @@ interface TerminatingFindNearOperation<T> {
118117
/**
119118
* Terminating operations invoking the actual query execution.
120119
*
121-
* @param <T>
122120
* @author Christoph Strobl
123121
* @since 2.0
124122
*/
@@ -146,7 +144,6 @@ interface FindOperationWithQuery<T> extends TerminatingFindOperation<T> {
146144
/**
147145
* Collection override (Optional).
148146
*
149-
* @param <T>
150147
* @author Christoph Strobl
151148
* @since 2.0
152149
*/
@@ -166,7 +163,6 @@ interface FindOperationWithCollection<T> extends FindOperationWithQuery<T> {
166163
/**
167164
* Result type override (Optional).
168165
*
169-
* @param <T>
170166
* @author Christoph Strobl
171167
* @since 2.0
172168
*/
@@ -187,11 +183,8 @@ interface FindOperationWithProjection<T> extends FindOperationWithQuery<T> {
187183
/**
188184
* {@link FindOperation} provides methods for constructing lookup operations in a fluent way.
189185
*
190-
* @param <T>
191186
* @author Christoph Strobl
192187
* @since 2.0
193188
*/
194-
interface FindOperation<T> extends FindOperationWithCollection<T>, FindOperationWithProjection<T> {
195-
196-
}
189+
interface FindOperation<T> extends FindOperationWithCollection<T>, FindOperationWithProjection<T> {}
197190
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public Optional<T> one() {
123123
public Optional<T> first() {
124124

125125
List<T> result = doFind(new DelegatingQueryCursorPreparer(getCursorPreparer(query, null)).limit(1));
126+
126127
return ObjectUtils.isEmpty(result) ? Optional.empty() : Optional.of(result.iterator().next());
127128
}
128129

@@ -170,7 +171,6 @@ private String asString() {
170171
}
171172

172173
/**
173-
* @param <T>
174174
* @author Christoph Strobl
175175
* @since 2.0
176176
*/
@@ -179,15 +179,16 @@ static class DelegatingQueryCursorPreparer implements CursorPreparer {
179179
private final CursorPreparer delegate;
180180
private Optional<Integer> limit = Optional.empty();
181181

182-
public DelegatingQueryCursorPreparer(CursorPreparer delegate) {
182+
DelegatingQueryCursorPreparer(CursorPreparer delegate) {
183183
this.delegate = delegate;
184184
}
185185

186186
@Override
187187
public FindIterable<Document> prepare(FindIterable<Document> cursor) {
188188

189189
FindIterable<Document> target = delegate.prepare(cursor);
190-
return limit.map(it -> target.limit(it)).orElse(target);
190+
191+
return limit.map(target::limit).orElse(target);
191192
}
192193

193194
CursorPreparer limit(int limit) {

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.mongodb.bulk.BulkWriteResult;
2323

2424
/**
25-
* {@link ExecutableFindOperation} allows creation and execution of MongoDB insert and bulk insert operations in a
25+
* {@link ExecutableInsertOperation} allows creation and execution of MongoDB insert and bulk insert operations in a
2626
* fluent API style. <br />
2727
* The collection to operate on is by default derived from the initial {@literal domainType} and can be defined there
2828
* via {@link org.springframework.data.mongodb.core.mapping.Document}. Using {@code inCollection} allows to override the
@@ -53,7 +53,6 @@ public interface ExecutableInsertOperation {
5353
/**
5454
* Trigger insert execution by calling one of the terminating methods.
5555
*
56-
* @param <T>
5756
* @author Christoph Strobl
5857
* @since 2.0
5958
*/
@@ -79,7 +78,6 @@ interface TerminatingInsertOperation<T> extends TerminatingBulkInsertOperation<T
7978
/**
8079
* Trigger bulk insert execution by calling one of the terminating methods.
8180
*
82-
* @param <T>
8381
* @author Christoph Strobl
8482
* @since 2.0
8583
*/
@@ -96,19 +94,15 @@ interface TerminatingBulkInsertOperation<T> {
9694
}
9795

9896
/**
99-
* @param <T>
10097
* @author Christoph Strobl
10198
* @since 2.0
10299
*/
103100
interface InsertOperation<T>
104-
extends TerminatingInsertOperation<T>, InsertOperationWithCollection<T>, InsertOperationWithBulkMode<T> {
105-
106-
}
101+
extends TerminatingInsertOperation<T>, InsertOperationWithCollection<T>, InsertOperationWithBulkMode<T> {}
107102

108103
/**
109104
* Collection override (Optional).
110105
*
111-
* @param <T>
112106
* @author Christoph Strobl
113107
* @since 2.0
114108
*/
@@ -126,7 +120,6 @@ interface InsertOperationWithCollection<T> {
126120
}
127121

128122
/**
129-
* @param <T>
130123
* @author Christoph Strobl
131124
* @since 2.0
132125
*/
@@ -135,7 +128,7 @@ interface InsertOperationWithBulkMode<T> extends TerminatingInsertOperation<T> {
135128
/**
136129
* Define the {@link BulkMode} to use for bulk insert operation.
137130
*
138-
* @param mode must not be {@literal null}.
131+
* @param bulkMode must not be {@literal null}.
139132
* @return new instance of {@link TerminatingBulkInsertOperation}.
140133
* @throws IllegalArgumentException if bulkMode is {@literal null}.
141134
*/

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class ExecutableInsertOperationSupport implements ExecutableInsertOperation {
5353
public <T> InsertOperation<T> insert(Class<T> domainType) {
5454

5555
Assert.notNull(domainType, "DomainType must not be null!");
56-
return new InsertOperationSupport<T>(template, domainType, null, null);
56+
57+
return new InsertOperationSupport<>(template, domainType, null, null);
5758
}
5859

5960
/**
60-
* @param <T>
6161
* @author Christoph Strobl
6262
* @since 2.0
6363
*/
@@ -73,41 +73,45 @@ static class InsertOperationSupport<T> implements InsertOperation<T> {
7373
public void one(T object) {
7474

7575
Assert.notNull(object, "Object must not be null!");
76+
7677
template.insert(object, getCollectionName());
7778
}
7879

7980
@Override
8081
public void all(Collection<? extends T> objects) {
8182

8283
Assert.notNull(objects, "Objects must not be null!");
84+
8385
template.insert(objects, getCollectionName());
8486
}
8587

8688
@Override
8789
public BulkWriteResult bulk(Collection<? extends T> objects) {
8890

8991
Assert.notNull(objects, "Objects must not be null!");
92+
9093
return template.bulkOps(bulkMode != null ? bulkMode : BulkMode.ORDERED, domainType, getCollectionName())
9194
.insert(new ArrayList<>(objects)).execute();
9295
}
9396

9497
@Override
95-
public InsertOperationWithBulkMode inCollection(String collection) {
98+
public InsertOperationWithBulkMode<T> inCollection(String collection) {
9699

97100
Assert.hasText(collection, "Collection must not be null nor empty.");
98-
return new InsertOperationSupport<T>(template, domainType, collection, bulkMode);
101+
102+
return new InsertOperationSupport<>(template, domainType, collection, bulkMode);
99103
}
100104

101105
@Override
102-
public TerminatingBulkInsertOperation withBulkMode(BulkMode bulkMode) {
106+
public TerminatingBulkInsertOperation<T> withBulkMode(BulkMode bulkMode) {
103107

104108
Assert.notNull(bulkMode, "BulkMode must not be null!");
105-
return new InsertOperationSupport<T>(template, domainType, collection, bulkMode);
109+
110+
return new InsertOperationSupport<>(template, domainType, collection, bulkMode);
106111
}
107112

108113
private String getCollectionName() {
109114
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
110115
}
111-
112116
}
113117
}

0 commit comments

Comments
 (0)