|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * Running things that declare their inputs and outputs. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2026 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.execute; |
| 31 | + |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +/** |
| 37 | + * What came of a run: its outputs, or why it did not happen. |
| 38 | + * <p> |
| 39 | + * NB: this is deliberately not itself a {@link java.util.concurrent.Future}. |
| 40 | + * A future's {@code isCancelled()} and {@code get()} are coupled by contract - |
| 41 | + * whenever the former is true the latter must throw - so a declined run |
| 42 | + * modelled as a cancelled future would be an exception again, which is what |
| 43 | + * returning a result avoids. The asynchrony lives in |
| 44 | + * {@code Future<ExecutionResult>}, and what happened lives here. |
| 45 | + * </p> |
| 46 | + * |
| 47 | + * @author Curtis Rueden |
| 48 | + */ |
| 49 | +public class ExecutionResult { |
| 50 | + |
| 51 | + private final Map<String, Object> outputs; |
| 52 | + private final String reason; |
| 53 | + private final Preprocessor declinedBy; |
| 54 | + |
| 55 | + private ExecutionResult(final Map<String, Object> outputs, |
| 56 | + final String reason, final Preprocessor declinedBy) |
| 57 | + { |
| 58 | + this.outputs = outputs; |
| 59 | + this.reason = reason; |
| 60 | + this.declinedBy = declinedBy; |
| 61 | + } |
| 62 | + |
| 63 | + /** Creates the result of a run that happened. */ |
| 64 | + public static ExecutionResult completed(final Map<String, Object> outputs) { |
| 65 | + return new ExecutionResult(Map.copyOf(outputs), null, null); |
| 66 | + } |
| 67 | + |
| 68 | + /** Creates the result of a run that was declined before it happened. */ |
| 69 | + public static ExecutionResult declined(final String reason, |
| 70 | + final Preprocessor declinedBy) |
| 71 | + { |
| 72 | + return new ExecutionResult(Collections.emptyMap(), reason, declinedBy); |
| 73 | + } |
| 74 | + |
| 75 | + /** Gets whether the run happened. */ |
| 76 | + public boolean isCompleted() { |
| 77 | + return declinedBy == null && reason == null; |
| 78 | + } |
| 79 | + |
| 80 | + /** Gets whether the run was declined before it happened. */ |
| 81 | + public boolean isDeclined() { |
| 82 | + return !isCompleted(); |
| 83 | + } |
| 84 | + |
| 85 | + /** Gets why the run was declined, if it was, for the user to read. */ |
| 86 | + public Optional<String> reason() { |
| 87 | + return Optional.ofNullable(reason); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets which preprocessor declined the run, if one did. |
| 92 | + * <p> |
| 93 | + * NB: the instance rather than its class, since a chain may hold several |
| 94 | + * instances of one class and the useful question is which of them objected. |
| 95 | + * </p> |
| 96 | + */ |
| 97 | + public Optional<Preprocessor> declinedBy() { |
| 98 | + return Optional.ofNullable(declinedBy); |
| 99 | + } |
| 100 | + |
| 101 | + /** Gets the outputs, by name. Empty if the run was declined. */ |
| 102 | + public Map<String, Object> outputs() { |
| 103 | + return outputs; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String toString() { |
| 108 | + return isDeclined() ? "declined: " + reason : "completed: " + outputs; |
| 109 | + } |
| 110 | +} |
0 commit comments