03f8921bdbb6c67a88b1baf8a209d4ab458dc022
[ldk-java] / src / main / java / org / ldk / structs / EventsProvider.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 javax.annotation.Nullable;
8
9 /**
10  * A trait indicating an object may generate events.
11  * 
12  * Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
13  * 
14  * # Requirements
15  * 
16  * See [`process_pending_events`] for requirements around event processing.
17  * 
18  * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
19  * event since the last invocation. The handler must either act upon the event immediately
20  * or preserve it for later handling.
21  * 
22  * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
23  * consult the provider's documentation on the implication of processing events and how a handler
24  * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
25  * [`ChainMonitor::process_pending_events`]).
26  * 
27  * (C-not implementable) As there is likely no reason for a user to implement this trait on their
28  * own type(s).
29  * 
30  * [`process_pending_events`]: Self::process_pending_events
31  * [`handle_event`]: EventHandler::handle_event
32  * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
33  * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
34  */
35 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
36 public class EventsProvider extends CommonBase {
37         final bindings.LDKEventsProvider bindings_instance;
38         EventsProvider(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
39         private EventsProvider(bindings.LDKEventsProvider arg) {
40                 super(bindings.LDKEventsProvider_new(arg));
41                 this.ptrs_to.add(arg);
42                 this.bindings_instance = arg;
43         }
44         @Override @SuppressWarnings("deprecation")
45         protected void finalize() throws Throwable {
46                 if (ptr != 0) { bindings.EventsProvider_free(ptr); } super.finalize();
47         }
48
49         public static interface EventsProviderInterface {
50                 /**
51                  * Processes any events generated since the last call using the given event handler.
52                  * 
53                  * Subsequent calls must only process new events. However, handlers must be capable of handling
54                  * duplicate events across process restarts. This may occur if the provider was recovered from
55                  * an old state (i.e., it hadn't been successfully persisted after processing pending events).
56                  */
57                 void process_pending_events(EventHandler handler);
58         }
59         private static class LDKEventsProviderHolder { EventsProvider held; }
60         public static EventsProvider new_impl(EventsProviderInterface arg) {
61                 final LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
62                 impl_holder.held = new EventsProvider(new bindings.LDKEventsProvider() {
63                         @Override public void process_pending_events(long handler) {
64                                 EventHandler ret_hu_conv = new EventHandler(null, handler);
65                                 ret_hu_conv.ptrs_to.add(this);
66                                 arg.process_pending_events(ret_hu_conv);
67                         }
68                 });
69                 return impl_holder.held;
70         }
71         /**
72          * Processes any events generated since the last call using the given event handler.
73          * 
74          * Subsequent calls must only process new events. However, handlers must be capable of handling
75          * duplicate events across process restarts. This may occur if the provider was recovered from
76          * an old state (i.e., it hadn't been successfully persisted after processing pending events).
77          */
78         public void process_pending_events(EventHandler handler) {
79                 bindings.EventsProvider_process_pending_events(this.ptr, handler == null ? 0 : handler.ptr);
80                 this.ptrs_to.add(handler);
81         }
82
83 }