1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * A trait defining behavior for routing an [`Invoice`] payment.
12 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
13 public class Router extends CommonBase {
14 final bindings.LDKRouter bindings_instance;
15 Router(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
16 private Router(bindings.LDKRouter arg) {
17 super(bindings.LDKRouter_new(arg));
18 this.ptrs_to.add(arg);
19 this.bindings_instance = arg;
21 @Override @SuppressWarnings("deprecation")
22 protected void finalize() throws Throwable {
23 if (ptr != 0) { bindings.Router_free(ptr); } super.finalize();
26 public static interface RouterInterface {
28 * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
30 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
32 Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, ChannelDetails[] first_hops, Score scorer);
34 private static class LDKRouterHolder { Router held; }
35 public static Router new_impl(RouterInterface arg) {
36 final LDKRouterHolder impl_holder = new LDKRouterHolder();
37 impl_holder.held = new Router(new bindings.LDKRouter() {
38 @Override public long find_route(byte[] payer, long params, long[] first_hops, long scorer) {
39 RouteParameters params_hu_conv = null; if (params < 0 || params > 4096) { params_hu_conv = new RouteParameters(null, params); }
40 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops.length];
41 for (int q = 0; q < first_hops.length; q++) {
42 long first_hops_conv_16 = first_hops[q];
43 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); }
44 first_hops_conv_16_hu_conv.ptrs_to.add(this);
45 first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
47 Score ret_hu_conv = new Score(null, scorer);
48 ret_hu_conv.ptrs_to.add(this);
49 Result_RouteLightningErrorZ ret = arg.find_route(payer, params_hu_conv, first_hops_conv_16_arr, ret_hu_conv);
50 long result = ret != null ? ret.ptr : 0;
54 return impl_holder.held;
57 * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
59 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
61 public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, @Nullable ChannelDetails[] first_hops, Score scorer) {
62 long ret = bindings.Router_find_route(this.ptr, payer, params == null ? 0 : params.ptr & ~1, 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);
63 if (ret >= 0 && ret <= 4096) { return null; }
64 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
65 this.ptrs_to.add(params);
66 for (ChannelDetails first_hops_conv_16: first_hops) { this.ptrs_to.add(first_hops_conv_16); };
67 this.ptrs_to.add(scorer);