Skip to content

Commit

Permalink
[NCL-6344] Implement Supplier interface (#603)
Browse files Browse the repository at this point in the history
Implement Supplier interface for `BuildFinder` and
`DistributionAnalyzer`. This is useful when invoking both of them in a
`CompletableFuture.supplyAsync` to produce a `CompletableFuture`. The
latter can then be composed and combined with other `CompletableFuture`s.

One reason for using that composability for NCL-6344 is to implement a
cancellation feature. The flow goes from:

url -> DistributionAnalyzer -> BuildFinder

When running `DistributionAnalyzer` and `BuildFinder` as a future, it is
really impossible to stop them in case of cancellation. However, we can
cancel the rest of the flow after them.

url -> future(DistributionAnalyzer) -> check if cancelled -> future(BuildFinder)

Using a `CompletableFuture` makes this much easier.
  • Loading branch information
thescouser89 committed Mar 14, 2022
1 parent 7446187 commit be2329d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -70,7 +72,8 @@
import com.redhat.red.build.koji.model.xmlrpc.KojiTagInfo;
import com.redhat.red.build.koji.model.xmlrpc.KojiTaskInfo;

public class BuildFinder implements Callable<Map<BuildSystemInteger, KojiBuild>> {
public class BuildFinder
implements Callable<Map<BuildSystemInteger, KojiBuild>>, Supplier<Map<BuildSystemInteger, KojiBuild>> {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildFinder.class);

private static final String BUILDS_FILENAME = "builds.json";
Expand Down Expand Up @@ -1203,6 +1206,23 @@ public Map<BuildSystemInteger, KojiBuild> call() throws KojiClientException {
return allBuilds;
}

/**
* Provide a Supplier version of the Callable. This is useful when using the BuildFinder to obtain a
* CompletableFuture (via {@link java.util.concurrent.CompletableFuture#supplyAsync(Supplier)})
*
* throws CompletionException if a KojiClientException is thrown
*
* @return For each checksum type (key), the checksum values of the files
*/
@Override
public Map<BuildSystemInteger, KojiBuild> get() {
try {
return call();
} catch (KojiClientException e) {
throw new CompletionException(e);
}
}

public void setListener(BuildFinderListener listener) {
this.listener = listener;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -64,7 +66,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DistributionAnalyzer implements Callable<Map<ChecksumType, MultiValuedMap<String, LocalFile>>> {
public class DistributionAnalyzer implements Callable<Map<ChecksumType, MultiValuedMap<String, LocalFile>>>,
Supplier<Map<ChecksumType, MultiValuedMap<String, LocalFile>>> {
private static final Logger LOGGER = LoggerFactory.getLogger(DistributionAnalyzer.class);

/**
Expand Down Expand Up @@ -574,6 +577,23 @@ public Map<ChecksumType, MultiValuedMap<String, LocalFile>> call() throws IOExce
return Collections.unmodifiableMap(map);
}

/**
* Provide a Supplier version of the Callable. This is useful when using the DistributionAnalyzer to obtain a
* CompletableFuture (via {@link java.util.concurrent.CompletableFuture#supplyAsync(Supplier)})
*
* throws CompletionException if an IO exception is thrown
*
* @return For each checksum type (key), the checksum values of the files
*/
@Override
public Map<ChecksumType, MultiValuedMap<String, LocalFile>> get() {
try {
return call();
} catch (IOException e) {
throw new CompletionException(e);
}
}

public BlockingQueue<Checksum> getQueue() {
return queue;
}
Expand Down

0 comments on commit be2329d

Please sign in to comment.