Skip to content

Commit 603d058

Browse files
authored
Merge pull request #197 from jglick/consumer
Replace `StepExecution.applyAll(Function)` with `.acceptAll(Consumer)`
2 parents a885a76 + 74d42f7 commit 603d058

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java

+31-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import edu.umd.cs.findbugs.annotations.CheckForNull;
1616
import edu.umd.cs.findbugs.annotations.NonNull;
1717
import jakarta.inject.Inject;
18+
import java.util.function.Consumer;
1819
import jenkins.model.queue.AsynchronousExecution;
1920
import jenkins.util.Timer;
2021

@@ -168,32 +169,50 @@ public boolean blocksRestart() {
168169
}
169170

170171
/**
171-
* Apply the given function to all the active running {@link StepExecution}s in the system.
172+
* Apply the given action to all the active running {@link StepExecution}s in the system.
172173
*
173174
* @return
174-
* Future object that signals when the function application is complete.
175+
* Future object that signals when the calls are complete.
175176
* @see StepExecutionIterator
176177
*/
177-
public static ListenableFuture<?> applyAll(Function<StepExecution,Void> f) {
178+
public static ListenableFuture<?> acceptAll(Consumer<StepExecution> c) {
178179
List<ListenableFuture<?>> futures = new ArrayList<>();
179-
for (StepExecutionIterator i : StepExecutionIterator.all())
180-
futures.add(i.apply(f));
180+
for (StepExecutionIterator i : StepExecutionIterator.all()) {
181+
futures.add(i.accept(c));
182+
}
181183
return Futures.allAsList(futures);
182184
}
183185

186+
/**
187+
* @deprecated use {@link #acceptAll(Consumer)}
188+
*/
189+
@Deprecated
190+
public static ListenableFuture<?> applyAll(Function<StepExecution, Void> f) {
191+
return acceptAll(fromGuava(f));
192+
}
193+
184194
/**
185195
* Applies only to the specific subtypes.
186196
*/
187-
public static <T extends StepExecution> ListenableFuture<?> applyAll(final Class<T> type, final Function<T,Void> f) {
188-
return applyAll(new Function<StepExecution, Void>() {
189-
@Override
190-
public Void apply(StepExecution e) {
191-
if (type.isInstance(e))
192-
f.apply(type.cast(e));
193-
return null;
197+
public static <T extends StepExecution> ListenableFuture<?> acceptAll(Class<T> type, Consumer<T> c) {
198+
return acceptAll(e -> {
199+
if (type.isInstance(e)) {
200+
c.accept(type.cast(e));
194201
}
195202
});
196203
}
197204

205+
/**
206+
* @deprecated use {@link #acceptAll(Class, Consumer)}
207+
*/
208+
@Deprecated
209+
public static <T extends StepExecution> ListenableFuture<?> applyAll(Class<T> type, Function<T, Void> f) {
210+
return acceptAll(type, fromGuava(f));
211+
}
212+
213+
private static <T> Consumer<T> fromGuava(Function<T, Void> func) {
214+
return func::apply;
215+
}
216+
198217
private static final long serialVersionUID = 1L;
199218
}

src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutionIterator.java

+26-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
import com.google.common.util.concurrent.ListenableFuture;
55
import hudson.ExtensionList;
66
import hudson.ExtensionPoint;
7+
import hudson.Util;
8+
import java.util.function.Consumer;
79

810
/**
911
* Enumerates active running {@link StepExecution}s in the system.
10-
* @see StepExecution#applyAll(Class, Function)
12+
* @see StepExecution#acceptAll(Class, Consumer)
1113
* @author Kohsuke Kawaguchi
1214
*/
1315
public abstract class StepExecutionIterator implements ExtensionPoint {
1416
/**
15-
* Finds all the ongoing {@link StepExecution} and apply the function.
17+
* Finds all the ongoing {@link StepExecution} and apply the action.
1618
*
1719
* The control flow is inverted because a major use case (workflow) loads
1820
* {@link StepExecution}s asynchronously (for example when workflow run
@@ -21,7 +23,28 @@ public abstract class StepExecutionIterator implements ExtensionPoint {
2123
* @return
2224
* {@link ListenableFuture} to signal the completion of the application.
2325
*/
24-
public abstract ListenableFuture<?> apply(Function<StepExecution,Void> f);
26+
public /* abstract */ ListenableFuture<?> accept(Consumer<StepExecution> f) {
27+
return Util.ifOverridden(() -> apply(toGuava(f)), StepExecutionIterator.class, getClass(), "apply", Function.class);
28+
}
29+
30+
/**
31+
* @deprecated use {@link #accept}
32+
*/
33+
@Deprecated
34+
public /* abstract */ ListenableFuture<?> apply(Function<StepExecution, Void> f) {
35+
return Util.ifOverridden(() -> accept(fromGuava(f)), StepExecutionIterator.class, getClass(), "accept", Consumer.class);
36+
}
37+
38+
private static <T> Consumer<T> fromGuava(Function<T, Void> func) {
39+
return func::apply;
40+
}
41+
42+
private static <T> Function<T, Void> toGuava(Consumer<T> consumer) {
43+
return v -> {
44+
consumer.accept(v);
45+
return null;
46+
};
47+
}
2548

2649
public static ExtensionList<StepExecutionIterator> all() {
2750
return ExtensionList.lookup(StepExecutionIterator.class);

0 commit comments

Comments
 (0)