Skip to content

Commit fdafb49

Browse files
singhbaljitfmbenhassine
authored andcommitted
Avoid collection copying
1 parent 6334fbf commit fdafb49

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5454
import org.springframework.context.support.ClassPathXmlApplicationContext;
5555
import org.springframework.util.Assert;
56+
import org.springframework.util.CollectionUtils;
5657
import org.springframework.util.StringUtils;
5758

5859
/**
@@ -405,7 +406,7 @@ private List<JobExecution> getJobExecutionsWithStatusGreaterThan(String jobIdent
405406

406407
for (JobInstance jobInstance : lastInstances) {
407408
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
408-
if (jobExecutions == null || jobExecutions.isEmpty()) {
409+
if (CollectionUtils.isEmpty(jobExecutions)) {
409410
continue;
410411
}
411412
for (JobExecution jobExecution : jobExecutions) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
*/
3939
public class Chunk<W> implements Iterable<W>, Serializable {
4040

41-
private List<W> items = new ArrayList<>();
41+
private final List<W> items = new ArrayList<>();
4242

43-
private List<SkipWrapper<W>> skips = new ArrayList<>();
43+
private final List<SkipWrapper<W>> skips = new ArrayList<>();
4444

4545
private final List<Exception> errors = new ArrayList<>();
4646

@@ -67,10 +67,10 @@ public Chunk(List<? extends W> items) {
6767
public Chunk(List<? extends W> items, List<SkipWrapper<W>> skips) {
6868
super();
6969
if (items != null) {
70-
this.items = new ArrayList<>(items);
70+
this.items.addAll(items);
7171
}
7272
if (skips != null) {
73-
this.skips = new ArrayList<>(skips);
73+
this.skips.addAll(skips);
7474
}
7575
}
7676

@@ -103,7 +103,7 @@ public void clear() {
103103
* @return a copy of the items to be processed as an unmodifiable list
104104
*/
105105
public List<W> getItems() {
106-
return List.copyOf(items);
106+
return Collections.unmodifiableList(items);
107107
}
108108

109109
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void write(Chunk<? extends T> chunk) throws Exception {
188188
* @param chunk the chunk of items to be persisted.
189189
*/
190190
protected void doWrite(Chunk<? extends T> chunk) {
191-
if (!CollectionUtils.isEmpty(chunk.getItems())) {
191+
if (!chunk.isEmpty()) {
192192
switch (this.mode) {
193193
case INSERT -> insert(chunk);
194194
case REMOVE -> remove(chunk);
@@ -263,7 +263,7 @@ private Chunk<T> getCurrentBuffer() {
263263
public void beforeCommit(boolean readOnly) {
264264
Chunk<T> chunk = (Chunk<T>) TransactionSynchronizationManager.getResource(bufferKey);
265265

266-
if (!CollectionUtils.isEmpty(chunk.getItems())) {
266+
if (!chunk.isEmpty()) {
267267
if (!readOnly) {
268268
doWrite(chunk);
269269
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void afterPropertiesSet() throws Exception {
8989
*/
9090
@Override
9191
public void write(Chunk<? extends T> chunk) throws Exception {
92-
if (!CollectionUtils.isEmpty(chunk.getItems())) {
92+
if (!chunk.isEmpty()) {
9393
doWrite(chunk);
9494
}
9595
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void setRepository(CrudRepository<T, ?> repository) {
9393
*/
9494
@Override
9595
public void write(Chunk<? extends T> chunk) throws Exception {
96-
if (!CollectionUtils.isEmpty(chunk.getItems())) {
96+
if (!chunk.isEmpty()) {
9797
doWrite(chunk);
9898
}
9999
}

0 commit comments

Comments
 (0)