Skip to content

Commit d68a812

Browse files
committed
Polishing.
Omit StreamUtils usage if input is a collection. Remove superfluous Flux.from(…). Simplify test and migrate test to JUnit 5. See #3609. Original pull request: #3611.
1 parent ccb9f11 commit d68a812

File tree

3 files changed

+79
-71
lines changed

3 files changed

+79
-71
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.springframework.data.mongodb.core.query.Criteria.*;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collection;
2122
import java.util.Collections;
2223
import java.util.List;
2324
import java.util.Optional;
@@ -215,7 +216,7 @@ public Iterable<T> findAllById(Iterable<ID> ids) {
215216
Assert.notNull(ids, "The given Ids of entities not be null!");
216217

217218
return findAll(new Query(new Criteria(entityInformation.getIdAttribute())
218-
.in(Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()))));
219+
.in(toCollection(ids))));
219220
}
220221

221222
/*
@@ -266,10 +267,10 @@ public <S extends T> List<S> insert(Iterable<S> entities) {
266267

267268
Assert.notNull(entities, "The given Iterable of entities not be null!");
268269

269-
List<S> list = Streamable.of(entities).stream().collect(StreamUtils.toUnmodifiableList());
270+
Collection<S> list = toCollection(entities);
270271

271272
if (list.isEmpty()) {
272-
return list;
273+
return Collections.emptyList();
273274
}
274275

275276
return new ArrayList<>(mongoOperations.insertAll(list));
@@ -374,6 +375,11 @@ private Criteria getIdCriteria(Object id) {
374375
return where(entityInformation.getIdAttribute()).is(id);
375376
}
376377

378+
private static <E> Collection<E> toCollection(Iterable<E> ids) {
379+
return ids instanceof Collection ? (Collection<E>) ids
380+
: StreamUtils.createStreamFromIterator(ids.iterator()).collect(Collectors.toList());
381+
}
382+
377383
private List<T> findAll(@Nullable Query query) {
378384

379385
if (query == null) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
import reactor.core.publisher.Mono;
2222

2323
import java.io.Serializable;
24-
import java.util.List;
24+
import java.util.Collection;
2525
import java.util.stream.Collectors;
2626

2727
import org.reactivestreams.Publisher;
28+
2829
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2930
import org.springframework.dao.OptimisticLockingFailureException;
3031
import org.springframework.data.domain.Example;
@@ -174,7 +175,7 @@ public Flux<T> findAllById(Iterable<ID> ids) {
174175
Assert.notNull(ids, "The given Iterable of Id's must not be null!");
175176

176177
return findAll(new Query(new Criteria(entityInformation.getIdAttribute())
177-
.in(Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()))));
178+
.in(toCollection(ids))));
178179
}
179180

180181
/*
@@ -275,9 +276,9 @@ public <S extends T> Flux<S> insert(Iterable<S> entities) {
275276

276277
Assert.notNull(entities, "The given Iterable of entities must not be null!");
277278

278-
List<S> source = Streamable.of(entities).stream().collect(StreamUtils.toUnmodifiableList());
279+
Collection<S> source = toCollection(entities);
279280

280-
return source.isEmpty() ? Flux.empty() : Flux.from(mongoOperations.insertAll(source));
281+
return source.isEmpty() ? Flux.empty() : mongoOperations.insertAll(source);
281282
}
282283

283284
/*
@@ -437,8 +438,12 @@ private Criteria getIdCriteria(Object id) {
437438
return where(entityInformation.getIdAttribute()).is(id);
438439
}
439440

440-
private Flux<T> findAll(Query query) {
441+
private static <E> Collection<E> toCollection(Iterable<E> ids) {
442+
return ids instanceof Collection ? (Collection<E>) ids
443+
: StreamUtils.createStreamFromIterator(ids.iterator()).collect(Collectors.toList());
444+
}
441445

446+
private Flux<T> findAll(Query query) {
442447
return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
443448
}
444449
}

0 commit comments

Comments
 (0)