Skip to content

Commit 5585749

Browse files
committed
Use Map.computeIfAbsent where appropriate
1 parent d0be13e commit 5585749

File tree

3 files changed

+4
-16
lines changed

3 files changed

+4
-16
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/JobContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ public Map<String, Object> getJobParameters() {
107107
*/
108108
public void registerDestructionCallback(String name, Runnable callback) {
109109
synchronized (callbacks) {
110-
Set<Runnable> set = callbacks.get(name);
111-
if (set == null) {
112-
set = new HashSet<>();
113-
callbacks.put(name, set);
114-
}
110+
Set<Runnable> set = callbacks.computeIfAbsent(name, k -> new HashSet<>());
115111
set.add(callback);
116112
}
117113
}

spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,7 @@ public Map<String, Object> getJobParameters() {
147147
*/
148148
public void registerDestructionCallback(String name, Runnable callback) {
149149
synchronized (callbacks) {
150-
Set<Runnable> set = callbacks.get(name);
151-
if (set == null) {
152-
set = new HashSet<>();
153-
callbacks.put(name, set);
154-
}
150+
Set<Runnable> set = callbacks.computeIfAbsent(name, k -> new HashSet<>());
155151
set.add(callback);
156152
}
157153
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/context/RepeatContextSupport.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-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.
@@ -125,11 +125,7 @@ public synchronized int getStartedCount() {
125125
@Override
126126
public void registerDestructionCallback(String name, Runnable callback) {
127127
synchronized (callbacks) {
128-
Set<Runnable> set = callbacks.get(name);
129-
if (set == null) {
130-
set = new HashSet<>();
131-
callbacks.put(name, set);
132-
}
128+
Set<Runnable> set = callbacks.computeIfAbsent(name, k -> new HashSet<>());
133129
set.add(callback);
134130
}
135131
}

0 commit comments

Comments
 (0)