From 1e7594359a11f827c16dd9d75e2bb351be51ef82 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Thu, 4 Jul 2019 10:09:12 +0200 Subject: [PATCH] DATAJPA-433 - Polishing. Extracted conversion of iterable into a method and added a shortcut for the likely case that we already have a collection. Original pull request: #185. --- .../support/SimpleJpaRepository.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 708226336..898ed0c21 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -87,6 +87,19 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation Collection toCollection(Iterable ts) { + + if (ts instanceof Collection) { + return (Collection) ts; + } + + List tCollection = new ArrayList(); + for (T t : ts) { + tCollection.add(t); + } + return tCollection; + } + /** * Creates a new {@link SimpleJpaRepository} to manage objects of the given {@link JpaEntityInformation}. * @@ -349,11 +362,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation idCollection = new ArrayList(); - for (ID id : ids) { - idCollection.add(id); - } + Collection idCollection = toCollection(ids); ByIdsSpecification specification = new ByIdsSpecification(entityInformation); TypedQuery query = getQuery(specification, Sort.unsorted()); @@ -430,8 +439,9 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation Optional findOne(Example example) { try { - return Optional.of( - getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), Sort.unsorted()).getSingleResult()); + return Optional + .of(getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), Sort.unsorted()) + .getSingleResult()); } catch (NoResultException e) { return Optional.empty(); } @@ -443,7 +453,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation long count(Example example) { - return executeCountQuery(getCountQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType())); + return executeCountQuery( + getCountQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType())); } /* @@ -452,8 +463,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation boolean exists(Example example) { - return !getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), Sort.unsorted()).getResultList() - .isEmpty(); + return !getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), Sort.unsorted()) + .getResultList().isEmpty(); } /* @@ -462,7 +473,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation List findAll(Example example) { - return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), Sort.unsorted()).getResultList(); + return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), Sort.unsorted()) + .getResultList(); } /* @@ -471,7 +483,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation List findAll(Example example, Sort sort) { - return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), sort).getResultList(); + return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), sort) + .getResultList(); } /*