|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.util.concurrent;
|
18 | 18 |
|
19 |
| -import java.time.Duration; |
20 |
| -import java.util.concurrent.TimeUnit; |
21 |
| - |
22 | 19 | import reactor.core.publisher.Mono;
|
23 |
| -import reactor.core.publisher.MonoProcessor; |
24 |
| - |
25 |
| -import org.springframework.lang.Nullable; |
26 |
| -import org.springframework.util.Assert; |
27 | 20 |
|
28 | 21 | /**
|
29 |
| - * Adapts a {@link Mono} into a {@link ListenableFuture}. |
| 22 | + * Adapts a {@link Mono} into a {@link ListenableFuture} by obtaining a |
| 23 | + * {@code CompletableFuture} from the {@code Mono} via {@link Mono#toFuture()} |
| 24 | + * and then adapting it with {@link CompletableToListenableFutureAdapter}. |
30 | 25 | *
|
31 | 26 | * @author Rossen Stoyanchev
|
32 | 27 | * @author Stephane Maldini
|
33 | 28 | * @since 5.1
|
34 | 29 | * @param <T> the object type
|
35 | 30 | */
|
36 |
| -@SuppressWarnings("deprecation") |
37 |
| -public class MonoToListenableFutureAdapter<T> implements ListenableFuture<T> { |
38 |
| - |
39 |
| - private final MonoProcessor<T> processor; |
40 |
| - |
41 |
| - private final ListenableFutureCallbackRegistry<T> registry = new ListenableFutureCallbackRegistry<>(); |
42 |
| - |
| 31 | +public class MonoToListenableFutureAdapter<T> extends CompletableToListenableFutureAdapter<T> { |
43 | 32 |
|
44 | 33 | public MonoToListenableFutureAdapter(Mono<T> mono) {
|
45 |
| - Assert.notNull(mono, "Mono must not be null"); |
46 |
| - this.processor = mono |
47 |
| - .doOnSuccess(this.registry::success) |
48 |
| - .doOnError(this.registry::failure) |
49 |
| - .toProcessor(); |
50 |
| - } |
51 |
| - |
52 |
| - |
53 |
| - @Override |
54 |
| - @Nullable |
55 |
| - public T get() { |
56 |
| - return this.processor.block(); |
57 |
| - } |
58 |
| - |
59 |
| - @Override |
60 |
| - @Nullable |
61 |
| - public T get(long timeout, TimeUnit unit) { |
62 |
| - Assert.notNull(unit, "TimeUnit must not be null"); |
63 |
| - Duration duration = Duration.ofMillis(TimeUnit.MILLISECONDS.convert(timeout, unit)); |
64 |
| - return this.processor.block(duration); |
65 |
| - } |
66 |
| - |
67 |
| - @Override |
68 |
| - public boolean cancel(boolean mayInterruptIfRunning) { |
69 |
| - if (isCancelled()) { |
70 |
| - return false; |
71 |
| - } |
72 |
| - this.processor.cancel(); |
73 |
| - // isCancelled may still return false, if mono completed before the cancel |
74 |
| - return this.processor.isCancelled(); |
75 |
| - } |
76 |
| - |
77 |
| - @Override |
78 |
| - public boolean isCancelled() { |
79 |
| - return this.processor.isCancelled(); |
80 |
| - } |
81 |
| - |
82 |
| - @Override |
83 |
| - public boolean isDone() { |
84 |
| - return this.processor.isTerminated(); |
85 |
| - } |
86 |
| - |
87 |
| - @Override |
88 |
| - public void addCallback(ListenableFutureCallback<? super T> callback) { |
89 |
| - this.registry.addCallback(callback); |
90 |
| - } |
91 |
| - |
92 |
| - @Override |
93 |
| - public void addCallback(SuccessCallback<? super T> success, FailureCallback failure) { |
94 |
| - this.registry.addSuccessCallback(success); |
95 |
| - this.registry.addFailureCallback(failure); |
| 34 | + super(mono.toFuture()); |
96 | 35 | }
|
97 | 36 |
|
98 | 37 | }
|
0 commit comments