diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java index 4a4affc..b6f5ab3 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java @@ -15,6 +15,7 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import jakarta.inject.Inject; +import java.util.function.Consumer; import jenkins.model.queue.AsynchronousExecution; import jenkins.util.Timer; @@ -168,32 +169,50 @@ public boolean blocksRestart() { } /** - * Apply the given function to all the active running {@link StepExecution}s in the system. + * Apply the given action to all the active running {@link StepExecution}s in the system. * * @return - * Future object that signals when the function application is complete. + * Future object that signals when the calls are complete. * @see StepExecutionIterator */ - public static ListenableFuture applyAll(Function f) { + public static ListenableFuture acceptAll(Consumer c) { List> futures = new ArrayList<>(); - for (StepExecutionIterator i : StepExecutionIterator.all()) - futures.add(i.apply(f)); + for (StepExecutionIterator i : StepExecutionIterator.all()) { + futures.add(i.accept(c)); + } return Futures.allAsList(futures); } + /** + * @deprecated use {@link #acceptAll(Consumer)} + */ + @Deprecated + public static ListenableFuture applyAll(Function f) { + return acceptAll(fromGuava(f)); + } + /** * Applies only to the specific subtypes. */ - public static ListenableFuture applyAll(final Class type, final Function f) { - return applyAll(new Function() { - @Override - public Void apply(StepExecution e) { - if (type.isInstance(e)) - f.apply(type.cast(e)); - return null; + public static ListenableFuture acceptAll(Class type, Consumer c) { + return acceptAll(e -> { + if (type.isInstance(e)) { + c.accept(type.cast(e)); } }); } + /** + * @deprecated use {@link #acceptAll(Class, Consumer)} + */ + @Deprecated + public static ListenableFuture applyAll(Class type, Function f) { + return acceptAll(type, fromGuava(f)); + } + + private static Consumer fromGuava(Function func) { + return func::apply; + } + private static final long serialVersionUID = 1L; } diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutionIterator.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutionIterator.java index 9d93270..8b1e253 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutionIterator.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutionIterator.java @@ -4,15 +4,17 @@ import com.google.common.util.concurrent.ListenableFuture; import hudson.ExtensionList; import hudson.ExtensionPoint; +import hudson.Util; +import java.util.function.Consumer; /** * Enumerates active running {@link StepExecution}s in the system. - * @see StepExecution#applyAll(Class, Function) + * @see StepExecution#acceptAll(Class, Consumer) * @author Kohsuke Kawaguchi */ public abstract class StepExecutionIterator implements ExtensionPoint { /** - * Finds all the ongoing {@link StepExecution} and apply the function. + * Finds all the ongoing {@link StepExecution} and apply the action. * * The control flow is inverted because a major use case (workflow) loads * {@link StepExecution}s asynchronously (for example when workflow run @@ -21,7 +23,28 @@ public abstract class StepExecutionIterator implements ExtensionPoint { * @return * {@link ListenableFuture} to signal the completion of the application. */ - public abstract ListenableFuture apply(Function f); + public /* abstract */ ListenableFuture accept(Consumer f) { + return Util.ifOverridden(() -> apply(toGuava(f)), StepExecutionIterator.class, getClass(), "apply", Function.class); + } + + /** + * @deprecated use {@link #accept} + */ + @Deprecated + public /* abstract */ ListenableFuture apply(Function f) { + return Util.ifOverridden(() -> accept(fromGuava(f)), StepExecutionIterator.class, getClass(), "accept", Consumer.class); + } + + private static Consumer fromGuava(Function func) { + return func::apply; + } + + private static Function toGuava(Consumer consumer) { + return v -> { + consumer.accept(v); + return null; + }; + } public static ExtensionList all() { return ExtensionList.lookup(StepExecutionIterator.class);