92378e08b69f8a06f9ba43cff6c1df2f1f0efc2c
[ldk-java] / src / main / java / org / ldk / structs / CustomMessageReader.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  * Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
12  * decoders.
13  */
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class CustomMessageReader extends CommonBase {
16         final bindings.LDKCustomMessageReader bindings_instance;
17         CustomMessageReader(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
18         private CustomMessageReader(bindings.LDKCustomMessageReader arg) {
19                 super(bindings.LDKCustomMessageReader_new(arg));
20                 this.ptrs_to.add(arg);
21                 this.bindings_instance = arg;
22         }
23         @Override @SuppressWarnings("deprecation")
24         protected void finalize() throws Throwable {
25                 if (ptr != 0) { bindings.CustomMessageReader_free(ptr); } super.finalize();
26         }
27
28         public static interface CustomMessageReaderInterface {
29                 /**
30                  * Decodes a custom message to `CustomMessageType`. If the given message type is known to the
31                  * implementation and the message could be decoded, must return `Ok(Some(message))`. If the
32                  * message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
33                  * occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
34                  */
35                 Result_COption_TypeZDecodeErrorZ read(short message_type, byte[] buffer);
36         }
37         private static class LDKCustomMessageReaderHolder { CustomMessageReader held; }
38         public static CustomMessageReader new_impl(CustomMessageReaderInterface arg) {
39                 final LDKCustomMessageReaderHolder impl_holder = new LDKCustomMessageReaderHolder();
40                 impl_holder.held = new CustomMessageReader(new bindings.LDKCustomMessageReader() {
41                         @Override public long read(short message_type, byte[] buffer) {
42                                 Result_COption_TypeZDecodeErrorZ ret = arg.read(message_type, buffer);
43                                 long result = ret == null ? 0 : ret.clone_ptr();
44                                 return result;
45                         }
46                 });
47                 return impl_holder.held;
48         }
49         /**
50          * Decodes a custom message to `CustomMessageType`. If the given message type is known to the
51          * implementation and the message could be decoded, must return `Ok(Some(message))`. If the
52          * message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
53          * occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
54          */
55         public Result_COption_TypeZDecodeErrorZ read(short message_type, byte[] buffer) {
56                 long ret = bindings.CustomMessageReader_read(this.ptr, message_type, buffer);
57                 Reference.reachabilityFence(this);
58                 Reference.reachabilityFence(message_type);
59                 Reference.reachabilityFence(buffer);
60                 if (ret >= 0 && ret <= 4096) { return null; }
61                 Result_COption_TypeZDecodeErrorZ ret_hu_conv = Result_COption_TypeZDecodeErrorZ.constr_from_ptr(ret);
62                 return ret_hu_conv;
63         }
64
65 }