529868cd90fdb4769ae5643b0fac493f0ab9753a
[ldk-java] / src / main / java / org / ldk / structs / Router.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 defining behavior for routing an [`Invoice`] payment.
12  */
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class Router extends CommonBase {
15         final bindings.LDKRouter bindings_instance;
16         Router(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17         private Router(bindings.LDKRouter arg) {
18                 super(bindings.LDKRouter_new(arg));
19                 this.ptrs_to.add(arg);
20                 this.bindings_instance = arg;
21         }
22         @Override @SuppressWarnings("deprecation")
23         protected void finalize() throws Throwable {
24                 if (ptr != 0) { bindings.Router_free(ptr); } super.finalize();
25         }
26
27         public static interface RouterInterface {
28                 /**
29                  * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
30                  * 
31                  * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
32                  */
33                 Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, byte[] payment_hash, ChannelDetails[] first_hops, Score scorer);
34         }
35         private static class LDKRouterHolder { Router held; }
36         public static Router new_impl(RouterInterface arg) {
37                 final LDKRouterHolder impl_holder = new LDKRouterHolder();
38                 impl_holder.held = new Router(new bindings.LDKRouter() {
39                         @Override public long find_route(byte[] payer, long params, byte[] payment_hash, long[] first_hops, long scorer) {
40                                 RouteParameters params_hu_conv = null; if (params < 0 || params > 4096) { params_hu_conv = new RouteParameters(null, params); }
41                                 int first_hops_conv_16_len = first_hops.length;
42                                 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops_conv_16_len];
43                                 for (int q = 0; q < first_hops_conv_16_len; q++) {
44                                         long first_hops_conv_16 = first_hops[q];
45                                         ChannelDetails first_hops_conv_16_hu_conv = null; if (first_hops_conv_16 < 0 || first_hops_conv_16 > 4096) { first_hops_conv_16_hu_conv = new ChannelDetails(null, first_hops_conv_16); }
46                                         first_hops_conv_16_hu_conv.ptrs_to.add(this);
47                                         first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
48                                 }
49                                 Score ret_hu_conv = new Score(null, scorer);
50                                 ret_hu_conv.ptrs_to.add(this);
51                                 Result_RouteLightningErrorZ ret = arg.find_route(payer, params_hu_conv, payment_hash, first_hops_conv_16_arr, ret_hu_conv);
52                                 long result = ret == null ? 0 : ret.clone_ptr();
53                                 return result;
54                         }
55                 });
56                 return impl_holder.held;
57         }
58         /**
59          * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
60          * 
61          * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
62          */
63         public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, byte[] payment_hash, @Nullable ChannelDetails[] first_hops, Score scorer) {
64                 long ret = bindings.Router_find_route(this.ptr, InternalUtils.check_arr_len(payer, 33), params == null ? 0 : params.ptr & ~1, InternalUtils.check_arr_len(payment_hash, 32), first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr & ~1).toArray() : null, scorer == null ? 0 : scorer.ptr);
65                 Reference.reachabilityFence(this);
66                 Reference.reachabilityFence(payer);
67                 Reference.reachabilityFence(params);
68                 Reference.reachabilityFence(payment_hash);
69                 Reference.reachabilityFence(first_hops);
70                 Reference.reachabilityFence(scorer);
71                 if (ret >= 0 && ret <= 4096) { return null; }
72                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
73                 this.ptrs_to.add(params);
74                 for (ChannelDetails first_hops_conv_16: first_hops) { this.ptrs_to.add(first_hops_conv_16); };
75                 this.ptrs_to.add(scorer);
76                 return ret_hu_conv;
77         }
78
79 }