Skip to content

Commit b209fc7

Browse files
quafffmbenhassine
authored andcommitted
Use Spring Framework's CollectionUtils to create HashSet/HashMap
It handles initial capacity correctly. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 81b5df1 commit b209fc7

File tree

8 files changed

+18
-10
lines changed

8 files changed

+18
-10
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/MultiResourcePartitioner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.batch.infrastructure.item.ExecutionContext;
2525
import org.springframework.core.io.Resource;
2626
import org.springframework.util.Assert;
27+
import org.springframework.util.CollectionUtils;
2728

2829
/**
2930
* Implementation of {@link Partitioner} that locates multiple resources and associates
@@ -70,7 +71,7 @@ public void setKeyName(String keyName) {
7071
*/
7172
@Override
7273
public Map<String, ExecutionContext> partition(int gridSize) {
73-
Map<String, ExecutionContext> map = new HashMap<>(gridSize);
74+
Map<String, ExecutionContext> map = CollectionUtils.newHashMap(gridSize);
7475
int i = 0;
7576
for (Resource resource : resources) {
7677
ExecutionContext context = new ExecutionContext();

spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/SimplePartitioner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.batch.core.partition.Partitioner;
2323
import org.springframework.batch.infrastructure.item.ExecutionContext;
24+
import org.springframework.util.CollectionUtils;
2425

2526
/**
2627
* Simplest possible implementation of {@link Partitioner}. Just creates a set of empty
@@ -37,7 +38,7 @@ public class SimplePartitioner implements Partitioner {
3738

3839
@Override
3940
public Map<String, ExecutionContext> partition(int gridSize) {
40-
Map<String, ExecutionContext> map = new HashMap<>(gridSize);
41+
Map<String, ExecutionContext> map = CollectionUtils.newHashMap(gridSize);
4142
for (int i = 0; i < gridSize; i++) {
4243
map.put(PARTITION_KEY + i, new ExecutionContext());
4344
}

spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/SimpleStepExecutionSplitter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.batch.core.partition.StepExecutionSplitter;
3434
import org.springframework.batch.core.repository.JobRepository;
3535
import org.springframework.batch.infrastructure.item.ExecutionContext;
36+
import org.springframework.util.CollectionUtils;
3637

3738
/**
3839
* Generic implementation of {@link StepExecutionSplitter} that delegates to a
@@ -128,7 +129,7 @@ public Set<StepExecution> split(StepExecution stepExecution, int gridSize) throw
128129
JobExecution jobExecution = stepExecution.getJobExecution();
129130

130131
Map<String, ExecutionContext> contexts = getContexts(stepExecution, gridSize);
131-
Set<StepExecution> set = new HashSet<>(contexts.size());
132+
Set<StepExecution> set = CollectionUtils.newHashSet(contexts.size());
132133

133134
for (Entry<String, ExecutionContext> context : contexts.entrySet()) {
134135

@@ -153,7 +154,7 @@ && shouldStart(allowStartIfComplete, stepExecution, lastStepExecution)) {
153154
}
154155
}
155156

156-
Set<StepExecution> executions = new HashSet<>(set.size());
157+
Set<StepExecution> executions = CollectionUtils.newHashSet(set.size());
157158
executions.addAll(set);
158159

159160
return executions;

spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/TaskExecutorPartitionHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.core.task.TaskExecutor;
3535
import org.springframework.core.task.TaskRejectedException;
3636
import org.springframework.util.Assert;
37+
import org.springframework.util.CollectionUtils;
3738

3839
/**
3940
* A {@link PartitionHandler} that uses a {@link TaskExecutor} to execute the partitioned
@@ -94,8 +95,8 @@ public Step getStep() {
9495
protected Set<StepExecution> doHandle(StepExecution managerStepExecution,
9596
Set<StepExecution> partitionStepExecutions) throws Exception {
9697
Assert.notNull(step, "A Step must be provided.");
97-
final Set<Future<StepExecution>> tasks = new HashSet<>(getGridSize());
98-
final Set<StepExecution> result = new HashSet<>();
98+
final Set<Future<StepExecution>> tasks = CollectionUtils.newHashSet(partitionStepExecutions.size());
99+
final Set<StepExecution> result = CollectionUtils.newHashSet(partitionStepExecutions.size());
99100

100101
for (StepExecution stepExecution : partitionStepExecutions) {
101102
final FutureTask<StepExecution> task = createTask(step, stepExecution);

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/jdbc/JdbcExecutionContextDao.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
4646
import org.springframework.jdbc.core.RowMapper;
4747
import org.springframework.util.Assert;
48+
import org.springframework.util.CollectionUtils;
4849

4950
/**
5051
* JDBC DAO for {@link ExecutionContext}.
@@ -238,7 +239,7 @@ public void saveExecutionContext(StepExecution stepExecution) {
238239
@Override
239240
public void saveExecutionContexts(Collection<StepExecution> stepExecutions) {
240241
Assert.notNull(stepExecutions, "Attempt to save an null collection of step executions");
241-
Map<Long, String> serializedContexts = new HashMap<>(stepExecutions.size());
242+
Map<Long, String> serializedContexts = CollectionUtils.newHashMap(stepExecutions.size());
242243
for (StepExecution stepExecution : stepExecutions) {
243244
Long executionId = stepExecution.getId();
244245
ExecutionContext executionContext = stepExecution.getExecutionContext();

spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/builder/FlatFileItemReaderBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.core.io.ByteArrayResource;
5353
import org.springframework.core.io.Resource;
5454
import org.springframework.util.Assert;
55+
import org.springframework.util.CollectionUtils;
5556
import org.springframework.util.StringUtils;
5657

5758
/**
@@ -691,7 +692,7 @@ public DelimitedLineTokenizer build() {
691692
}
692693

693694
if (!this.includedFields.isEmpty()) {
694-
Set<Integer> deDupedFields = new HashSet<>(this.includedFields.size());
695+
Set<Integer> deDupedFields = CollectionUtils.newHashSet(this.includedFields.size());
695696
deDupedFields.addAll(this.includedFields);
696697
deDupedFields.remove(null);
697698

spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/DefaultFieldSet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import org.jspecify.annotations.NullUnmarked;
3737
import org.jspecify.annotations.Nullable;
38+
import org.springframework.util.CollectionUtils;
3839
import org.springframework.util.StringUtils;
3940

4041
/**
@@ -131,7 +132,7 @@ public DefaultFieldSet(@Nullable String[] tokens, String[] names, @Nullable Date
131132
}
132133
this.tokens = tokens.clone();
133134
this.names = Arrays.asList(names);
134-
this.nameIndexMap = new HashMap<>(names.length);
135+
this.nameIndexMap = CollectionUtils.newHashMap(names.length);
135136
for (int i = 0; i < names.length; i++) {
136137
this.nameIndexMap.put(names[i], i);
137138
}

spring-batch-samples/src/main/java/org/springframework/batch/samples/loom/JobConfigurationForRunningPartitionedStepsWithVirtualThreads.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.context.annotation.Import;
3838
import org.springframework.core.task.VirtualThreadTaskExecutor;
3939
import org.springframework.jdbc.support.JdbcTransactionManager;
40+
import org.springframework.util.CollectionUtils;
4041

4142
/**
4243
* Configuration class that defines a partitioned step based on a
@@ -76,7 +77,7 @@ public Tasklet tasklet(@Value("#{stepExecutionContext['data']}") String partitio
7677
@Bean
7778
public Partitioner partitioner() {
7879
return gridSize -> {
79-
Map<String, ExecutionContext> partitionMap = new HashMap<>(gridSize);
80+
Map<String, ExecutionContext> partitionMap = CollectionUtils.newHashMap(gridSize);
8081
for (int i = 0; i < gridSize; i++) {
8182
ExecutionContext executionContext = new ExecutionContext();
8283
executionContext.put("data", "data" + i);

0 commit comments

Comments
 (0)