Skip to content

Commit 74d42f7

Browse files
committed
Switching from Function to Consumer
1 parent ea0f260 commit 74d42f7

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jenkinsci.plugins.workflow.steps;
22

3+
import com.google.common.base.Function;
34
import com.google.common.util.concurrent.Futures;
45
import com.google.common.util.concurrent.ListenableFuture;
56
import java.io.Serializable;
@@ -9,12 +10,12 @@
910
import java.util.concurrent.Callable;
1011
import java.util.concurrent.Future;
1112
import java.util.concurrent.TimeUnit;
12-
import java.util.function.Function;
1313
import java.util.logging.Level;
1414
import java.util.logging.Logger;
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,51 +169,48 @@ 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

184186
/**
185-
* @deprecated use {@link #applyAll(Function)}
187+
* @deprecated use {@link #acceptAll(Consumer)}
186188
*/
187189
@Deprecated
188-
public static ListenableFuture<?> applyAll(com.google.common.base.Function<StepExecution, Void> f) {
189-
return applyAll(fromGuava(f));
190+
public static ListenableFuture<?> applyAll(Function<StepExecution, Void> f) {
191+
return acceptAll(fromGuava(f));
190192
}
191193

192194
/**
193195
* Applies only to the specific subtypes.
194196
*/
195-
public static <T extends StepExecution> ListenableFuture<?> applyAll(final Class<T> type, final Function<T,Void> f) {
196-
return applyAll(new Function<StepExecution, Void>() {
197-
@Override
198-
public Void apply(StepExecution e) {
199-
if (type.isInstance(e))
200-
f.apply(type.cast(e));
201-
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));
202201
}
203202
});
204203
}
205204

206205
/**
207-
* @deprecated use {@link #applyAll(Class, Function)}
206+
* @deprecated use {@link #acceptAll(Class, Consumer)}
208207
*/
209208
@Deprecated
210-
public static <T extends StepExecution> ListenableFuture<?> applyAll(
211-
final Class<T> type, final com.google.common.base.Function<T, Void> f) {
212-
return applyAll(type, fromGuava(f));
209+
public static <T extends StepExecution> ListenableFuture<?> applyAll(Class<T> type, Function<T, Void> f) {
210+
return acceptAll(type, fromGuava(f));
213211
}
214212

215-
private static <T, R> Function<T, R> fromGuava(com.google.common.base.Function<T, R> func) {
213+
private static <T> Consumer<T> fromGuava(Function<T, Void> func) {
216214
return func::apply;
217215
}
218216

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package org.jenkinsci.plugins.workflow.steps;
22

3+
import com.google.common.base.Function;
34
import com.google.common.util.concurrent.ListenableFuture;
45
import hudson.ExtensionList;
56
import hudson.ExtensionPoint;
67
import hudson.Util;
7-
import java.util.function.Function;
8+
import java.util.function.Consumer;
89

910
/**
1011
* Enumerates active running {@link StepExecution}s in the system.
11-
* @see StepExecution#applyAll(Class, Function)
12+
* @see StepExecution#acceptAll(Class, Consumer)
1213
* @author Kohsuke Kawaguchi
1314
*/
1415
public abstract class StepExecutionIterator implements ExtensionPoint {
1516
/**
16-
* Finds all the ongoing {@link StepExecution} and apply the function.
17+
* Finds all the ongoing {@link StepExecution} and apply the action.
1718
*
1819
* The control flow is inverted because a major use case (workflow) loads
1920
* {@link StepExecution}s asynchronously (for example when workflow run
@@ -22,30 +23,27 @@ public abstract class StepExecutionIterator implements ExtensionPoint {
2223
* @return
2324
* {@link ListenableFuture} to signal the completion of the application.
2425
*/
25-
public /* abstract */ ListenableFuture<?> apply(Function<StepExecution, Void> f) {
26-
return Util.ifOverridden(
27-
() -> apply(toGuava(f)),
28-
StepExecutionIterator.class,
29-
getClass(),
30-
"apply",
31-
com.google.common.base.Function.class);
26+
public /* abstract */ ListenableFuture<?> accept(Consumer<StepExecution> f) {
27+
return Util.ifOverridden(() -> apply(toGuava(f)), StepExecutionIterator.class, getClass(), "apply", Function.class);
3228
}
3329

3430
/**
35-
* @deprecated use {@link #apply(Function)}
31+
* @deprecated use {@link #accept}
3632
*/
3733
@Deprecated
38-
public /* abstract */ ListenableFuture<?> apply(com.google.common.base.Function<StepExecution, Void> f) {
39-
return Util.ifOverridden(
40-
() -> apply(fromGuava(f)), StepExecutionIterator.class, getClass(), "apply", Function.class);
34+
public /* abstract */ ListenableFuture<?> apply(Function<StepExecution, Void> f) {
35+
return Util.ifOverridden(() -> accept(fromGuava(f)), StepExecutionIterator.class, getClass(), "accept", Consumer.class);
4136
}
4237

43-
private static <T, R> Function<T, R> fromGuava(com.google.common.base.Function<T, R> func) {
38+
private static <T> Consumer<T> fromGuava(Function<T, Void> func) {
4439
return func::apply;
4540
}
4641

47-
private static <T, R> com.google.common.base.Function<T, R> toGuava(Function<T, R> func) {
48-
return func::apply;
42+
private static <T> Function<T, Void> toGuava(Consumer<T> consumer) {
43+
return v -> {
44+
consumer.accept(v);
45+
return null;
46+
};
4947
}
5048

5149
public static ExtensionList<StepExecutionIterator> all() {

0 commit comments

Comments
 (0)