Skip to content

Commit 2367379

Browse files
mp911dechristophstrobl
authored andcommitted
Use Java 8 Stream as return type for Template operations returning a stream.
We now use Stream instead of CloseableIterator for easier stream creation. Closes: #3944 Original Pull Request: #3946
1 parent a1c483f commit 2367379

File tree

12 files changed

+146
-169
lines changed

12 files changed

+146
-169
lines changed

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18+
import java.util.stream.Stream;
19+
1820
import org.springframework.data.mongodb.core.aggregation.Aggregation;
1921
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
20-
import org.springframework.data.util.CloseableIterator;
2122

2223
/**
2324
* {@link ExecutableAggregationOperation} allows creation and execution of MongoDB aggregation operations in a fluent
@@ -88,12 +89,12 @@ interface TerminatingAggregation<T> {
8889

8990
/**
9091
* Apply pipeline operations as specified and stream all matching elements. <br />
91-
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.FindIterable}
92+
* Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.FindIterable}
9293
*
93-
* @return a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.FindIterable} that needs to be closed.
94-
* Never {@literal null}.
94+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
95+
* through a try-with-resources clause).
9596
*/
96-
CloseableIterator<T> stream();
97+
Stream<T> stream();
9798
}
9899

99100
/**

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18+
import java.util.stream.Stream;
19+
1820
import org.springframework.data.mongodb.core.aggregation.Aggregation;
1921
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
2022
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
21-
import org.springframework.data.util.CloseableIterator;
2223
import org.springframework.util.Assert;
2324
import org.springframework.util.StringUtils;
2425

@@ -87,7 +88,7 @@ public AggregationResults<T> all() {
8788
}
8889

8990
@Override
90-
public CloseableIterator<T> stream() {
91+
public Stream<T> stream() {
9192
return template.aggregateStream(aggregation, getCollectionName(aggregation), domainType);
9293
}
9394

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ default Optional<T> first() {
118118
/**
119119
* Stream all matching elements.
120120
*
121-
* @return a {@link Stream} that wraps the a Mongo DB {@link com.mongodb.client.FindIterable} that needs to be closed. Never
122-
* {@literal null}.
121+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
122+
* through a try-with-resources clause).
123123
*/
124124
Stream<T> stream();
125125

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
import java.util.stream.Stream;
2121

2222
import org.bson.Document;
23+
2324
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2425
import org.springframework.data.mongodb.core.query.NearQuery;
2526
import org.springframework.data.mongodb.core.query.Query;
2627
import org.springframework.data.mongodb.core.query.SerializationUtils;
27-
import org.springframework.data.util.CloseableIterator;
28-
import org.springframework.data.util.StreamUtils;
2928
import org.springframework.lang.Nullable;
3029
import org.springframework.util.Assert;
3130
import org.springframework.util.ObjectUtils;
@@ -70,11 +69,11 @@ static class ExecutableFindSupport<T>
7069
private final MongoTemplate template;
7170
private final Class<?> domainType;
7271
private final Class<T> returnType;
73-
@Nullable private final String collection;
72+
private final @Nullable String collection;
7473
private final Query query;
7574

7675
ExecutableFindSupport(MongoTemplate template, Class<?> domainType, Class<T> returnType,
77-
String collection, Query query) {
76+
@Nullable String collection, Query query) {
7877
this.template = template;
7978
this.domainType = domainType;
8079
this.returnType = returnType;
@@ -137,7 +136,7 @@ public List<T> all() {
137136

138137
@Override
139138
public Stream<T> stream() {
140-
return StreamUtils.createStreamFromIterator(doStream());
139+
return doStream();
141140
}
142141

143142
@Override
@@ -179,7 +178,7 @@ private List<T> doFindDistinct(String field) {
179178
returnType == domainType ? (Class<T>) Object.class : returnType);
180179
}
181180

182-
private CloseableIterator<T> doStream() {
181+
private Stream<T> doStream() {
183182
return template.doStream(query, domainType, getCollectionName(), returnType);
184183
}
185184

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

+39-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2021 the original author or authors.
2+
* Copyright 2011-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,8 +20,10 @@
2020
import java.util.Set;
2121
import java.util.function.Consumer;
2222
import java.util.function.Supplier;
23+
import java.util.stream.Stream;
2324

2425
import org.bson.Document;
26+
2527
import org.springframework.data.geo.GeoResults;
2628
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
2729
import org.springframework.data.mongodb.core.aggregation.Aggregation;
@@ -42,7 +44,6 @@
4244
import org.springframework.data.mongodb.core.query.Query;
4345
import org.springframework.data.mongodb.core.query.Update;
4446
import org.springframework.data.mongodb.core.query.UpdateDefinition;
45-
import org.springframework.data.util.CloseableIterator;
4647
import org.springframework.lang.Nullable;
4748
import org.springframework.util.Assert;
4849
import org.springframework.util.ClassUtils;
@@ -225,34 +226,34 @@ public <T> T execute(SessionCallback<T> action, Consumer<ClientSession> onComple
225226
* Executes the given {@link Query} on the entity collection of the specified {@code entityType} backed by a Mongo DB
226227
* {@link com.mongodb.client.FindIterable}.
227228
* <p>
228-
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.FindIterable} that needs to
229-
* be closed.
229+
* Returns a {@link String} that wraps the Mongo DB {@link com.mongodb.client.FindIterable} that needs to be closed.
230230
*
231231
* @param query the query class that specifies the criteria used to find a record and also an optional fields
232232
* specification. Must not be {@literal null}.
233233
* @param entityType must not be {@literal null}.
234234
* @param <T> element return type
235-
* @return will never be {@literal null}.
235+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
236+
* through a try-with-resources clause).
236237
* @since 1.7
237238
*/
238-
<T> CloseableIterator<T> stream(Query query, Class<T> entityType);
239+
<T> Stream<T> stream(Query query, Class<T> entityType);
239240

240241
/**
241242
* Executes the given {@link Query} on the entity collection of the specified {@code entityType} and collection backed
242243
* by a Mongo DB {@link com.mongodb.client.FindIterable}.
243244
* <p>
244-
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.FindIterable} that needs to
245-
* be closed.
245+
* Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.FindIterable} that needs to be closed.
246246
*
247247
* @param query the query class that specifies the criteria used to find a record and also an optional fields
248248
* specification. Must not be {@literal null}.
249249
* @param entityType must not be {@literal null}.
250250
* @param collectionName must not be {@literal null} or empty.
251251
* @param <T> element return type
252-
* @return will never be {@literal null}.
252+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
253+
* through a try-with-resources clause).
253254
* @since 1.10
254255
*/
255-
<T> CloseableIterator<T> stream(Query query, Class<T> entityType, String collectionName);
256+
<T> Stream<T> stream(Query query, Class<T> entityType, String collectionName);
256257

257258
/**
258259
* Create an uncapped collection with a name based on the provided entity class.
@@ -521,9 +522,9 @@ <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionN
521522
/**
522523
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
523524
* <p>
524-
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
525-
* needs to be closed. The raw results will be mapped to the given entity class. The name of the inputCollection is
526-
* derived from the inputType of the aggregation.
525+
* Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
526+
* closed. The raw results will be mapped to the given entity class. The name of the inputCollection is derived from
527+
* the inputType of the aggregation.
527528
* <p>
528529
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
529530
* explanation mode will throw an {@link IllegalArgumentException}.
@@ -532,35 +533,37 @@ <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionN
532533
* {@literal null}.
533534
* @param collectionName The name of the input collection to use for the aggreation.
534535
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
535-
* @return The results of the aggregation operation.
536+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
537+
* through a try-with-resources clause).
536538
* @since 2.0
537539
*/
538-
<O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, String collectionName, Class<O> outputType);
540+
<O> Stream<O> aggregateStream(TypedAggregation<?> aggregation, String collectionName, Class<O> outputType);
539541

