Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace StepExecution.applyAll(Function) with .acceptAll(Consumer) #197

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -168,32 +169,50 @@
}

/**
* 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<StepExecution,Void> f) {
public static ListenableFuture<?> acceptAll(Consumer<StepExecution> c) {
List<ListenableFuture<?>> 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<StepExecution, Void> f) {
return acceptAll(fromGuava(f));
}

/**
* Applies only to the specific subtypes.
*/
public static <T extends StepExecution> ListenableFuture<?> applyAll(final Class<T> type, final Function<T,Void> f) {
return applyAll(new Function<StepExecution, Void>() {
@Override
public Void apply(StepExecution e) {
if (type.isInstance(e))
f.apply(type.cast(e));
return null;
public static <T extends StepExecution> ListenableFuture<?> acceptAll(Class<T> type, Consumer<T> c) {
return acceptAll(e -> {
if (type.isInstance(e)) {
c.accept(type.cast(e));
}
});
}

/**
* @deprecated use {@link #acceptAll(Class, Consumer)}
*/
@Deprecated
public static <T extends StepExecution> ListenableFuture<?> applyAll(Class<T> type, Function<T, Void> f) {
return acceptAll(type, fromGuava(f));
}

private static <T> Consumer<T> fromGuava(Function<T, Void> func) {
return func::apply;

Check warning on line 214 in src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 180-214 are not covered by tests
}

private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +23,28 @@
* @return
* {@link ListenableFuture} to signal the completion of the application.
*/
public abstract ListenableFuture<?> apply(Function<StepExecution,Void> f);
public /* abstract */ ListenableFuture<?> accept(Consumer<StepExecution> f) {
return Util.ifOverridden(() -> apply(toGuava(f)), StepExecutionIterator.class, getClass(), "apply", Function.class);
}

/**
* @deprecated use {@link #accept}
*/
@Deprecated
public /* abstract */ ListenableFuture<?> apply(Function<StepExecution, Void> f) {
return Util.ifOverridden(() -> accept(fromGuava(f)), StepExecutionIterator.class, getClass(), "accept", Consumer.class);
}

private static <T> Consumer<T> fromGuava(Function<T, Void> func) {
return func::apply;
}

private static <T> Function<T, Void> toGuava(Consumer<T> consumer) {
return v -> {
consumer.accept(v);
return null;

Check warning on line 45 in src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecutionIterator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 27-45 are not covered by tests
};
}

public static ExtensionList<StepExecutionIterator> all() {
return ExtensionList.lookup(StepExecutionIterator.class);
Expand Down
Loading