diff --git a/core/src/main/java/org/bitcoinj/core/TransactionConfidence.java b/core/src/main/java/org/bitcoinj/core/TransactionConfidence.java index a1ae187de..23fb611ea 100644 --- a/core/src/main/java/org/bitcoinj/core/TransactionConfidence.java +++ b/core/src/main/java/org/bitcoinj/core/TransactionConfidence.java @@ -19,8 +19,6 @@ package org.bitcoinj.core; import com.google.common.collect.*; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.SettableFuture; import org.bitcoinj.utils.*; import org.bitcoinj.wallet.CoinSelector; import org.bitcoinj.wallet.Wallet; @@ -489,23 +487,23 @@ public class TransactionConfidence { * depth to one will wait until it appears in a block on the best chain, and zero will wait until it has been seen * on the network. */ - public synchronized ListenableFuture getDepthFuture(final int depth, Executor executor) { - final SettableFuture result = SettableFuture.create(); + public synchronized ListenableCompletableFuture getDepthFuture(final int depth, Executor executor) { + final ListenableCompletableFuture result = new ListenableCompletableFuture<>(); if (getDepthInBlocks() >= depth) { - result.set(this); + result.complete(this); } addEventListener(executor, new Listener() { @Override public void onConfidenceChanged(TransactionConfidence confidence, ChangeReason reason) { if (getDepthInBlocks() >= depth) { removeEventListener(this); - result.set(confidence); + result.complete(confidence); } } }); return result; } - public synchronized ListenableFuture getDepthFuture(final int depth) { + public synchronized ListenableCompletableFuture getDepthFuture(final int depth) { return getDepthFuture(depth, Threading.USER_THREAD); } diff --git a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java index 40b2539bf..e59b64097 100644 --- a/core/src/test/java/org/bitcoinj/wallet/WalletTest.java +++ b/core/src/test/java/org/bitcoinj/wallet/WalletTest.java @@ -77,6 +77,7 @@ import java.math.BigInteger; import java.net.InetAddress; import java.security.SecureRandom; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -423,7 +424,7 @@ public class WalletTest extends TestWithWallet { // Send some pending coins to the wallet. Transaction t1 = sendMoneyToWallet(wallet, null, amount, toAddress); Threading.waitForUserCode(); - final ListenableFuture depthFuture = t1.getConfidence().getDepthFuture(1); + final CompletableFuture depthFuture = t1.getConfidence().getDepthFuture(1); assertFalse(depthFuture.isDone()); assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE)); assertEquals(amount, wallet.getBalance(Wallet.BalanceType.ESTIMATED)); diff --git a/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java b/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java index a74323dc3..da66be03f 100644 --- a/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java +++ b/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java @@ -104,19 +104,16 @@ public class ForwardingService { // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. - Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback() { - @Override - public void onSuccess(TransactionConfidence result) { + + tx.getConfidence().getDepthFuture(1).whenComplete((result, t) -> { + if (result != null) { System.out.println("Confirmation received."); forwardCoins(); - } - - @Override - public void onFailure(Throwable t) { + } else { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } - }, MoreExecutors.directExecutor()); + }); }); Address sendToAddress = LegacyAddress.fromKey(params, kit.wallet().currentReceiveKey());