From c9d411e8e7477c51dbe3f8a5258b22976d97055a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 5 Jun 2013 16:59:52 +0200 Subject: [PATCH] Add two new WalletExtension access methods. --- .../java/com/google/bitcoin/core/Wallet.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index 98a48796..3bf9da52 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -3122,6 +3122,41 @@ public class Wallet implements Serializable, BlockChainListener { if (extensions.containsKey(id)) throw new IllegalStateException("Cannot add two extensions with the same ID: " + id); extensions.put(id, extension); + invokeOnWalletChanged(); + } finally { + lock.unlock(); + } + } + + /** + * Atomically adds extension or returns an existing extension if there is one with the same id alreadypresent. + */ + public WalletExtension addOrGetExistingExtension(WalletExtension extension) { + String id = checkNotNull(extension).getWalletExtensionID(); + lock.lock(); + try { + WalletExtension previousExtension = extensions.get(id); + if (previousExtension != null) + return previousExtension; + extensions.put(id, extension); + invokeOnWalletChanged(); + return extension; + } finally { + lock.unlock(); + } + } + + /** + * Either adds extension as a new extension or replaces the existing extension if one already exists with the same + * id. This also calls onWalletChanged, triggering wallet saving, so may be useful even when called with the same + * extension as is already present. + */ + public void addOrUpdateExtension(WalletExtension extension) { + String id = checkNotNull(extension).getWalletExtensionID(); + lock.lock(); + try { + extensions.put(id, extension); + invokeOnWalletChanged(); } finally { lock.unlock(); }