b21cea97b488ddf3af679341767f15846b74b9c9
[ldk-java] / src / main / java / org / ldk / structs / FutureCallback.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * A callback which is called when a [`Future`] completes.
12  * 
13  * Note that this MUST NOT call back into LDK directly, it must instead schedule actions to be
14  * taken later. Rust users should use the [`std::future::Future`] implementation for [`Future`]
15  * instead.
16  * 
17  * Note that the [`std::future::Future`] implementation may only work for runtimes which schedule
18  * futures when they receive a wake, rather than immediately executing them.
19  */
20 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
21 public class FutureCallback extends CommonBase {
22         final bindings.LDKFutureCallback bindings_instance;
23         FutureCallback(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
24         private FutureCallback(bindings.LDKFutureCallback arg) {
25                 super(bindings.LDKFutureCallback_new(arg));
26                 this.ptrs_to.add(arg);
27                 this.bindings_instance = arg;
28         }
29         @Override @SuppressWarnings("deprecation")
30         protected void finalize() throws Throwable {
31                 if (ptr != 0) { bindings.FutureCallback_free(ptr); } super.finalize();
32         }
33
34         public static interface FutureCallbackInterface {
35                 /**
36                  * The method which is called.
37                  */
38                 void call();
39         }
40         private static class LDKFutureCallbackHolder { FutureCallback held; }
41         public static FutureCallback new_impl(FutureCallbackInterface arg) {
42                 final LDKFutureCallbackHolder impl_holder = new LDKFutureCallbackHolder();
43                 impl_holder.held = new FutureCallback(new bindings.LDKFutureCallback() {
44                         @Override public void call() {
45                                 arg.call();
46                                 Reference.reachabilityFence(arg);
47                         }
48                 });
49                 return impl_holder.held;
50         }
51         /**
52          * The method which is called.
53          */
54         public void call() {
55                 bindings.FutureCallback_call(this.ptr);
56                 Reference.reachabilityFence(this);
57         }
58
59 }