1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * A trait implemented for objects handling events from [`EventsProvider`].
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`.
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;
26 @Override @SuppressWarnings("deprecation")
27 protected void finalize() throws Throwable {
28 if (ptr != 0) { bindings.EventHandler_free(ptr); } super.finalize();
31 * Destroys the object, freeing associated resources. After this call, any access
32 * to this object may result in a SEGFAULT or worse.
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
39 public void destroy() {
40 if (ptr != 0) { bindings.EventHandler_free(ptr); }
43 public static interface EventHandlerInterface {
45 * Handles the given [`Event`].
47 * See [`EventsProvider`] for details that must be considered when implementing this method.
49 void handle_event(Event event);
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);
62 return impl_holder.held;
65 * Handles the given [`Event`].
67 * See [`EventsProvider`] for details that must be considered when implementing this method.
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); };