Skip to content

Commit 9146357

Browse files
committed
Polishing.
Let List… interface variants extend their base parent so that List-based variants participate in the type hierarchy. See #2538 Original pull request: #2538.
1 parent f92a117 commit 9146357

6 files changed

+64
-180
lines changed

src/main/java/org/springframework/data/querydsl/ListQuerydslPredicateExecutor.java

+7-53
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,21 @@
1616
package org.springframework.data.querydsl;
1717

1818
import java.util.List;
19-
import java.util.Optional;
20-
import java.util.function.Function;
2119

22-
import org.springframework.data.domain.Page;
23-
import org.springframework.data.domain.Pageable;
2420
import org.springframework.data.domain.Sort;
25-
import org.springframework.data.repository.query.FluentQuery;
2621

2722
import com.querydsl.core.types.OrderSpecifier;
2823
import com.querydsl.core.types.Predicate;
2924

3025
/**
31-
* Interface to allow execution of QueryDsl {@link Predicate} instances.This identical to *
32-
* {@link QuerydslPredicateExecutor} but returns {@link List} instead of {@link Iterable} where applicable.
26+
* Interface to allow execution of QueryDsl {@link Predicate} instances. This an extension to
27+
* {@link QuerydslPredicateExecutor} returning {@link List} instead of {@link Iterable} where applicable.
3328
*
3429
* @author Jens Schauder
3530
* @since 3.0
3631
* @see QuerydslPredicateExecutor
3732
*/
38-
public interface ListQuerydslPredicateExecutor<T> {
39-
40-
/**
41-
* Returns a single entity matching the given {@link Predicate} or {@link Optional#empty()} if none was found.
42-
*
43-
* @param predicate must not be {@literal null}.
44-
* @return a single entity matching the given {@link Predicate} or {@link Optional#empty()} if none was found.
45-
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the predicate yields more than one
46-
* result.
47-
*/
48-
Optional<T> findOne(Predicate predicate);
33+
public interface ListQuerydslPredicateExecutor<T> extends QuerydslPredicateExecutor<T> {
4934

5035
/**
5136
* 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<T> {
5439
* @param predicate must not be {@literal null}.
5540
* @return all entities matching the given {@link Predicate}.
5641
*/
42+
@Override
5743
List<T> findAll(Predicate predicate);
5844

5945
/**
@@ -65,6 +51,7 @@ public interface ListQuerydslPredicateExecutor<T> {
6551
* {@literal null}.
6652
* @return all entities matching the given {@link Predicate}.
6753
*/
54+
@Override
6855
List<T> findAll(Predicate predicate, Sort sort);
6956

7057
/**
@@ -75,6 +62,7 @@ public interface ListQuerydslPredicateExecutor<T> {
7562
* @param orders the {@link OrderSpecifier}s to sort the results by, must not be {@literal null}.
7663
* @return all entities matching the given {@link Predicate} applying the given {@link OrderSpecifier}s.
7764
*/
65+
@Override
7866
List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
7967

8068
/**
@@ -83,41 +71,7 @@ public interface ListQuerydslPredicateExecutor<T> {
8371
* @param orders the {@link OrderSpecifier}s to sort the results by, must not be {@literal null}.
8472
* @return all entities ordered by the given {@link OrderSpecifier}s.
8573
*/
74+
@Override
8675
List<T> findAll(OrderSpecifier<?>... orders);
8776

88-
/**
89-
* Returns a {@link Page} of entities matching the given {@link Predicate}. In case no match could be found, an empty
90-
* {@link Page} is returned.
91-
*
92-
* @param predicate must not be {@literal null}.
93-
* @param pageable may be {@link Pageable#unpaged()}, must not be {@literal null}.
94-
* @return a {@link Page} of entities matching the given {@link Predicate}.
95-
*/
96-
Page<T> findAll(Predicate predicate, Pageable pageable);
97-
98-
/**
99-
* Returns the number of instances matching the given {@link Predicate}.
100-
*
101-
* @param predicate the {@link Predicate} to count instances for, must not be {@literal null}.
102-
* @return the number of instances matching the {@link Predicate}.
103-
*/
104-
long count(Predicate predicate);
105-
106-
/**
107-
* Checks whether the data store contains elements that match the given {@link Predicate}.
108-
*
109-
* @param predicate the {@link Predicate} to use for the existence check, must not be {@literal null}.
110-
* @return {@literal true} if the data store contains elements that match the given {@link Predicate}.
111-
*/
112-
boolean exists(Predicate predicate);
113-
114-
/**
115-
* Returns entities matching the given {@link Predicate} applying the {@link Function queryFunction} that defines the
116-
* query and its result type.
117-
*
118-
* @param predicate must not be {@literal null}.
119-
* @param queryFunction the query function defining projection, sorting, and the result type
120-
* @return all entities matching the given {@link Predicate}.
121-
*/
122-
<S extends T, R> R findBy(Predicate predicate, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
12377
}

src/main/java/org/springframework/data/querydsl/QuerydslPredicateExecutor.java

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @author Thomas Darimont
3434
* @author Christoph Strobl
3535
* @author Mark Paluch
36+
* @see ListQuerydslPredicateExecutor
3637
*/
3738
public interface QuerydslPredicateExecutor<T> {
3839

src/main/java/org/springframework/data/repository/ListCrudRepository.java

+4-76
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,17 @@
1616
package org.springframework.data.repository;
1717

1818
import java.util.List;
19-
import java.util.Optional;
2019

2120
/**
22-
* Interface for generic CRUD operations on a repository for a specific type. This identical to {@link CrudRepository}
23-
* but returns {@link List} instead of {@link Iterable} where applicable.
24-
*
21+
* Interface for generic CRUD operations on a repository for a specific type. This an extension to
22+
* {@link CrudRepository} returning {@link List} instead of {@link Iterable} where applicable.
23+
*
2524
* @author Jens Schauder
2625
* @see CrudRepository
2726
* @since 3.0
2827
*/
2928
@NoRepositoryBean
30-
public interface ListCrudRepository<T, ID> extends Repository<T, ID> {
31-
32-
/**
33-
* Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
34-
* entity instance completely.
35-
*
36-
* @param entity must not be {@literal null}.
37-
* @return the saved entity; will never be {@literal null}.
38-
* @throws IllegalArgumentException in case the given {@literal entity} is {@literal null}.
39-
*/
40-
<S extends T> S save(S entity);
29+
public interface ListCrudRepository<T, ID> extends CrudRepository<T, ID> {
4130

4231
/**
4332
* Saves all given entities.
@@ -50,24 +39,6 @@ public interface ListCrudRepository<T, ID> extends Repository<T, ID> {
5039
*/
5140
<S extends T> List<S> saveAll(Iterable<S> entities);
5241

53-
/**
54-
* Retrieves an entity by its id.
55-
*
56-
* @param id must not be {@literal null}.
57-
* @return the entity with the given id or {@literal Optional#empty()} if none found.
58-
* @throws IllegalArgumentException if {@literal id} is {@literal null}.
59-
*/
60-
Optional<T> findById(ID id);
61-
62-
/**
63-
* Returns whether an entity with the given id exists.
64-
*
65-
* @param id must not be {@literal null}.
66-
* @return {@literal true} if an entity with the given id exists, {@literal false} otherwise.
67-
* @throws IllegalArgumentException if {@literal id} is {@literal null}.
68-
*/
69-
boolean existsById(ID id);
70-
7142
/**
7243
* Returns all instances of the type.
7344
*
@@ -89,47 +60,4 @@ public interface ListCrudRepository<T, ID> extends Repository<T, ID> {
8960
*/
9061
List<T> findAllById(Iterable<ID> ids);
9162

92-
/**
93-
* Returns the number of entities available.
94-
*
95-
* @return the number of entities.
96-
*/
97-
long count();
98-
99-
/**
100-
* Deletes the entity with the given id.
101-
*
102-
* @param id must not be {@literal null}.
103-
* @throws IllegalArgumentException in case the given {@literal id} is {@literal null}
104-
*/
105-
void deleteById(ID id);
106-
107-
/**
108-
* Deletes a given entity.
109-
*
110-
* @param entity must not be {@literal null}.
111-
* @throws IllegalArgumentException in case the given entity is {@literal null}.
112-
*/
113-
void delete(T entity);
114-
115-
/**
116-
* Deletes all instances of the type {@code T} with the given IDs.
117-
*
118-
* @param ids must not be {@literal null}. Must not contain {@literal null} elements.
119-
* @throws IllegalArgumentException in case the given {@literal ids} or one of its elements is {@literal null}.
120-
*/
121-
void deleteAllById(Iterable<? extends ID> ids);
122-
123-
/**
124-
* Deletes the given entities.
125-
*
126-
* @param entities must not be {@literal null}. Must not contain {@literal null} elements.
127-
* @throws IllegalArgumentException in case the given {@literal entities} or one of its entities is {@literal null}.
128-
*/
129-
void deleteAll(Iterable<? extends T> entities);
130-
131-
/**
132-
* Deletes all entities managed by the repository.
133-
*/
134-
void deleteAll();
13563
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2008-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.repository;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.domain.Pageable;
22+
import org.springframework.data.domain.Sort;
23+
24+
/**
25+
* Repository fragment to provide methods to retrieve entities using the pagination and sorting abstraction. This an
26+
* extension to {@link PagingAndSortingRepository} returning {@link List} instead of {@link Iterable} where applicable.
27+
*
28+
* @author Oliver Gierke
29+
* @author Jens Schauder
30+
* @see Sort
31+
* @see Pageable
32+
* @see Page
33+
* @see CrudRepository
34+
*/
35+
@NoRepositoryBean
36+
public interface ListPagingAndSortingRepository<T, ID> extends PagingAndSortingRepository<T, ID> {
37+
38+
/**
39+
* Returns all entities sorted by the given options.
40+
*
41+
* @param sort
42+
* @return all entities sorted by the given options
43+
*/
44+
List<T> findAll(Sort sort);
45+
46+
}

src/main/java/org/springframework/data/repository/query/ListQueryByExampleExecutor.java

+5-51
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,20 @@
1616
package org.springframework.data.repository.query;
1717

1818
import java.util.List;
19-
import java.util.Optional;
20-
import java.util.function.Function;
2119

2220
import org.springframework.data.domain.Example;
23-
import org.springframework.data.domain.Page;
24-
import org.springframework.data.domain.Pageable;
2521
import org.springframework.data.domain.Sort;
2622

2723
/**
28-
* Interface to allow execution of Query by Example {@link Example} instances. This identical to
29-
* {@link QueryByExampleExecutor} but returns {@link List} instead of {@link Iterable} where applicable.
24+
* Interface to allow execution of Query by Example {@link Example} instances. This an extension to
25+
* {@link QueryByExampleExecutor} returning {@link List} instead of {@link Iterable} where applicable.
3026
*
3127
* @param <T> the type of entity for which this executor acts on.
3228
* @author Jens Schauder
3329
* @since 3.0
3430
* @see QueryByExampleExecutor
3531
*/
36-
public interface ListQueryByExampleExecutor<T> {
37-
38-
/**
39-
* Returns a single entity matching the given {@link Example} or {@link Optional#empty()} if none was found.
40-
*
41-
* @param example must not be {@literal null}.
42-
* @return a single entity matching the given {@link Example} or {@link Optional#empty()} if none was found.
43-
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if the Example yields more than one result.
44-
*/
45-
<S extends T> Optional<S> findOne(Example<S> example);
32+
public interface ListQueryByExampleExecutor<T> extends QueryByExampleExecutor<T> {
4633

4734
/**
4835
* 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<T> {
5138
* @param example must not be {@literal null}.
5239
* @return all entities matching the given {@link Example}.
5340
*/
41+
@Override
5442
<S extends T> List<S> findAll(Example<S> example);
5543

5644
/**
@@ -61,41 +49,7 @@ public interface ListQueryByExampleExecutor<T> {
6149
* @param sort the {@link Sort} specification to sort the results by, must not be {@literal null}.
6250
* @return all entities matching the given {@link Example}.
6351
*/
52+
@Override
6453
<S extends T> List<S> findAll(Example<S> example, Sort sort);
6554

66-
/**
67-
* Returns a {@link Page} of entities matching the given {@link Example}. In case no match could be found, an empty
68-
* {@link Page} is returned.
69-
*
70-
* @param example must not be {@literal null}.
71-
* @param pageable can be {@literal null}.
72-
* @return a {@link Page} of entities matching the given {@link Example}.
73-
*/
74-
<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);
75-
76-
/**
77-
* Returns the number of instances matching the given {@link Example}.
78-
*
79-
* @param example the {@link Example} to count instances for. Must not be {@literal null}.
80-
* @return the number of instances matching the {@link Example}.
81-
*/
82-
<S extends T> long count(Example<S> example);
83-
84-
/**
85-
* Checks whether the data store contains elements that match the given {@link Example}.
86-
*
87-
* @param example the {@link Example} to use for the existence check. Must not be {@literal null}.
88-
* @return {@literal true} if the data store contains elements that match the given {@link Example}.
89-
*/
90-
<S extends T> boolean exists(Example<S> example);
91-
92-
/**
93-
* Returns entities matching the given {@link Example} applying the {@link Function queryFunction} that defines the
94-
* query and its result type.
95-
*
96-
* @param example must not be {@literal null}.
97-
* @param queryFunction the query function defining projection, sorting, and the result type
98-
* @return all entities matching the given {@link Example}.
99-
*/
100-
<S extends T, R> R findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
10155
}

src/main/java/org/springframework/data/repository/query/QueryByExampleExecutor.java

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Mark Paluch
3131
* @author Christoph Strobl
3232
* @since 1.12
33+
* @see ListQueryByExampleExecutor
3334
*/
3435
public interface QueryByExampleExecutor<T> {
3536

0 commit comments

Comments
 (0)