7716950af939df3dac28f489a0cdce1dbc47cf12
[ldk-java] / src / main / java / org / ldk / structs / LockableScore.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  * A scorer that is accessed under a lock.
12  * 
13  * Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
14  * having shared ownership of a scorer but without requiring internal locking in [`Score`]
15  * implementations. Internal locking would be detrimental to route finding performance and could
16  * result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
17  * 
18  * [`find_route`]: crate::routing::router::find_route
19  */
20 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
21 public class LockableScore extends CommonBase {
22         final bindings.LDKLockableScore bindings_instance;
23         LockableScore(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
24         private LockableScore(bindings.LDKLockableScore arg) {
25                 super(bindings.LDKLockableScore_new(arg));
26                 this.ptrs_to.add(arg);
27                 this.bindings_instance = arg;
28         }
29         @Override @SuppressWarnings("deprecation")
30         protected void finalize() throws Throwable {
31                 if (ptr != 0) { bindings.LockableScore_free(ptr); } super.finalize();
32         }
33
34         public static interface LockableScoreInterface {
35                 /**
36                  * Returns the locked scorer.
37                  */
38                 Score lock();
39         }
40         private static class LDKLockableScoreHolder { LockableScore held; }
41         public static LockableScore new_impl(LockableScoreInterface arg) {
42                 final LDKLockableScoreHolder impl_holder = new LDKLockableScoreHolder();
43                 impl_holder.held = new LockableScore(new bindings.LDKLockableScore() {
44                         @Override public long lock() {
45                                 Score ret = arg.lock();
46                                 long result = ret == null ? 0 : ret.ptr;
47                                 impl_holder.held.ptrs_to.add(ret);
48                                 return result;
49                         }
50                 });
51                 return impl_holder.held;
52         }
53         /**
54          * Returns the locked scorer.
55          */
56         public Score lock() {
57                 long ret = bindings.LockableScore_lock(this.ptr);
58                 Reference.reachabilityFence(this);
59                 if (ret >= 0 && ret <= 4096) { return null; }
60                 Score ret_hu_conv = new Score(null, ret);
61                 ret_hu_conv.ptrs_to.add(this);
62                 return ret_hu_conv;
63         }
64
65 }