Support passing trait instance references from C to Java without clone or double...
[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 javax.annotation.Nullable;
8
9
10 /**
11  * A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
12  * it can take multiple paths. Each path is composed of one or more hops through the network.
13  */
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class Route extends CommonBase {
16         Route(Object _dummy, long ptr) { super(ptr); }
17         @Override @SuppressWarnings("deprecation")
18         protected void finalize() throws Throwable {
19                 super.finalize();
20                 if (ptr != 0) { bindings.Route_free(ptr); }
21         }
22
23         /**
24          * The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
25          * last RouteHop in each path must be the same.
26          * Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
27          * destination. Thus, this must always be at least length one. While the maximum length of any
28          * given path is variable, keeping the length of any path to less than 20 should currently
29          * ensure it is viable.
30          */
31         public RouteHop[][] get_paths() {
32                 long[][] ret = bindings.Route_get_paths(this.ptr);
33                 RouteHop[][] ret_conv_12_arr = new RouteHop[ret.length][];
34                 for (int m = 0; m < ret.length; m++) {
35                         long[] ret_conv_12 = ret[m];
36                         RouteHop[] ret_conv_12_conv_10_arr = new RouteHop[ret_conv_12.length];
37                         for (int k = 0; k < ret_conv_12.length; k++) {
38                                 long ret_conv_12_conv_10 = ret_conv_12[k];
39                                 RouteHop ret_conv_12_conv_10_hu_conv = new RouteHop(null, ret_conv_12_conv_10);
40                                 ret_conv_12_conv_10_hu_conv.ptrs_to.add(this);
41                                 ret_conv_12_conv_10_arr[k] = ret_conv_12_conv_10_hu_conv;
42                         }
43                         ret_conv_12_arr[m] = ret_conv_12_conv_10_arr;
44                 }
45                 return ret_conv_12_arr;
46         }
47
48         /**
49          * The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
50          * last RouteHop in each path must be the same.
51          * Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
52          * destination. Thus, this must always be at least length one. While the maximum length of any
53          * given path is variable, keeping the length of any path to less than 20 should currently
54          * ensure it is viable.
55          */
56         public void set_paths(RouteHop[][] val) {
57                 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);
58         }
59
60         /**
61          * Constructs a new Route given each field
62          */
63         public static Route of(RouteHop[][] paths_arg) {
64                 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);
65                 if (ret >= 0 && ret < 1024) { return null; }
66                 Route ret_hu_conv = new Route(null, ret);
67                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
68                 return ret_hu_conv;
69         }
70
71         /**
72          * Creates a copy of the Route
73          */
74         public Route clone() {
75                 long ret = bindings.Route_clone(this.ptr);
76                 if (ret >= 0 && ret < 1024) { return null; }
77                 Route ret_hu_conv = new Route(null, ret);
78                 ret_hu_conv.ptrs_to.add(this);
79                 return ret_hu_conv;
80         }
81
82         /**
83          * Checks if two Routes contain equal inner contents.
84          */
85         public long hash() {
86                 long ret = bindings.Route_hash(this.ptr);
87                 return ret;
88         }
89
90         /**
91          * Checks if two Routes contain equal inner contents.
92          * This ignores pointers and is_owned flags and looks at the values in fields.
93          * Two objects with NULL inner values will be considered "equal" here.
94          */
95         public boolean eq(Route b) {
96                 boolean ret = bindings.Route_eq(this.ptr, b == null ? 0 : b.ptr & ~1);
97                 this.ptrs_to.add(b);
98                 return ret;
99         }
100
101         /**
102          * Returns the total amount of fees paid on this [`Route`].
103          * 
104          * This doesn't include any extra payment made to the recipient, which can happen in excess of
105          * the amount passed to [`get_route`]'s `final_value_msat`.
106          */
107         public long get_total_fees() {
108                 long ret = bindings.Route_get_total_fees(this.ptr);
109                 return ret;
110         }
111
112         /**
113          * Returns the total amount paid on this [`Route`], excluding the fees.
114          */
115         public long get_total_amount() {
116                 long ret = bindings.Route_get_total_amount(this.ptr);
117                 return ret;
118         }
119
120         /**
121          * Serialize the Route object into a byte array which can be read by Route_read
122          */
123         public byte[] write() {
124                 byte[] ret = bindings.Route_write(this.ptr);
125                 return ret;
126         }
127
128         /**
129          * Read a Route from a byte array, created by Route_write
130          */
131         public static Result_RouteDecodeErrorZ read(byte[] ser) {
132                 long ret = bindings.Route_read(ser);
133                 if (ret >= 0 && ret < 1024) { return null; }
134                 Result_RouteDecodeErrorZ ret_hu_conv = Result_RouteDecodeErrorZ.constr_from_ptr(ret);
135                 return ret_hu_conv;
136         }
137
138 }