Skip to content

Commit 3cb926e

Browse files
committed
Task list getters never return null now
Issue: SPR-13680
1 parent 55d2055 commit 3cb926e

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public TaskScheduler getScheduler() {
111111
public void setTriggerTasks(Map<Runnable, Trigger> triggerTasks) {
112112
this.triggerTasks = new ArrayList<TriggerTask>();
113113
for (Map.Entry<Runnable, Trigger> task : triggerTasks.entrySet()) {
114-
this.triggerTasks.add(new TriggerTask(task.getKey(), task.getValue()));
114+
addTriggerTask(new TriggerTask(task.getKey(), task.getValue()));
115115
}
116116
}
117117

@@ -127,10 +127,11 @@ public void setTriggerTasksList(List<TriggerTask> triggerTasks) {
127127

128128
/**
129129
* Get the trigger tasks as an unmodifiable list of {@link TriggerTask} objects.
130+
* @return the list of tasks (never {@code null})
130131
* @since 4.2
131132
*/
132133
public List<TriggerTask> getTriggerTaskList() {
133-
return (this.triggerTasks != null? Collections.unmodifiableList(this.triggerTasks) : null);
134+
return (this.triggerTasks != null? Collections.unmodifiableList(this.triggerTasks) : Collections.emptyList());
134135
}
135136

136137
/**
@@ -140,7 +141,7 @@ public List<TriggerTask> getTriggerTaskList() {
140141
public void setCronTasks(Map<Runnable, String> cronTasks) {
141142
this.cronTasks = new ArrayList<CronTask>();
142143
for (Map.Entry<Runnable, String> task : cronTasks.entrySet()) {
143-
this.addCronTask(task.getKey(), task.getValue());
144+
addCronTask(task.getKey(), task.getValue());
144145
}
145146
}
146147

@@ -156,10 +157,11 @@ public void setCronTasksList(List<CronTask> cronTasks) {
156157

157158
/**
158159
* Get the cron tasks as an unmodifiable list of {@link CronTask} objects.
160+
* @return the list of tasks (never {@code null})
159161
* @since 4.2
160162
*/
161163
public List<CronTask> getCronTaskList() {
162-
return (this.cronTasks != null ? Collections.unmodifiableList(this.cronTasks) : null);
164+
return (this.cronTasks != null ? Collections.unmodifiableList(this.cronTasks) : Collections.emptyList());
163165
}
164166

165167
/**
@@ -169,7 +171,7 @@ public List<CronTask> getCronTaskList() {
169171
public void setFixedRateTasks(Map<Runnable, Long> fixedRateTasks) {
170172
this.fixedRateTasks = new ArrayList<IntervalTask>();
171173
for (Map.Entry<Runnable, Long> task : fixedRateTasks.entrySet()) {
172-
this.addFixedRateTask(task.getKey(), task.getValue());
174+
addFixedRateTask(task.getKey(), task.getValue());
173175
}
174176
}
175177

@@ -185,10 +187,11 @@ public void setFixedRateTasksList(List<IntervalTask> fixedRateTasks) {
185187

186188
/**
187189
* Get the fixed-rate tasks as an unmodifiable list of {@link IntervalTask} objects.
190+
* @return the list of tasks (never {@code null})
188191
* @since 4.2
189192
*/
190193
public List<IntervalTask> getFixedRateTaskList() {
191-
return (this.fixedRateTasks != null ? Collections.unmodifiableList(this.fixedRateTasks) : null);
194+
return (this.fixedRateTasks != null ? Collections.unmodifiableList(this.fixedRateTasks) : Collections.emptyList());
192195
}
193196

194197
/**
@@ -198,7 +201,7 @@ public List<IntervalTask> getFixedRateTaskList() {
198201
public void setFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks) {
199202
this.fixedDelayTasks = new ArrayList<IntervalTask>();
200203
for (Map.Entry<Runnable, Long> task : fixedDelayTasks.entrySet()) {
201-
this.addFixedDelayTask(task.getKey(), task.getValue());
204+
addFixedDelayTask(task.getKey(), task.getValue());
202205
}
203206
}
204207

@@ -214,18 +217,19 @@ public void setFixedDelayTasksList(List<IntervalTask> fixedDelayTasks) {
214217

215218
/**
216219
* Get the fixed-delay tasks as an unmodifiable list of {@link IntervalTask} objects.
220+
* @return the list of tasks (never {@code null})
217221
* @since 4.2
218222
*/
219223
public List<IntervalTask> getFixedDelayTaskList() {
220-
return (this.fixedDelayTasks != null ? Collections.unmodifiableList(this.fixedDelayTasks) : null);
224+
return (this.fixedDelayTasks != null ? Collections.unmodifiableList(this.fixedDelayTasks) : Collections.emptyList());
221225
}
222226

223227
/**
224228
* Add a Runnable task to be triggered per the given {@link Trigger}.
225229
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
226230
*/
227231
public void addTriggerTask(Runnable task, Trigger trigger) {
228-
this.addTriggerTask(new TriggerTask(task, trigger));
232+
addTriggerTask(new TriggerTask(task, trigger));
229233
}
230234

231235
/**
@@ -244,7 +248,7 @@ public void addTriggerTask(TriggerTask task) {
244248
* Add a Runnable task to be triggered per the given cron expression
245249
*/
246250
public void addCronTask(Runnable task, String expression) {
247-
this.addCronTask(new CronTask(task, expression));
251+
addCronTask(new CronTask(task, expression));
248252
}
249253

250254
/**
@@ -263,7 +267,7 @@ public void addCronTask(CronTask task) {
263267
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
264268
*/
265269
public void addFixedRateTask(Runnable task, long interval) {
266-
this.addFixedRateTask(new IntervalTask(task, interval, 0));
270+
addFixedRateTask(new IntervalTask(task, interval, 0));
267271
}
268272

269273
/**
@@ -283,7 +287,7 @@ public void addFixedRateTask(IntervalTask task) {
283287
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
284288
*/
285289
public void addFixedDelayTask(Runnable task, long delay) {
286-
this.addFixedDelayTask(new IntervalTask(task, delay, 0));
290+
addFixedDelayTask(new IntervalTask(task, delay, 0));
287291
}
288292

289293
/**

spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,22 @@
2828
* Unit tests for {@link ScheduledTaskRegistrar}.
2929
*
3030
* @author Tobias Montagna-Hay
31+
* @author Juergen Hoeller
3132
* @since 4.2
3233
*/
3334
public class ScheduledTaskRegistrarTests {
3435

3536
private final ScheduledTaskRegistrar taskRegistrar = new ScheduledTaskRegistrar();
3637

38+
39+
@Test
40+
public void emptyTaskLists() {
41+
assertTrue(this.taskRegistrar.getTriggerTaskList().isEmpty());
42+
assertTrue(this.taskRegistrar.getCronTaskList().isEmpty());
43+
assertTrue(this.taskRegistrar.getFixedRateTaskList().isEmpty());
44+
assertTrue(this.taskRegistrar.getFixedDelayTaskList().isEmpty());
45+
}
46+
3747
@Test
3848
public void getTriggerTasks() {
3949
TriggerTask mockTriggerTask = mock(TriggerTask.class);

0 commit comments

Comments
 (0)