8ff6799d27e70d3c8b55de3421426808224764b8
[ldk-java] / src / main / java / org / ldk / structs / Destination.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  * The destination of an onion message.
13  */
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class Destination extends CommonBase {
16         private Destination(Object _dummy, long ptr) { super(ptr); }
17         @Override @SuppressWarnings("deprecation")
18         protected void finalize() throws Throwable {
19                 super.finalize();
20                 if (ptr != 0) { bindings.Destination_free(ptr); }
21         }
22         static Destination constr_from_ptr(long ptr) {
23                 bindings.LDKDestination raw_val = bindings.LDKDestination_ref_from_ptr(ptr);
24                 if (raw_val.getClass() == bindings.LDKDestination.Node.class) {
25                         return new Node(ptr, (bindings.LDKDestination.Node)raw_val);
26                 }
27                 if (raw_val.getClass() == bindings.LDKDestination.BlindedRoute.class) {
28                         return new BlindedRoute(ptr, (bindings.LDKDestination.BlindedRoute)raw_val);
29                 }
30                 assert false; return null; // Unreachable without extending the (internal) bindings interface
31         }
32
33         /**
34          * We're sending this onion message to a node.
35          */
36         public final static class Node extends Destination {
37                 public final byte[] node;
38                 private Node(long ptr, bindings.LDKDestination.Node obj) {
39                         super(null, ptr);
40                         this.node = obj.node;
41                 }
42         }
43         /**
44          * We're sending this onion message to a blinded route.
45          */
46         public final static class BlindedRoute extends Destination {
47                 public final org.ldk.structs.BlindedRoute blinded_route;
48                 private BlindedRoute(long ptr, bindings.LDKDestination.BlindedRoute obj) {
49                         super(null, ptr);
50                         long blinded_route = obj.blinded_route;
51                         org.ldk.structs.BlindedRoute blinded_route_hu_conv = null; if (blinded_route < 0 || blinded_route > 4096) { blinded_route_hu_conv = new org.ldk.structs.BlindedRoute(null, blinded_route); }
52                         if (blinded_route_hu_conv != null) { blinded_route_hu_conv.ptrs_to.add(this); };
53                         this.blinded_route = blinded_route_hu_conv;
54                 }
55         }
56         /**
57          * Utility method to constructs a new Node-variant Destination
58          */
59         public static Destination node(byte[] a) {
60                 long ret = bindings.Destination_node(InternalUtils.check_arr_len(a, 33));
61                 Reference.reachabilityFence(a);
62                 if (ret >= 0 && ret <= 4096) { return null; }
63                 org.ldk.structs.Destination ret_hu_conv = org.ldk.structs.Destination.constr_from_ptr(ret);
64                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
65                 return ret_hu_conv;
66         }
67
68         /**
69          * Utility method to constructs a new BlindedRoute-variant Destination
70          */
71         public static Destination blinded_route(BlindedRoute a) {
72                 long ret = bindings.Destination_blinded_route(a == null ? 0 : a.ptr);
73                 Reference.reachabilityFence(a);
74                 if (ret >= 0 && ret <= 4096) { return null; }
75                 org.ldk.structs.Destination ret_hu_conv = org.ldk.structs.Destination.constr_from_ptr(ret);
76                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
77                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(a); };
78                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
79                 // an object to pass exclusive ownership to the function being called.
80                 // In most cases, we avoid ret_hu_conv being visible in GC'd languages by cloning the object
81                 // at the FFI layer, creating a new object which Rust can claim ownership of
82                 // However, in some cases (eg here), there is no way to clone an object, and thus
83                 // we actually have to pass full ownership to Rust.
84                 // Thus, after ret_hu_conv call, a is reset to null and is now a dummy object.
85                 a.ptr = 0;;
86                 return ret_hu_conv;
87         }
88
89 }