[C#] Run tests against release library in determinism CI run
[ldk-java] / src / main / java / org / ldk / structs / EventHandler.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 trait implemented for objects handling events from [`EventsProvider`].
12  * 
13  * An async variation also exists for implementations of [`EventsProvider`] that support async
14  * event handling. The async event handler should satisfy the generic bounds: `F:
15  * core::future::Future, H: Fn(Event) -> F`.
16  */
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class EventHandler extends CommonBase {
19         final bindings.LDKEventHandler bindings_instance;
20         EventHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
21         private EventHandler(bindings.LDKEventHandler arg) {
22                 super(bindings.LDKEventHandler_new(arg));
23                 this.ptrs_to.add(arg);
24                 this.bindings_instance = arg;
25         }
26         @Override @SuppressWarnings("deprecation")
27         protected void finalize() throws Throwable {
28                 if (ptr != 0) { bindings.EventHandler_free(ptr); } super.finalize();
29         }
30         /**
31          * Destroys the object, freeing associated resources. After this call, any access
32          * to this object may result in a SEGFAULT or worse.
33          *
34          * You should generally NEVER call this method. You should let the garbage collector
35          * do this for you when it finalizes objects. However, it may be useful for types
36          * which represent locks and should be closed immediately to avoid holding locks
37          * until the GC runs.
38          */
39         public void destroy() {
40                 if (ptr != 0) { bindings.EventHandler_free(ptr); }
41                 ptr = 0;
42         }
43         public static interface EventHandlerInterface {
44                 /**
45                  * Handles the given [`Event`].
46                  * 
47                  * See [`EventsProvider`] for details that must be considered when implementing this method.
48                  */
49                 void handle_event(Event event);
50         }
51         private static class LDKEventHandlerHolder { EventHandler held; }
52         public static EventHandler new_impl(EventHandlerInterface arg) {
53                 final LDKEventHandlerHolder impl_holder = new LDKEventHandlerHolder();
54                 impl_holder.held = new EventHandler(new bindings.LDKEventHandler() {
55                         @Override public void handle_event(long event) {
56                                 org.ldk.structs.Event event_hu_conv = org.ldk.structs.Event.constr_from_ptr(event);
57                                 if (event_hu_conv != null) { event_hu_conv.ptrs_to.add(this); };
58                                 arg.handle_event(event_hu_conv);
59                                 Reference.reachabilityFence(arg);
60                         }
61                 });
62                 return impl_holder.held;
63         }
64         /**
65          * Handles the given [`Event`].
66          * 
67          * See [`EventsProvider`] for details that must be considered when implementing this method.
68          */
69         public void handle_event(org.ldk.structs.Event event) {
70                 bindings.EventHandler_handle_event(this.ptr, event.ptr);
71                 Reference.reachabilityFence(this);
72                 Reference.reachabilityFence(event);
73                 if (this != null) { this.ptrs_to.add(event); };
74         }
75
76 }