MCPcopy Create free account
hub / github.com/traneio/future / Promise

Class Promise

future-java/src/main/java/io/trane/future/Promise.java:26–864  ·  view source on GitHub ↗

Promise is a Future that provides methods to set its result. They are useful to interact with callback-based APIs like the ones that are typically provided by network libraries. A promise can be created and returned synchronously to the caller, but its completion is deferred until the value is set,

Source from the content-addressed store, hash-verified

24 * the type of the asynchronous computation result
25 */
26public abstract class Promise<T> implements Future<T> {
27
28 private static final long STATE_OFFSET = Unsafe.objectFieldOffset(Promise.class, "state");
29 private static final Logger LOGGER = Logger.getLogger(Promise.class.getName());
30
31 /**
32 * Creates a promise that triggers the provided handlers in case it receives
33 * an interrupt.
34 *
35 * @param handlers the list of handlers to be triggered.
36 * @return the new promise instance.
37 */
38 public static final <T> Promise<T> apply(final List<? extends InterruptHandler> handlers) {
39 final Optional<?>[] savedContext = Local.save();
40 if (savedContext.length == 0)
41 return new Promise<T>() {
42 @Override
43 protected final Optional<?>[] getSavedContext() {
44 return Local.EMPTY;
45 }
46
47 @Override
48 protected final InterruptHandler getInterruptHandler() {
49 return InterruptHandler.apply(handlers);
50 }
51 };
52 else
53 return new Promise<T>() {
54 @Override
55 protected final Optional<?>[] getSavedContext() {
56 return savedContext;
57 }
58
59 @Override
60 protected final InterruptHandler getInterruptHandler() {
61 return InterruptHandler.apply(handlers);
62 }
63 };
64 }
65
66 /**
67 * Creates a promise that triggers the provided handler in case it receives an
68 * interrupt.
69 *
70 * @param handler the handler to be triggered.
71 * @return the new promise instance.
72 */
73 public static final <T> Promise<T> apply(final InterruptHandler handler) {
74 final Optional<?>[] savedContext = Local.save();
75 if (savedContext.length == 0)
76 return new Promise<T>() {
77 @Override
78 protected final Optional<?>[] getSavedContext() {
79 return Local.EMPTY;
80 }
81
82 @Override
83 protected final InterruptHandler getInterruptHandler() {

Callers

nothing calls this directly

Calls 1

objectFieldOffsetMethod · 0.95

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…