Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Consistently convert Iterable to Collection for identifier parameters.

See #3580
  • Loading branch information
mp911de committed Aug 14, 2024
1 parent f7aed2a commit 298c1c6
Showing 1 changed file with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.KeysetScrollPosition;
Expand Down Expand Up @@ -246,14 +244,8 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
/*
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
*/

if (ids instanceof Collection) {
query.setParameter("ids", ids);
} else {
Collection<ID> idsCollection = StreamSupport.stream(ids.spliterator(), false)
.collect(Collectors.toCollection(ArrayList::new));
query.setParameter("ids", idsCollection);
}
Collection<ID> idCollection = toCollection(ids);
query.setParameter("ids", idCollection);

applyQueryHints(query);

Expand Down Expand Up @@ -414,7 +406,7 @@ public List<T> findAllById(Iterable<ID> ids) {
return results;
}

Collection<ID> idCollection = Streamable.of(ids).toList();
Collection<ID> idCollection = toCollection(ids);

ByIdsSpecification<T> specification = new ByIdsSpecification<>(entityInformation);
TypedQuery<T> query = getQuery(specification, Sort.unsorted());
Expand Down Expand Up @@ -918,6 +910,11 @@ private ProjectionFactory getProjectionFactory() {
return projectionFactory;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private static <T> Collection<T> toCollection(Iterable<T> ids) {
return ids instanceof Collection c ? c : Streamable.of(ids).toList();
}

/**
* Executes a count query and transparently sums up all values returned.
*
Expand Down

0 comments on commit 298c1c6

Please sign in to comment.