diff --git a/examples/src/main/java/org/bitcoinj/examples/DoubleSpend.java b/examples/src/main/java/org/bitcoinj/examples/DoubleSpend.java index 335558977..4ec94995f 100644 --- a/examples/src/main/java/org/bitcoinj/examples/DoubleSpend.java +++ b/examples/src/main/java/org/bitcoinj/examples/DoubleSpend.java @@ -50,13 +50,10 @@ public class DoubleSpend { Transaction tx2 = kit.wallet().createSend(LegacyAddress.fromBase58(params, "muYPFNCv7KQEG2ZLM7Z3y96kJnNyXJ53wm"), CENT.add(SATOSHI.multiply(10))); final Peer peer = kit.peerGroup().getConnectedPeers().get(0); peer.addPreMessageReceivedEventListener(Threading.SAME_THREAD, - new PreMessageReceivedEventListener() { - @Override - public Message onPreMessageReceived(Peer peer, Message m) { + (peer1, m) -> { System.err.println("Got a message!" + m.getClass().getSimpleName() + ": " + m); return m; } - } ); peer.sendMessage(tx1); peer.sendMessage(tx2); diff --git a/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java b/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java index 3bb45aca5..ca1ae409f 100644 --- a/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java +++ b/examples/src/main/java/org/bitcoinj/examples/ForwardingService.java @@ -91,35 +91,32 @@ public class ForwardingService { kit.awaitRunning(); // We want to know when we receive money. - kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() { - @Override - public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { - // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this). - // - // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). - Coin value = tx.getValueSentToMe(w); - System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx); - System.out.println("Transaction will be forwarded after it confirms."); - // Wait until it's made it into the block chain (may run immediately if it's already there). - // - // For this dummy app of course, we could just forward the unconfirmed transaction. If it were - // 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) { - System.out.println("Confirmation received."); - forwardCoins(tx); - } + kit.wallet().addCoinsReceivedEventListener((w, tx, prevBalance, newBalance) -> { + // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this). + // + // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). + Coin value = tx.getValueSentToMe(w); + System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx); + System.out.println("Transaction will be forwarded after it confirms."); + // Wait until it's made it into the block chain (may run immediately if it's already there). + // + // For this dummy app of course, we could just forward the unconfirmed transaction. If it were + // 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) { + System.out.println("Confirmation received."); + forwardCoins(tx); + } - @Override - public void onFailure(Throwable t) { - // This kind of future can't fail, just rethrow in case something weird happens. - throw new RuntimeException(t); - } - }, MoreExecutors.directExecutor()); - } + @Override + public void onFailure(Throwable t) { + // 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()); @@ -141,12 +138,9 @@ public class ForwardingService { // Register a callback that is invoked when the transaction has propagated across the network. // This shows a second style of registering ListenableFuture callbacks, it works when you don't // need access to the object the future returns. - sendResult.broadcastComplete.addListener(new Runnable() { - @Override - public void run() { - // The wallet has changed now, it'll get auto saved shortly or when the app shuts down. - System.out.println("Sent coins onwards! Transaction hash is " + sendResult.tx.getTxId()); - } + sendResult.broadcastComplete.addListener(() -> { + // The wallet has changed now, it'll get auto saved shortly or when the app shuts down. + System.out.println("Sent coins onwards! Transaction hash is " + sendResult.tx.getTxId()); }, MoreExecutors.directExecutor()); } catch (KeyCrypterException | InsufficientMoneyException e) { // We don't use encrypted wallets in this example - can never happen. diff --git a/examples/src/main/java/org/bitcoinj/examples/Kit.java b/examples/src/main/java/org/bitcoinj/examples/Kit.java index 12a5473a4..d978e744d 100644 --- a/examples/src/main/java/org/bitcoinj/examples/Kit.java +++ b/examples/src/main/java/org/bitcoinj/examples/Kit.java @@ -65,42 +65,21 @@ public class Kit { kit.startAsync(); kit.awaitRunning(); - kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() { - @Override - public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) { - System.out.println("-----> coins resceived: " + tx.getTxId()); - System.out.println("received: " + tx.getValue(wallet)); - } + kit.wallet().addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> { + System.out.println("-----> coins resceived: " + tx.getTxId()); + System.out.println("received: " + tx.getValue(wallet)); }); - kit.wallet().addCoinsSentEventListener(new WalletCoinsSentEventListener() { - @Override - public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) { - System.out.println("coins sent"); - } - }); + kit.wallet().addCoinsSentEventListener((wallet, tx, prevBalance, newBalance) -> System.out.println("coins sent")); - kit.wallet().addKeyChainEventListener(new KeyChainEventListener() { - @Override - public void onKeysAdded(List keys) { - System.out.println("new key added"); - } - }); + kit.wallet().addKeyChainEventListener(keys -> System.out.println("new key added")); - kit.wallet().addScriptsChangeEventListener(new ScriptsChangeEventListener() { - @Override - public void onScriptsChanged(Wallet wallet, List