Skip to content

Commit 6697683

Browse files
hpoettkerfmbenhassine
authored andcommitted
Prefer Slice over Page in RepositoryItemReader
Resolves #4115
1 parent 900b36f commit 6697683

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,9 +28,9 @@
2828
import org.springframework.batch.item.adapter.DynamicMethodInvocationException;
2929
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
3030
import org.springframework.beans.factory.InitializingBean;
31-
import org.springframework.data.domain.Page;
3231
import org.springframework.data.domain.PageRequest;
3332
import org.springframework.data.domain.Pageable;
33+
import org.springframework.data.domain.Slice;
3434
import org.springframework.data.domain.Sort;
3535
import org.springframework.data.repository.PagingAndSortingRepository;
3636
import org.springframework.lang.Nullable;
@@ -168,7 +168,7 @@ protected T doRead() throws Exception {
168168
results = doPageRead();
169169
page ++;
170170

171-
if(results.size() <= 0) {
171+
if(results.isEmpty()) {
172172
return null;
173173
}
174174

@@ -220,7 +220,7 @@ protected List<T> doPageRead() throws Exception {
220220

221221
invoker.setArguments(parameters.toArray());
222222

223-
Page<T> curPage = (Page<T>) doInvoke(invoker);
223+
Slice<T> curPage = (Slice<T>) doInvoke(invoker);
224224

225225
return curPage.getContent();
226226
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemReaderTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,10 +29,11 @@
2929

3030
import org.springframework.batch.item.ExecutionContext;
3131
import org.springframework.batch.item.adapter.DynamicMethodInvocationException;
32-
import org.springframework.data.domain.Page;
3332
import org.springframework.data.domain.PageImpl;
3433
import org.springframework.data.domain.PageRequest;
3534
import org.springframework.data.domain.Pageable;
35+
import org.springframework.data.domain.Slice;
36+
import org.springframework.data.domain.SliceImpl;
3637
import org.springframework.data.domain.Sort;
3738
import org.springframework.data.domain.Sort.Direction;
3839
import org.springframework.data.repository.PagingAndSortingRepository;
@@ -263,7 +264,7 @@ public void testDifferentTypes() throws Exception {
263264
reader.setMethodName("findFirstNames");
264265

265266
ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
266-
when(differentRepository.findFirstNames(pageRequestContainer.capture())).thenReturn(new PageImpl<>(singletonList(
267+
when(differentRepository.findFirstNames(pageRequestContainer.capture())).thenReturn(new SliceImpl<>(singletonList(
267268
"result"
268269
)));
269270

@@ -365,7 +366,7 @@ public void testResetOfPage() throws Exception {
365366
}
366367

367368
public interface TestRepository extends PagingAndSortingRepository<Map, Long> {
368-
Page<String> findFirstNames(Pageable pageable);
369+
Slice<String> findFirstNames(Pageable pageable);
369370
}
370371

371372
// Simple object for readability

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,8 +28,8 @@
2828
import org.mockito.MockitoAnnotations;
2929

3030
import org.springframework.batch.item.data.RepositoryItemReader;
31-
import org.springframework.data.domain.Page;
3231
import org.springframework.data.domain.PageRequest;
32+
import org.springframework.data.domain.Slice;
3333
import org.springframework.data.domain.Sort;
3434
import org.springframework.data.repository.PagingAndSortingRepository;
3535

@@ -54,7 +54,7 @@ public class RepositoryItemReaderBuilderTests {
5454
private TestRepository repository;
5555

5656
@Mock
57-
private Page<String> page;
57+
private Slice<String> slice;
5858

5959
private Map<String, Sort.Direction> sorts;
6060

@@ -69,9 +69,9 @@ public void setUp() throws Exception {
6969

7070
List<String> testResult = new ArrayList<>();
7171
testResult.add(TEST_CONTENT);
72-
when(page.getContent()).thenReturn(testResult);
73-
when(page.getSize()).thenReturn(5);
74-
when(this.repository.foo(this.pageRequestContainer.capture())).thenReturn(this.page);
72+
when(slice.getContent()).thenReturn(testResult);
73+
when(slice.getSize()).thenReturn(5);
74+
when(this.repository.foo(this.pageRequestContainer.capture())).thenReturn(this.slice);
7575
}
7676

7777
@Test
@@ -116,7 +116,7 @@ public void testRepositoryMethodReferenceWithArgs() throws Exception {
116116
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
117117
ArgumentCaptor<String> arg3Captor = ArgumentCaptor.forClass(String.class);
118118
when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(),
119-
this.pageRequestContainer.capture())).thenReturn(this.page);
119+
this.pageRequestContainer.capture())).thenReturn(this.slice);
120120

121121
String result = (String) reader.read();
122122
assertEquals("Result returned from reader was not expected value.", TEST_CONTENT, result);
@@ -244,7 +244,7 @@ public void testArguments() throws Exception {
244244
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
245245
ArgumentCaptor<String> arg3Captor = ArgumentCaptor.forClass(String.class);
246246
when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(),
247-
this.pageRequestContainer.capture())).thenReturn(this.page);
247+
this.pageRequestContainer.capture())).thenReturn(this.slice);
248248

249249
RepositoryItemReader<Object> reader = new RepositoryItemReaderBuilder<>().repository(this.repository)
250250
.sorts(this.sorts)
@@ -264,7 +264,7 @@ public void testVarargArguments() throws Exception {
264264
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
265265
ArgumentCaptor<String> arg3Captor = ArgumentCaptor.forClass(String.class);
266266
when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(),
267-
this.pageRequestContainer.capture())).thenReturn(this.page);
267+
this.pageRequestContainer.capture())).thenReturn(this.slice);
268268

269269
RepositoryItemReader<Object> reader = new RepositoryItemReaderBuilder<>().repository(this.repository)
270270
.sorts(this.sorts)

spring-batch-samples/src/main/java/org/springframework/batch/sample/data/CustomerCreditRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,10 @@
1818
import java.math.BigDecimal;
1919

2020
import org.springframework.batch.sample.domain.trade.CustomerCredit;
21-
import org.springframework.data.domain.Page;
2221
import org.springframework.data.domain.Pageable;
22+
import org.springframework.data.domain.Slice;
2323
import org.springframework.data.repository.PagingAndSortingRepository;
2424

2525
public interface CustomerCreditRepository extends PagingAndSortingRepository<CustomerCredit, Long>{
26-
Page<CustomerCredit> findByCreditGreaterThan(BigDecimal credit, Pageable request);
26+
Slice<CustomerCredit> findByCreditGreaterThan(BigDecimal credit, Pageable request);
2727
}

0 commit comments

Comments
 (0)