Skip to content

Commit eefb1dc

Browse files
committed
Add support for fluent queries returning a Slice.
Closes #1011
1 parent 3969be0 commit eefb1dc

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/main/antora/modules/ROOT/pages/query-by-example.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ You do so by invoking the various methods of the `FetchableFluentQuery` in the s
207207
`sortBy` lets you specify an ordering for your result.
208208
`as` lets you specify the type to which you want the result to be transformed.
209209
`project` limits the queried attributes.
210-
`first`, `firstValue`, `one`, `oneValue`, `all`, `page`, `stream`, `count`, and `exists` define what kind of result you get and how the query behaves when more than the expected number of results are available.
210+
`first`, `firstValue`, `one`, `oneValue`, `all`, `page`, `slice`, `stream`, `count`, and `exists` define what kind of result you get and how the query behaves when more than the expected number of results are available.
211211

212212

213213
.Use the fluent API to get the last of potentially many results, ordered by lastname.

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@
1515
*/
1616
package org.springframework.data.repository.query;
1717

18-
import org.springframework.data.domain.Window;
1918
import reactor.core.publisher.Flux;
2019
import reactor.core.publisher.Mono;
2120

2221
import java.util.Arrays;
2322
import java.util.Collection;
2423
import java.util.List;
2524
import java.util.Optional;
25+
import java.util.function.Function;
2626
import java.util.stream.Stream;
2727

2828
import org.springframework.data.domain.Page;
2929
import org.springframework.data.domain.Pageable;
3030
import org.springframework.data.domain.ScrollPosition;
31+
import org.springframework.data.domain.Slice;
3132
import org.springframework.data.domain.Sort;
33+
import org.springframework.data.domain.Window;
3234
import org.springframework.lang.Nullable;
3335

3436
/**
@@ -41,7 +43,7 @@
4143
public interface FluentQuery<T> {
4244

4345
/**
44-
* Define the sort order.
46+
* Define the sort order. Multiple calls will add {@link Sort#and(Sort) Sort} specifications.
4547
*
4648
* @param sort the {@link Sort} specification to sort the results by, may be {@link Sort#unsorted()}, must not be
4749
* {@literal null}.
@@ -189,6 +191,19 @@ default Window<T> scroll(ScrollPosition scrollPosition) {
189191
*/
190192
Page<T> page(Pageable pageable);
191193

194+
/**
195+
* Get a slice of matching elements for {@link Pageable}.
196+
*
197+
* @param pageable the pageable to request a sliced result, can be {@link Pageable#unpaged()}, must not be
198+
* {@literal null}. The given {@link Pageable} will override any previously specified {@link Sort sort}.
199+
* Any potentially specified {@link #limit(int)} will be overridden by {@link Pageable#getPageSize()}.
200+
* @return
201+
* @since 3.5
202+
*/
203+
default Slice<T> slice(Pageable pageable) {
204+
return page(pageable);
205+
}
206+
192207
/**
193208
* Stream all matching elements.
194209
*
@@ -285,6 +300,19 @@ default Mono<Window<T>> scroll(ScrollPosition scrollPosition) {
285300
*/
286301
Mono<Page<T>> page(Pageable pageable);
287302

303+
/**
304+
* Get a slice of matching elements for {@link Pageable}.
305+
*
306+
* @param pageable the pageable to request a sliced result, can be {@link Pageable#unpaged()}, must not be
307+
* {@literal null}. The given {@link Pageable} will override any previously specified {@link Sort sort}.
308+
* Any potentially specified {@link #limit(int)} will be overridden by {@link Pageable#getPageSize()}.
309+
* @return
310+
* @since 3.5
311+
*/
312+
default Mono<Slice<T>> slice(Pageable pageable) {
313+
return page(pageable).map(Function.identity());
314+
}
315+
288316
/**
289317
* Get the number of matching elements.
290318
*

0 commit comments

Comments
 (0)