From 9146357b5176541245cdb14fea7cd86099c6f930 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 11 Feb 2022 10:37:26 +0100 Subject: [PATCH] Polishing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let List… interface variants extend their base parent so that List-based variants participate in the type hierarchy. See #2538 Original pull request: #2538. --- .../ListQuerydslPredicateExecutor.java | 60 ++------------ .../querydsl/QuerydslPredicateExecutor.java | 1 + .../data/repository/ListCrudRepository.java | 80 +------------------ .../ListPagingAndSortingRepository.java | 46 +++++++++++ .../query/ListQueryByExampleExecutor.java | 56 ++----------- .../query/QueryByExampleExecutor.java | 1 + 6 files changed, 64 insertions(+), 180 deletions(-) create mode 100644 src/main/java/org/springframework/data/repository/ListPagingAndSortingRepository.java diff --git a/src/main/java/org/springframework/data/querydsl/ListQuerydslPredicateExecutor.java b/src/main/java/org/springframework/data/querydsl/ListQuerydslPredicateExecutor.java index c2fb073a0..2194afbf8 100644 --- a/src/main/java/org/springframework/data/querydsl/ListQuerydslPredicateExecutor.java +++ b/src/main/java/org/springframework/data/querydsl/ListQuerydslPredicateExecutor.java @@ -16,36 +16,21 @@ package org.springframework.data.querydsl; import java.util.List; -import java.util.Optional; -import java.util.function.Function; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -import org.springframework.data.repository.query.FluentQuery; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Predicate; /** - * Interface to allow execution of QueryDsl {@link Predicate} instances.This identical to * - * {@link QuerydslPredicateExecutor} but returns {@link List} instead of {@link Iterable} where applicable. + * Interface to allow execution of QueryDsl {@link Predicate} instances. This an extension to + * {@link QuerydslPredicateExecutor} returning {@link List} instead of {@link Iterable} where applicable. * * @author Jens Schauder * @since 3.0 * @see QuerydslPredicateExecutor */ -public interface ListQuerydslPredicateExecutor { - - /** - * Returns a single entity matching the given {@link Predicate} or {@link Optional#empty()} if none was found. - * - * @param predicate must not be {@literal null}. - * @return a single entity matching the given {@link Predicate} or {@link Optional#empty()} if none was found. - * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the predicate yields more than one - * result. - */ - Optional findOne(Predicate predicate); +public interface ListQuerydslPredicateExecutor extends QuerydslPredicateExecutor { /** * Returns all entities matching the given {@link Predicate}. In case no match could be found an empty {@link List} is @@ -54,6 +39,7 @@ public interface ListQuerydslPredicateExecutor { * @param predicate must not be {@literal null}. * @return all entities matching the given {@link Predicate}. */ + @Override List findAll(Predicate predicate); /** @@ -65,6 +51,7 @@ public interface ListQuerydslPredicateExecutor { * {@literal null}. * @return all entities matching the given {@link Predicate}. */ + @Override List findAll(Predicate predicate, Sort sort); /** @@ -75,6 +62,7 @@ public interface ListQuerydslPredicateExecutor { * @param orders the {@link OrderSpecifier}s to sort the results by, must not be {@literal null}. * @return all entities matching the given {@link Predicate} applying the given {@link OrderSpecifier}s. */ + @Override List findAll(Predicate predicate, OrderSpecifier... orders); /** @@ -83,41 +71,7 @@ public interface ListQuerydslPredicateExecutor { * @param orders the {@link OrderSpecifier}s to sort the results by, must not be {@literal null}. * @return all entities ordered by the given {@link OrderSpecifier}s. */ + @Override List findAll(OrderSpecifier... orders); - /** - * Returns a {@link Page} of entities matching the given {@link Predicate}. In case no match could be found, an empty - * {@link Page} is returned. - * - * @param predicate must not be {@literal null}. - * @param pageable may be {@link Pageable#unpaged()}, must not be {@literal null}. - * @return a {@link Page} of entities matching the given {@link Predicate}. - */ - Page findAll(Predicate predicate, Pageable pageable); - - /** - * Returns the number of instances matching the given {@link Predicate}. - * - * @param predicate the {@link Predicate} to count instances for, must not be {@literal null}. - * @return the number of instances matching the {@link Predicate}. - */ - long count(Predicate predicate); - - /** - * Checks whether the data store contains elements that match the given {@link Predicate}. - * - * @param predicate the {@link Predicate} to use for the existence check, must not be {@literal null}. - * @return {@literal true} if the data store contains elements that match the given {@link Predicate}. - */ - boolean exists(Predicate predicate); - - /** - * Returns entities matching the given {@link Predicate} applying the {@link Function queryFunction} that defines the - * query and its result type. - * - * @param predicate must not be {@literal null}. - * @param queryFunction the query function defining projection, sorting, and the result type - * @return all entities matching the given {@link Predicate}. - */ - R findBy(Predicate predicate, Function, R> queryFunction); } diff --git a/src/main/java/org/springframework/data/querydsl/QuerydslPredicateExecutor.java b/src/main/java/org/springframework/data/querydsl/QuerydslPredicateExecutor.java index c9f1311dc..0d9940a7e 100644 --- a/src/main/java/org/springframework/data/querydsl/QuerydslPredicateExecutor.java +++ b/src/main/java/org/springframework/data/querydsl/QuerydslPredicateExecutor.java @@ -33,6 +33,7 @@ import com.querydsl.core.types.Predicate; * @author Thomas Darimont * @author Christoph Strobl * @author Mark Paluch + * @see ListQuerydslPredicateExecutor */ public interface QuerydslPredicateExecutor { diff --git a/src/main/java/org/springframework/data/repository/ListCrudRepository.java b/src/main/java/org/springframework/data/repository/ListCrudRepository.java index e58cb1587..3f2d9545f 100644 --- a/src/main/java/org/springframework/data/repository/ListCrudRepository.java +++ b/src/main/java/org/springframework/data/repository/ListCrudRepository.java @@ -16,28 +16,17 @@ package org.springframework.data.repository; import java.util.List; -import java.util.Optional; /** - * Interface for generic CRUD operations on a repository for a specific type. This identical to {@link CrudRepository} - * but returns {@link List} instead of {@link Iterable} where applicable. - * + * Interface for generic CRUD operations on a repository for a specific type. This an extension to + * {@link CrudRepository} returning {@link List} instead of {@link Iterable} where applicable. + * * @author Jens Schauder * @see CrudRepository * @since 3.0 */ @NoRepositoryBean -public interface ListCrudRepository extends Repository { - - /** - * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the - * entity instance completely. - * - * @param entity must not be {@literal null}. - * @return the saved entity; will never be {@literal null}. - * @throws IllegalArgumentException in case the given {@literal entity} is {@literal null}. - */ - S save(S entity); +public interface ListCrudRepository extends CrudRepository { /** * Saves all given entities. @@ -50,24 +39,6 @@ public interface ListCrudRepository extends Repository { */ List saveAll(Iterable entities); - /** - * Retrieves an entity by its id. - * - * @param id must not be {@literal null}. - * @return the entity with the given id or {@literal Optional#empty()} if none found. - * @throws IllegalArgumentException if {@literal id} is {@literal null}. - */ - Optional findById(ID id); - - /** - * Returns whether an entity with the given id exists. - * - * @param id must not be {@literal null}. - * @return {@literal true} if an entity with the given id exists, {@literal false} otherwise. - * @throws IllegalArgumentException if {@literal id} is {@literal null}. - */ - boolean existsById(ID id); - /** * Returns all instances of the type. * @@ -89,47 +60,4 @@ public interface ListCrudRepository extends Repository { */ List findAllById(Iterable ids); - /** - * Returns the number of entities available. - * - * @return the number of entities. - */ - long count(); - - /** - * Deletes the entity with the given id. - * - * @param id must not be {@literal null}. - * @throws IllegalArgumentException in case the given {@literal id} is {@literal null} - */ - void deleteById(ID id); - - /** - * Deletes a given entity. - * - * @param entity must not be {@literal null}. - * @throws IllegalArgumentException in case the given entity is {@literal null}. - */ - void delete(T entity); - - /** - * Deletes all instances of the type {@code T} with the given IDs. - * - * @param ids must not be {@literal null}. Must not contain {@literal null} elements. - * @throws IllegalArgumentException in case the given {@literal ids} or one of its elements is {@literal null}. - */ - void deleteAllById(Iterable ids); - - /** - * Deletes the given entities. - * - * @param entities must not be {@literal null}. Must not contain {@literal null} elements. - * @throws IllegalArgumentException in case the given {@literal entities} or one of its entities is {@literal null}. - */ - void deleteAll(Iterable entities); - - /** - * Deletes all entities managed by the repository. - */ - void deleteAll(); } diff --git a/src/main/java/org/springframework/data/repository/ListPagingAndSortingRepository.java b/src/main/java/org/springframework/data/repository/ListPagingAndSortingRepository.java new file mode 100644 index 000000000..7cea911e3 --- /dev/null +++ b/src/main/java/org/springframework/data/repository/ListPagingAndSortingRepository.java @@ -0,0 +1,46 @@ +/* + * Copyright 2008-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.repository; + +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +/** + * Repository fragment to provide methods to retrieve entities using the pagination and sorting abstraction. This an + * extension to {@link PagingAndSortingRepository} returning {@link List} instead of {@link Iterable} where applicable. + * + * @author Oliver Gierke + * @author Jens Schauder + * @see Sort + * @see Pageable + * @see Page + * @see CrudRepository + */ +@NoRepositoryBean +public interface ListPagingAndSortingRepository extends PagingAndSortingRepository { + + /** + * Returns all entities sorted by the given options. + * + * @param sort + * @return all entities sorted by the given options + */ + List findAll(Sort sort); + +} diff --git a/src/main/java/org/springframework/data/repository/query/ListQueryByExampleExecutor.java b/src/main/java/org/springframework/data/repository/query/ListQueryByExampleExecutor.java index 375265d5e..301a565e0 100644 --- a/src/main/java/org/springframework/data/repository/query/ListQueryByExampleExecutor.java +++ b/src/main/java/org/springframework/data/repository/query/ListQueryByExampleExecutor.java @@ -16,33 +16,20 @@ package org.springframework.data.repository.query; import java.util.List; -import java.util.Optional; -import java.util.function.Function; import org.springframework.data.domain.Example; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; /** - * Interface to allow execution of Query by Example {@link Example} instances. This identical to - * {@link QueryByExampleExecutor} but returns {@link List} instead of {@link Iterable} where applicable. + * Interface to allow execution of Query by Example {@link Example} instances. This an extension to + * {@link QueryByExampleExecutor} returning {@link List} instead of {@link Iterable} where applicable. * * @param the type of entity for which this executor acts on. * @author Jens Schauder * @since 3.0 * @see QueryByExampleExecutor */ -public interface ListQueryByExampleExecutor { - - /** - * Returns a single entity matching the given {@link Example} or {@link Optional#empty()} if none was found. - * - * @param example must not be {@literal null}. - * @return a single entity matching the given {@link Example} or {@link Optional#empty()} if none was found. - * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the Example yields more than one result. - */ - Optional findOne(Example example); +public interface ListQueryByExampleExecutor extends QueryByExampleExecutor { /** * Returns all entities matching the given {@link Example}. In case no match could be found an empty {@link List} is @@ -51,6 +38,7 @@ public interface ListQueryByExampleExecutor { * @param example must not be {@literal null}. * @return all entities matching the given {@link Example}. */ + @Override List findAll(Example example); /** @@ -61,41 +49,7 @@ public interface ListQueryByExampleExecutor { * @param sort the {@link Sort} specification to sort the results by, must not be {@literal null}. * @return all entities matching the given {@link Example}. */ + @Override List findAll(Example example, Sort sort); - /** - * Returns a {@link Page} of entities matching the given {@link Example}. In case no match could be found, an empty - * {@link Page} is returned. - * - * @param example must not be {@literal null}. - * @param pageable can be {@literal null}. - * @return a {@link Page} of entities matching the given {@link Example}. - */ - Page findAll(Example example, Pageable pageable); - - /** - * Returns the number of instances matching the given {@link Example}. - * - * @param example the {@link Example} to count instances for. Must not be {@literal null}. - * @return the number of instances matching the {@link Example}. - */ - long count(Example example); - - /** - * Checks whether the data store contains elements that match the given {@link Example}. - * - * @param example the {@link Example} to use for the existence check. Must not be {@literal null}. - * @return {@literal true} if the data store contains elements that match the given {@link Example}. - */ - boolean exists(Example example); - - /** - * Returns entities matching the given {@link Example} applying the {@link Function queryFunction} that defines the - * query and its result type. - * - * @param example must not be {@literal null}. - * @param queryFunction the query function defining projection, sorting, and the result type - * @return all entities matching the given {@link Example}. - */ - R findBy(Example example, Function, R> queryFunction); } diff --git a/src/main/java/org/springframework/data/repository/query/QueryByExampleExecutor.java b/src/main/java/org/springframework/data/repository/query/QueryByExampleExecutor.java index 0d495746b..9283b0b8b 100644 --- a/src/main/java/org/springframework/data/repository/query/QueryByExampleExecutor.java +++ b/src/main/java/org/springframework/data/repository/query/QueryByExampleExecutor.java @@ -30,6 +30,7 @@ import org.springframework.data.domain.Sort; * @author Mark Paluch * @author Christoph Strobl * @since 1.12 + * @see ListQueryByExampleExecutor */ public interface QueryByExampleExecutor {