540542
/**
541543
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
542-
* <br />
543-
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
544-
* needs to be closed. The raw results will be mapped to the given entity class and are returned as stream. The name
545-
* of the inputCollection is derived from the inputType of the aggregation.
546-
* <br />
544+
* <p>
545+
* Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
546+
* closed. The raw results will be mapped to the given entity class and are returned as stream. The name of the
547+
* inputCollection is derived from the inputType of the aggregation.
548+
* <p>
547549
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
548550
* explanation mode will throw an {@link IllegalArgumentException}.
549551
*
550552
* @param aggregation The {@link TypedAggregation} specification holding the aggregation operations, must not be
551553
* {@literal null}.
552554
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
553-
* @return The results of the aggregation operation.
555+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
556+
* through a try-with-resources clause).
554557
* @since 2.0
555558
*/
556-
<O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, Class<O> outputType);
559+
<O> Stream<O> aggregateStream(TypedAggregation<?> aggregation, Class<O> outputType);
557560

558561
/**
559562
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
560-
* <br />
561-
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
562-
* needs to be closed. The raw results will be mapped to the given entity class.
563-
* <br />
563+
* <p>
564+
* Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
565+
* closed. The raw results will be mapped to the given entity class.
566+
* <p>
564567
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
565568
* explanation mode will throw an {@link IllegalArgumentException}.
566569
*
@@ -569,17 +572,18 @@ <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionN
569572
* @param inputType the inputType where the aggregation operation will read from, must not be {@literal null} or
570573
* empty.
571574
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
572-
* @return The results of the aggregation operation.
575+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
576+
* through a try-with-resources clause).
573577
* @since 2.0
574578
*/
575-
<O> CloseableIterator<O> aggregateStream(Aggregation aggregation, Class<?> inputType, Class<O> outputType);
579+
<O> Stream<O> aggregateStream(Aggregation aggregation, Class<?> inputType, Class<O> outputType);
576580

577581
/**
578582
* Execute an aggregation operation backed by a Mongo DB {@link com.mongodb.client.AggregateIterable}.
579-
* <br />
580-
* Returns a {@link CloseableIterator} that wraps the a Mongo DB {@link com.mongodb.client.AggregateIterable} that
581-
* needs to be closed. The raw results will be mapped to the given entity class.
582-
* <br />
583+
* <p>
584+
* Returns a {@link Stream} that wraps the Mongo DB {@link com.mongodb.client.AggregateIterable} that needs to be
585+
* closed. The raw results will be mapped to the given entity class.
586+
* <p>
583587
* Aggregation streaming can't be used with {@link AggregationOptions#isExplain() aggregation explain}. Enabling
584588
* explanation mode will throw an {@link IllegalArgumentException}.
585589
*
@@ -588,10 +592,11 @@ <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionN
588592
* @param collectionName the collection where the aggregation operation will read from, must not be {@literal null} or
589593
* empty.
590594
* @param outputType The parametrized type of the returned list, must not be {@literal null}.
591-
* @return The results of the aggregation operation.
595+
* @return the result {@link Stream}, containing mapped objects, needing to be closed once fully processed (e.g.
596+
* through a try-with-resources clause).
592597
* @since 2.0
593598
*/
594-
<O> CloseableIterator<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType);
599+
<O> Stream<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType);
595600

596601
/**
597602
* Execute a map-reduce operation. The map-reduce operation will be formed with an output type of INLINE

0 commit comments

Comments
 (0)