From 62e30b4fd99e8b09168d94db3f238ea17ea0cbff Mon Sep 17 00:00:00 2001 From: Baljit Singh Date: Thu, 22 Feb 2024 10:16:26 -0500 Subject: [PATCH] Avoid collection copying --- .../core/launch/support/CommandLineJobRunner.java | 3 ++- .../java/org/springframework/batch/item/Chunk.java | 10 +++++----- .../batch/item/data/MongoItemWriter.java | 4 ++-- .../batch/item/data/Neo4jItemWriter.java | 2 +- .../batch/item/data/RepositoryItemWriter.java | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java index 469ff15622..34bdf928b0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java @@ -53,6 +53,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -405,7 +406,7 @@ private List getJobExecutionsWithStatusGreaterThan(String jobIdent for (JobInstance jobInstance : lastInstances) { List jobExecutions = jobExplorer.getJobExecutions(jobInstance); - if (jobExecutions == null || jobExecutions.isEmpty()) { + if (CollectionUtils.isEmpty(jobExecutions)) { continue; } for (JobExecution jobExecution : jobExecutions) { diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/Chunk.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/Chunk.java index dd39b70e76..d772294c8e 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/Chunk.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/Chunk.java @@ -38,9 +38,9 @@ */ public class Chunk implements Iterable, Serializable { - private List items = new ArrayList<>(); + private final List items = new ArrayList<>(); - private List> skips = new ArrayList<>(); + private final List> skips = new ArrayList<>(); private final List errors = new ArrayList<>(); @@ -67,10 +67,10 @@ public Chunk(List items) { public Chunk(List items, List> skips) { super(); if (items != null) { - this.items = new ArrayList<>(items); + this.items.addAll(items); } if (skips != null) { - this.skips = new ArrayList<>(skips); + this.skips.addAll(skips); } } @@ -103,7 +103,7 @@ public void clear() { * @return a copy of the items to be processed as an unmodifiable list */ public List getItems() { - return List.copyOf(items); + return Collections.unmodifiableList(items); } /** diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java index 9ea598c5d4..0cb21451e7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java @@ -188,7 +188,7 @@ public void write(Chunk chunk) throws Exception { * @param chunk the chunk of items to be persisted. */ protected void doWrite(Chunk chunk) { - if (!CollectionUtils.isEmpty(chunk.getItems())) { + if (!chunk.isEmpty()) { switch (this.mode) { case INSERT -> insert(chunk); case REMOVE -> remove(chunk); @@ -263,7 +263,7 @@ private Chunk getCurrentBuffer() { public void beforeCommit(boolean readOnly) { Chunk chunk = (Chunk) TransactionSynchronizationManager.getResource(bufferKey); - if (!CollectionUtils.isEmpty(chunk.getItems())) { + if (!chunk.isEmpty()) { if (!readOnly) { doWrite(chunk); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java index 40cfbdb4cc..68d717d1d9 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/Neo4jItemWriter.java @@ -89,7 +89,7 @@ public void afterPropertiesSet() throws Exception { */ @Override public void write(Chunk chunk) throws Exception { - if (!CollectionUtils.isEmpty(chunk.getItems())) { + if (!chunk.isEmpty()) { doWrite(chunk); } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java index eb23f7800a..2065bf0d5a 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java @@ -93,7 +93,7 @@ public void setRepository(CrudRepository repository) { */ @Override public void write(Chunk chunk) throws Exception { - if (!CollectionUtils.isEmpty(chunk.getItems())) { + if (!chunk.isEmpty()) { doWrite(chunk); } }