|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.concurrent; |
| 20 | + |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +/** |
| 25 | + * Represents the outcome of an asynchronous computation that may have |
| 26 | + * succeeded or failed. This is used by {@code awaitAllSettled()} — |
| 27 | + * the Groovy equivalent of JavaScript's {@code Promise.allSettled()}. |
| 28 | + * <p> |
| 29 | + * An {@code AwaitResult} is either a {@linkplain #isSuccess() success} |
| 30 | + * carrying a value, or a {@linkplain #isFailure() failure} carrying a |
| 31 | + * {@link Throwable}. |
| 32 | + * |
| 33 | + * @param <T> the value type |
| 34 | + * @since 6.0.0 |
| 35 | + */ |
| 36 | +public final class AwaitResult<T> { |
| 37 | + |
| 38 | + private final T value; |
| 39 | + private final Throwable error; |
| 40 | + private final boolean success; |
| 41 | + |
| 42 | + private AwaitResult(T value, Throwable error, boolean success) { |
| 43 | + this.value = value; |
| 44 | + this.error = error; |
| 45 | + this.success = success; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a successful result with the given value. |
| 50 | + * |
| 51 | + * @param value the computation result (may be {@code null}) |
| 52 | + * @param <T> the value type |
| 53 | + * @return a success result wrapping the value |
| 54 | + */ |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + public static <T> AwaitResult<T> success(Object value) { |
| 57 | + return new AwaitResult<>((T) value, null, true); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates a failure result with the given exception. |
| 62 | + * |
| 63 | + * @param error the exception that caused the failure; must not be {@code null} |
| 64 | + * @param <T> the value type (never actually used, since the result is a failure) |
| 65 | + * @return a failure result wrapping the exception |
| 66 | + * @throws NullPointerException if {@code error} is {@code null} |
| 67 | + */ |
| 68 | + public static <T> AwaitResult<T> failure(Throwable error) { |
| 69 | + return new AwaitResult<>(null, Objects.requireNonNull(error), false); |
| 70 | + } |
| 71 | + |
| 72 | + /** Returns {@code true} if this result represents a successful completion. */ |
| 73 | + public boolean isSuccess() { |
| 74 | + return success; |
| 75 | + } |
| 76 | + |
| 77 | + /** Returns {@code true} if this result represents a failed completion. */ |
| 78 | + public boolean isFailure() { |
| 79 | + return !success; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the value if successful. |
| 84 | + * |
| 85 | + * @return the computation result |
| 86 | + * @throws IllegalStateException if this result represents a failure |
| 87 | + */ |
| 88 | + public T getValue() { |
| 89 | + if (!success) throw new IllegalStateException("Cannot get value from a failed result"); |
| 90 | + return value; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the exception if failed. |
| 95 | + * |
| 96 | + * @return the exception that caused the failure |
| 97 | + * @throws IllegalStateException if this result represents a success |
| 98 | + */ |
| 99 | + public Throwable getError() { |
| 100 | + if (success) throw new IllegalStateException("Cannot get error from a successful result"); |
| 101 | + return error; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the value if successful, or applies the given function to |
| 106 | + * the error to produce a fallback value. |
| 107 | + */ |
| 108 | + public T getOrElse(Function<Throwable, ? extends T> fallback) { |
| 109 | + return success ? value : fallback.apply(error); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns a human-readable representation of this result: |
| 114 | + * {@code AwaitResult.Success[value]} or {@code AwaitResult.Failure[error]}. |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public String toString() { |
| 118 | + return success |
| 119 | + ? "AwaitResult.Success[" + value + "]" |
| 120 | + : "AwaitResult.Failure[" + error + "]"; |
| 121 | + } |
| 122 | +} |
0 commit comments