[Java] Expose the ProbabilisticScorer from the CMC correctly
[ldk-java] / src / main / java / org / ldk / batteries / ChannelManagerConstructor.java
index 029fac792e0b407be50b8eeeb7ffe625ec4be2d0..db37118dfd71cb10f43dda02d21789d6facad658 100644 (file)
@@ -76,10 +76,32 @@ public class ChannelManagerConstructor {
      * we want to expose underlying details of the scorer itself. Thus, we expose a safe version that takes the lock
      * then returns a reference to this scorer.
      */
-    @Nullable private final ProbabilisticScorer prob_scorer;
+    private final ProbabilisticScorer prob_scorer;
     private final Logger logger;
     private final KeysManager keys_manager;
 
+    /**
+     * Exposes the `ProbabilisticScorer` wrapped inside a lock. Don't forget to `close` this lock when you're done with
+     * it so normal scoring operation can continue.
+     */
+    public class ScorerWrapper implements AutoCloseable {
+        private final Score lock;
+        public final ProbabilisticScorer prob_scorer;
+        private ScorerWrapper(Score lock, ProbabilisticScorer prob_scorer) {
+            this.lock = lock; this.prob_scorer = prob_scorer;
+        }
+        @Override public void close() throws Exception {
+            lock.destroy();
+        }
+    }
+    /**
+     * Gets the `ProbabilisticScorer` which backs the public lockable `scorer`. Don't forget to `close` the lock when
+     * you're done with it.
+     */
+    public ScorerWrapper get_locked_scorer() {
+        return new ScorerWrapper(this.scorer.as_LockableScore().lock(), this.prob_scorer);
+    }
+
     /**
      * Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations
      *