001package com.identityworksllc.iiq.common.threads;
002
003import com.identityworksllc.iiq.common.vo.Failure;
004
005import java.util.ArrayList;
006import java.util.List;
007import java.util.concurrent.atomic.AtomicInteger;
008
009/**
010 * A container for holding the results of a whole pool of workers. This makes
011 * working with the outputs of pooled actions easier in Beanshell and other
012 * similar contexts.
013 *
014 * @param <T> The type of the results expected
015 */
016public class PooledWorkerResults<T> {
017    /**
018     * The shared completion counter
019     */
020    private final AtomicInteger completed;
021
022    /**
023     * The shared failure counter
024     */
025    private final AtomicInteger failed;
026
027    /**
028     * The shared list of failures
029     */
030    private final List<Failure<T, ? extends Exception>> failures;
031
032    /**
033     * The flag indicating that the action has been interrupted
034     */
035    private boolean interrupted;
036
037    /**
038     * Creates a new pooled worker result container
039     */
040    public PooledWorkerResults() {
041        this.completed = new AtomicInteger();
042        this.failed = new AtomicInteger();
043        this.failures = new ArrayList<>();
044    }
045
046    /**
047     * Adds a failure to the output
048     * @param f The failure object, containing the thing that failed and the exception
049     */
050    public void addFailure(Failure<T, ? extends Exception> f) {
051        this.failures.add(f);
052    }
053
054    /**
055     * Gets the completion counter
056     * @return The completion counter
057     */
058    public AtomicInteger getCompleted() {
059        return completed;
060    }
061
062    /**
063     * Gets the failure counter
064     * @return The failure counter
065     */
066    public AtomicInteger getFailed() {
067        return failed;
068    }
069
070    /**
071     * Gets the list of any failures associated with this pooled action
072     * @return The list of failures
073     */
074    public List<Failure<T, ? extends Exception>> getFailures() {
075        return failures;
076    }
077
078    public boolean isInterrupted() {
079        return interrupted;
080    }
081
082    /**
083     * Sets this action as interrupted
084     * @param interrupted The interrupted flag
085     */
086    public void setInterrupted(boolean interrupted) {
087        this.interrupted = interrupted;
088    }
089}