2727import static io .vavr .concurrent .Future .DEFAULT_EXECUTOR ;
2828
2929/**
30- * A Promise is a write-once wrapper around a read-only Future which can complete the underlying Future with a value
31- * or an exception.
30+ * A {@code Promise} is a write-once container for a read-only {@code Future}, allowing the underlying {@code Future}
31+ * to be completed with a value or an exception.
3232 * <p>
33- * The underlying {@code Executor} is used to execute asynchronous handlers, e.g. via
33+ * The associated {@code Executor} is used to run asynchronous handlers, for example, via
3434 * {@code promise.future().onComplete(...)}.
3535 *
3636 * <h2>Creation</h2>
3737 * <p>
38- * Promise offers static factory methods to create new promises which hasn't been fulfilled yet :
38+ * {@code Promise} provides static factory methods to create new promises:
3939 * <ul>
40- * <li>create new promises: {@link Promise#make()}</li>
40+ * <li>Unfulfilled promises: {@link Promise#make()}</li>
41+ * <li>Already completed promises:
42+ * <ul>
43+ * <li>{@link #failed(Throwable)}</li>
44+ * <li>{@link #fromTry(Try)}</li>
45+ * <li>{@link #successful(Object)}</li>
46+ * </ul>
47+ * </li>
4148 * </ul>
42- * And we may create new promises that are already finished:
43- * <ul>
44- * <li>{@link #failed(Throwable)}</li>
45- * <li>{@link #fromTry(Try)}</li>
46- * <li>{@link #successful(Object)}</li>
47- * </ul>
48- * All the static factory methods mentioned above have additional versions which take an {@link Executor} as
49- * argument. This gives us more control over thread creation and thread pool sizes.
49+ * All factory methods also have variants that accept an {@link Executor}, allowing finer control over
50+ * thread usage and thread pool configuration.
5051 *
5152 * <h3>One-shot API</h3>
5253 * <p>
53- * The main purpose of a {@code Promise} is to complete its underlying {@code Future}. When only a single {@code Thread}
54- * will eventually complete the {@code Promise}, we use one of these methods. Calls will throw if the {@code Promise} is already
55- * completed.
54+ * When a single {@code Thread} is responsible for completing the {@code Promise}, use one of the following methods.
55+ * Calls will throw an exception if the {@code Promise} has already been completed:
5656 * <ul>
57- * <li>{@link #complete(Try)}</li>
58- * <li>{@link #completeWith(Future)}</li>
59- * <li>{@link #failure(Throwable)}</li>
60- * <li>{@link #success(Object)}</li>
57+ * <li>{@link #complete(Try)}</li>
58+ * <li>{@link #completeWith(Future)}</li>
59+ * <li>{@link #failure(Throwable)}</li>
60+ * <li>{@link #success(Object)}</li>
6161 * </ul>
6262 *
63- * <h3>API for competing threads </h3>
63+ * <h3>API for concurrent completion </h3>
6464 * <p>
65- * When multiple {@code Thread}s may complete our {@code Promise}, we typically use one of these methods. Calls will
66- * gracefully return {@code false} if the {@code Promise} is already completed.
65+ * When multiple {@code Thread}s may attempt to complete the {@code Promise}, use one of the following "try" methods.
66+ * Calls will return {@code false} if the {@code Promise} is already completed:
6767 * <ul>
68- * <li>{@link #tryComplete(Try)}</li>
69- * <li>{@link #tryCompleteWith(Future)}</li>
70- * <li>{@link #tryFailure(Throwable)}</li>
71- * <li>{@link #trySuccess(Object)}</li>
68+ * <li>{@link #tryComplete(Try)}</li>
69+ * <li>{@link #tryCompleteWith(Future)}</li>
70+ * <li>{@link #tryFailure(Throwable)}</li>
71+ * <li>{@link #trySuccess(Object)}</li>
7272 * </ul>
7373 *
74- * @param <T> The result type of the underlying {@code Future}.
74+ * @param <T> the type of the value that completes the underlying {@code Future}
7575 * @author Daniel Dietrich
7676 */
7777public interface Promise <T > {
7878
7979 /**
80- * Creates a failed {@code Promise}, backed by the {@link Future#DEFAULT_EXECUTOR}.
80+ * Creates a {@code Promise} that is already completed with a failure, using the
81+ * {@link Future#DEFAULT_EXECUTOR} for asynchronous operations.
8182 *
82- * @param exception The reason why it failed.
83- * @param <T> The value type of a successful result.
84- * @return A failed {@code Promise}.
85- * @throws NullPointerException if exception is null
83+ * @param exception the cause of the failure
84+ * @param <T> the type of the value that would have been returned on success
85+ * @return a {@code Promise} completed with the given failure
86+ * @throws NullPointerException if {@code exception} is null
8687 */
8788 static <T > Promise <T > failed (Throwable exception ) {
8889 Objects .requireNonNull (exception , "exception is null" );
8990 return failed (DEFAULT_EXECUTOR , exception );
9091 }
9192
9293 /**
93- * Creates a failed {@code Promise}, backed by the given {@link Executor}.
94+ * Creates a {@code Promise} that is already completed with a failure, using the specified {@link Executor}
95+ * for asynchronous operations.
9496 *
95- * @param executor An {@code Executor} passed to the underlying {@link Future}.
96- * @param exception The reason why it failed.
97- * @param <T> The value type of a successful result.
98- * @return A failed {@code Promise}.
99- * @throws NullPointerException if executor or exception is null
97+ * @param executor the {@code Executor} used by the underlying {@link Future}
98+ * @param exception the cause of the failure
99+ * @param <T> the type of the value that would have been returned on success
100+ * @return a {@code Promise} completed with the given failure
101+ * @throws NullPointerException if {@code executor} or {@code exception} is null
100102 */
101103 static <T > Promise <T > failed (Executor executor , Throwable exception ) {
102104 Objects .requireNonNull (executor , "executor is null" );
@@ -105,25 +107,27 @@ static <T> Promise<T> failed(Executor executor, Throwable exception) {
105107 }
106108
107109 /**
108- * Creates a {@code Promise} from a {@link Try}, backed by the {@link Future#DEFAULT_EXECUTOR}.
110+ * Creates a {@code Promise} from the given {@link Try}, using the {@link Future#DEFAULT_EXECUTOR}
111+ * for asynchronous operations.
109112 *
110- * @param result The result.
111- * @param <T> The value type of a successful result.
112- * @return A completed {@code Promise} which contains either a {@code Success} or a {@code Failure}.
113- * @throws NullPointerException if result is null
113+ * @param result the {@code Try} representing a success or failure
114+ * @param <T> the type of the value in case of success
115+ * @return a {@code Promise} already completed with the given {@code Try} result
116+ * @throws NullPointerException if {@code result} is null
114117 */
115118 static <T > Promise <T > fromTry (Try <? extends T > result ) {
116119 return fromTry (DEFAULT_EXECUTOR , result );
117120 }
118121
119122 /**
120- * Creates a {@code Promise} from a {@link Try}, backed by the given {@link Executor}.
123+ * Creates a {@code Promise} from the given {@link Try}, using the specified {@link Executor}
124+ * for asynchronous operations.
121125 *
122- * @param executor An {@code Executor} passed to the underlying {@link Future}.
123- * @param result The result.
124- * @param <T> The value type of a successful result.
125- * @return A completed {@code Promise} which contains either a {@code Success} or a {@code Failure}.
126- * @throws NullPointerException if executor or result is null
126+ * @param executor the {@code Executor} used by the underlying {@link Future}
127+ * @param result the {@code Try} representing a success or failure
128+ * @param <T> the type of the value in case of success
129+ * @return a {@code Promise} already completed with the given {@code Try} result
130+ * @throws NullPointerException if {@code executor} or {@code result} is null
127131 */
128132 static <T > Promise <T > fromTry (Executor executor , Try <? extends T > result ) {
129133 Objects .requireNonNull (executor , "executor is null" );
@@ -132,62 +136,64 @@ static <T> Promise<T> fromTry(Executor executor, Try<? extends T> result) {
132136 }
133137
134138 /**
135- * Makes a {@code Promise} that isn't fulfilled yet, backed by the {@link Future#DEFAULT_EXECUTOR}.
136- * {@link ForkJoinPool#commonPool()}.
139+ * Creates a new {@code Promise} that is not yet completed, using the {@link Future#DEFAULT_EXECUTOR}
140+ * (typically {@link ForkJoinPool#commonPool()}) for asynchronous operations .
137141 *
138- * @param <T> Result type of the {@code Promise}.
139- * @return A new {@code Promise}.
142+ * @param <T> the type of the value that will complete the {@code Promise}
143+ * @return a new, uncompleted {@code Promise}
140144 */
141145 static <T > Promise <T > make () {
142146 return make (DEFAULT_EXECUTOR );
143147 }
144148
145149 /**
146- * Makes a {@code Promise} that isn't fulfilled yet, backed by the given {@link Executor}.
150+ * Creates a new {@code Promise} that is not yet completed, using the specified {@link Executor}
151+ * for asynchronous operations.
147152 *
148- * @param executor An {@code Executor} passed to the underlying {@link Future}.
149- * @param <T> Result type of the {@code Promise}.
150- * @return A new {@code Promise}.
151- * @throws NullPointerException if executor is null
153+ * @param executor the {@code Executor} used by the underlying {@link Future}
154+ * @param <T> the type of the value that will complete the {@code Promise}
155+ * @return a new, uncompleted {@code Promise}
156+ * @throws NullPointerException if {@code executor} is null
152157 */
153158 static <T > Promise <T > make (Executor executor ) {
154159 Objects .requireNonNull (executor , "executor is null" );
155160 return new PromiseImpl <>(FutureImpl .of (executor ));
156161 }
157162
158163 /**
159- * Narrows a widened {@code Promise<? extends T>} to {@code Promise<T>}
160- * by performing a type-safe cast. This is eligible because immutable/read-only
161- * collections are covariant.
164+ * Narrows a {@code Promise<? extends T>} to {@code Promise<T>} through a type-safe cast.
165+ * This is safe because immutable or read-only collections are covariant.
162166 *
163- * @param promise A {@code Promise}.
164- * @param <T> Component type of the {@code Promise}.
165- * @return the given {@code promise} instance as narrowed type {@code Promise<T>}.
167+ * @param promise the {@code Promise} to narrow
168+ * @param <T> the component type of the {@code Promise}
169+ * @return the same {@code promise} instance, cast to {@code Promise<T>}
166170 */
167171 @ SuppressWarnings ("unchecked" )
168172 static <T > Promise <T > narrow (Promise <? extends T > promise ) {
169173 return (Promise <T >) promise ;
170174 }
171175
172176 /**
173- * Creates a succeeded {@code Promise}, backed by the {@link Future#DEFAULT_EXECUTOR}.
177+ * Creates a {@code Promise} that is already completed successfully, using the
178+ * {@link Future#DEFAULT_EXECUTOR} for asynchronous operations.
174179 *
175- * @param result The result.
176- * @param <T> The value type of a successful result.
177- * @return A succeeded {@code Promise}.
180+ * @param result the value of the successful result
181+ * @param <T> the type of the value
182+ * @return a {@code Promise} already completed with the given result
178183 */
179184 static <T > Promise <T > successful (T result ) {
180185 return successful (DEFAULT_EXECUTOR , result );
181186 }
182187
183188 /**
184- * Creates a succeeded {@code Promise}, backed by the given {@link Executor}.
189+ * Creates a {@code Promise} that is already completed successfully, using the specified {@link Executor}
190+ * for asynchronous operations.
185191 *
186- * @param executor An {@code Executor} passed to the underlying {@link Future}.
187- * @param result The result.
188- * @param <T> The value type of a successful result.
189- * @return A succeeded {@code Promise}.
190- * @throws NullPointerException if executor is null
192+ * @param executor the {@code Executor} used by the underlying {@link Future}
193+ * @param result the value of the successful result
194+ * @param <T> the type of the value
195+ * @return a {@code Promise} already completed with the given result
196+ * @throws NullPointerException if {@code executor} is null
191197 */
192198 static <T > Promise <T > successful (Executor executor , T result ) {
193199 Objects .requireNonNull (executor , "executor is null" );
@@ -197,7 +203,7 @@ static <T> Promise<T> successful(Executor executor, T result) {
197203 /**
198204 * Returns the {@link Executor} used by the underlying {@link Future} of this {@code Promise}.
199205 *
200- * @return The underlying {@code Executor}.
206+ * @return the {@code Executor} associated with this {@code Promise}
201207 */
202208 default Executor executor () {
203209 return executorService ();
@@ -218,16 +224,16 @@ default Executor executor() {
218224 ExecutorService executorService ();
219225
220226 /**
221- * Returns the underlying {@link Future} of this {@code Promise}.
227+ * Returns the underlying {@link Future} associated with this {@code Promise}.
222228 *
223- * @return The {@code Future}.
229+ * @return the underlying {@code Future}
224230 */
225231 Future <T > future ();
226232
227233 /**
228- * Checks if this {@code Promise} is completed, i.e. has a value .
234+ * Checks whether this {@code Promise} has been completed, either successfully or with a failure .
229235 *
230- * @return true, if the computation successfully finished or failed, false otherwise.
236+ * @return {@code true} if the {@code Promise} is completed, {@code false} otherwise
231237 */
232238 default boolean isCompleted () {
233239 return future ().isCompleted ();
@@ -236,9 +242,9 @@ default boolean isCompleted() {
236242 /**
237243 * Completes this {@code Promise} with the given {@code value}.
238244 *
239- * @param value Either a {@link Try.Success} containing the result or a {@link Try.Failure} containing an exception.
240- * @return This {@code Promise}.
241- * @throws IllegalStateException if this {@code Promise} has already been completed.
245+ * @param value a {@link Try.Success} containing the result or a {@link Try.Failure} containing an exception
246+ * @return this {@code Promise}
247+ * @throws IllegalStateException if this {@code Promise} has already been completed
242248 */
243249 default Promise <T > complete (Try <? extends T > value ) {
244250 if (tryComplete (value )) {
@@ -249,72 +255,75 @@ default Promise<T> complete(Try<? extends T> value) {
249255 }
250256
251257 /**
252- * Attempts to completes this {@code Promise} with the given {@code value}.
258+ * Attempts to complete this {@code Promise} with the given {@code value}.
253259 *
254- * @param value Either a {@link Try.Success} containing the result or a {@link Try.Failure} containing an exception.
255- * @return {@code false } if this {@code Promise} has already been completed, {@code true} otherwise.
256- * @throws IllegalStateException if this {@code Promise} has already been completed.
260+ * @param value a {@link Try.Success} containing the result or a {@link Try.Failure} containing an exception
261+ * @return {@code true } if the {@code Promise} was completed successfully,
262+ * {@code false} if it was already completed
257263 */
258264 boolean tryComplete (Try <? extends T > value );
259265
260266 /**
261- * Completes this {@code Promise} with the given {@code Future}, once that {@code Future} is completed.
267+ * Completes this {@code Promise} with the result of the given {@code Future} once it is completed.
262268 *
263- * @param other Another {@code Future} to react on.
264- * @return This {@code Promise}.
269+ * @param other the {@code Future} whose result or failure will complete this {@code Promise}
270+ * @return this {@code Promise}
265271 */
266272 default Promise <T > completeWith (Future <? extends T > other ) {
267273 return tryCompleteWith (other );
268274 }
269275
270276 /**
271- * Attempts to complete this {@code Promise} with the specified {@code Future}, once that {@code Future} is completed.
277+ * Attempts to complete this {@code Promise} with the result of the given {@code Future} once it is completed.
272278 *
273- * @param other Another {@code Future} to react on.
274- * @return This {@code Promise}.
279+ * @param other the {@code Future} whose result or failure may complete this {@code Promise}
280+ * @return {@code true} if this {@code Promise} was completed by {@code other},
281+ * {@code false} if it was already completed
275282 */
276283 default Promise <T > tryCompleteWith (Future <? extends T > other ) {
277284 other .onComplete (this ::tryComplete );
278285 return this ;
279286 }
280287
281288 /**
282- * Completes this {@code Promise} with the given {@code value} .
289+ * Completes this {@code Promise} with the given value.
283290 *
284- * @param value A value.
285- * @return This {@code Promise}.
286- * @throws IllegalStateException if this {@code Promise} has already been completed.
291+ * @param value the value to complete this {@code Promise} with
292+ * @return this {@code Promise}
293+ * @throws IllegalStateException if this {@code Promise} has already been completed
287294 */
288295 default Promise <T > success (T value ) {
289296 return complete (Try .success (value ));
290297 }
291298
292299 /**
293- * Completes this {@code Promise} with the given {@code value} .
300+ * Attempts to complete this {@code Promise} with the given value.
294301 *
295- * @param value A value.
296- * @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
302+ * @param value the value to complete this {@code Promise} with
303+ * @return {@code true} if the {@code Promise} was completed successfully,
304+ * {@code false} if it was already completed
297305 */
298306 default boolean trySuccess (T value ) {
299307 return tryComplete (Try .success (value ));
300308 }
301309
302310 /**
303- * Completes this {@code Promise} with the given {@code exception} .
311+ * Completes this {@code Promise} with the given exception.
304312 *
305- * @param exception An exception.
306- * @return This {@code Promise}.
307- * @throws IllegalStateException if this {@code Promise} has already been completed.
313+ * @param exception the exception to complete this {@code Promise} with
314+ * @return this {@code Promise}
315+ * @throws IllegalStateException if this {@code Promise} has already been completed
308316 */
309317 default Promise <T > failure (Throwable exception ) {
310318 return complete (Try .failure (exception ));
311319 }
312320
313321 /**
314- * Completes this {@code Promise} with the given {@code exception} .
322+ * Attempts to complete this {@code Promise} with the given exception.
315323 *
316- * @param exception An exception.
317- * @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
324+ * @param exception the exception to complete this {@code Promise} with
325+ * @return {@code true} if the {@code Promise} was completed successfully,
326+ * {@code false} if it was already completed
318327 */
319328 default boolean tryFailure (Throwable exception ) {
320329 return tryComplete (Try .failure (exception ));
0 commit comments