9682331efbe810d2c40332770f12dbc1e62a9789
[ldk-java] / src / main / java / org / ldk / structs / Route.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 /**
12  * A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
13  * it can take multiple paths. Each path is composed of one or more hops through the network.
14  */
15 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
16 public class Route extends CommonBase {
17         Route(Object _dummy, long ptr) { super(ptr); }
18         @Override @SuppressWarnings("deprecation")
19         protected void finalize() throws Throwable {
20                 super.finalize();
21                 if (ptr != 0) { bindings.Route_free(ptr); }
22         }
23
24         /**
25          * The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
26          * last RouteHop in each path must be the same.
27          * Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
28          * destination. Thus, this must always be at least length one. While the maximum length of any
29          * given path is variable, keeping the length of any path to less than 20 should currently
30          * ensure it is viable.
31          */
32         public RouteHop[][] get_paths() {
33                 long[][] ret = bindings.Route_get_paths(this.ptr);
34                 Reference.reachabilityFence(this);
35                 int ret_conv_12_len = ret.length;
36                 RouteHop[][] ret_conv_12_arr = new RouteHop[ret_conv_12_len][];
37                 for (int m = 0; m < ret_conv_12_len; m++) {
38                         long[] ret_conv_12 = ret[m];
39                         int ret_conv_12_conv_10_len = ret_conv_12.length;
40                         RouteHop[] ret_conv_12_conv_10_arr = new RouteHop[ret_conv_12_conv_10_len];
41                         for (int k = 0; k < ret_conv_12_conv_10_len; k++) {
42                                 long ret_conv_12_conv_10 = ret_conv_12[k];
43                                 RouteHop ret_conv_12_conv_10_hu_conv = null; if (ret_conv_12_conv_10 < 0 || ret_conv_12_conv_10 > 4096) { ret_conv_12_conv_10_hu_conv = new RouteHop(null, ret_conv_12_conv_10); }
44                                 ret_conv_12_conv_10_hu_conv.ptrs_to.add(this);
45                                 ret_conv_12_conv_10_arr[k] = ret_conv_12_conv_10_hu_conv;
46                         }
47                         ret_conv_12_arr[m] = ret_conv_12_conv_10_arr;
48                 }
49                 return ret_conv_12_arr;
50         }
51
52         /**
53          * The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
54          * last RouteHop in each path must be the same.
55          * Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
56          * destination. Thus, this must always be at least length one. While the maximum length of any
57          * given path is variable, keeping the length of any path to less than 20 should currently
58          * ensure it is viable.
59          */
60         public void set_paths(RouteHop[][] val) {
61                 bindings.Route_set_paths(this.ptr, val != null ? Arrays.stream(val).map(val_conv_12 -> val_conv_12 != null ? Arrays.stream(val_conv_12).mapToLong(val_conv_12_conv_10 -> val_conv_12_conv_10 == null ? 0 : val_conv_12_conv_10.ptr & ~1).toArray() : null).toArray(long[][]::new) : null);
62                 Reference.reachabilityFence(this);
63                 Reference.reachabilityFence(val);
64         }
65
66         /**
67          * The `payee` parameter passed to [`find_route`].
68          * This is used by `ChannelManager` to track information which may be required for retries,
69          * provided back to you via [`Event::PaymentPathFailed`].
70          * 
71          * [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
72          * 
73          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
74          */
75         @Nullable
76         public Payee get_payee() {
77                 long ret = bindings.Route_get_payee(this.ptr);
78                 Reference.reachabilityFence(this);
79                 if (ret >= 0 && ret <= 4096) { return null; }
80                 Payee ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new Payee(null, ret); }
81                 ret_hu_conv.ptrs_to.add(this);
82                 return ret_hu_conv;
83         }
84
85         /**
86          * The `payee` parameter passed to [`find_route`].
87          * This is used by `ChannelManager` to track information which may be required for retries,
88          * provided back to you via [`Event::PaymentPathFailed`].
89          * 
90          * [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
91          * 
92          * Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None
93          */
94         public void set_payee(@Nullable Payee val) {
95                 bindings.Route_set_payee(this.ptr, val == null ? 0 : val.ptr & ~1);
96                 Reference.reachabilityFence(this);
97                 Reference.reachabilityFence(val);
98         }
99
100         /**
101          * Constructs a new Route given each field
102          */
103         public static Route of(RouteHop[][] paths_arg, Payee payee_arg) {
104                 long ret = bindings.Route_new(paths_arg != null ? Arrays.stream(paths_arg).map(paths_arg_conv_12 -> paths_arg_conv_12 != null ? Arrays.stream(paths_arg_conv_12).mapToLong(paths_arg_conv_12_conv_10 -> paths_arg_conv_12_conv_10 == null ? 0 : paths_arg_conv_12_conv_10.ptr & ~1).toArray() : null).toArray(long[][]::new) : null, payee_arg == null ? 0 : payee_arg.ptr & ~1);
105                 Reference.reachabilityFence(paths_arg);
106                 Reference.reachabilityFence(payee_arg);
107                 if (ret >= 0 && ret <= 4096) { return null; }
108                 Route ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new Route(null, ret); }
109                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
110                 return ret_hu_conv;
111         }
112
113         long clone_ptr() {
114                 long ret = bindings.Route_clone_ptr(this.ptr);
115                 Reference.reachabilityFence(this);
116                 return ret;
117         }
118
119         /**
120          * Creates a copy of the Route
121          */
122         public Route clone() {
123                 long ret = bindings.Route_clone(this.ptr);
124                 Reference.reachabilityFence(this);
125                 if (ret >= 0 && ret <= 4096) { return null; }
126                 Route ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new Route(null, ret); }
127                 ret_hu_conv.ptrs_to.add(this);
128                 return ret_hu_conv;
129         }
130
131         /**
132          * Checks if two Routes contain equal inner contents.
133          */
134         public long hash() {
135                 long ret = bindings.Route_hash(this.ptr);
136                 Reference.reachabilityFence(this);
137                 return ret;
138         }
139
140         @Override public int hashCode() {
141                 return (int)this.hash();
142         }
143         /**
144          * Checks if two Routes contain equal inner contents.
145          * This ignores pointers and is_owned flags and looks at the values in fields.
146          * Two objects with NULL inner values will be considered "equal" here.
147          */
148         public boolean eq(Route b) {
149                 boolean ret = bindings.Route_eq(this.ptr, b == null ? 0 : b.ptr & ~1);
150                 Reference.reachabilityFence(this);
151                 Reference.reachabilityFence(b);
152                 this.ptrs_to.add(b);
153                 return ret;
154         }
155
156         @Override public boolean equals(Object o) {
157                 if (!(o instanceof Route)) return false;
158                 return this.eq((Route)o);
159         }
160         /**
161          * Returns the total amount of fees paid on this [`Route`].
162          * 
163          * This doesn't include any extra payment made to the recipient, which can happen in excess of
164          * the amount passed to [`find_route`]'s `params.final_value_msat`.
165          */
166         public long get_total_fees() {
167                 long ret = bindings.Route_get_total_fees(this.ptr);
168                 Reference.reachabilityFence(this);
169                 return ret;
170         }
171
172         /**
173          * Returns the total amount paid on this [`Route`], excluding the fees.
174          */
175         public long get_total_amount() {
176                 long ret = bindings.Route_get_total_amount(this.ptr);
177                 Reference.reachabilityFence(this);
178                 return ret;
179         }
180
181         /**
182          * Serialize the Route object into a byte array which can be read by Route_read
183          */
184         public byte[] write() {
185                 byte[] ret = bindings.Route_write(this.ptr);
186                 Reference.reachabilityFence(this);
187                 return ret;
188         }
189
190         /**
191          * Read a Route from a byte array, created by Route_write
192          */
193         public static Result_RouteDecodeErrorZ read(byte[] ser) {
194                 long ret = bindings.Route_read(ser);
195                 Reference.reachabilityFence(ser);
196                 if (ret >= 0 && ret <= 4096) { return null; }
197                 Result_RouteDecodeErrorZ ret_hu_conv = Result_RouteDecodeErrorZ.constr_from_ptr(ret);
198                 return ret_hu_conv;
199         }
200
201 }