From 99bc95a93d14a72402afc62463951401e6998a60 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sun, 5 Mar 2023 13:38:42 +0100 Subject: [PATCH] WalletFiles: migrate `delay` field to `java.time` API --- .../main/java/org/bitcoinj/wallet/Wallet.java | 24 +++++++++++-------- .../java/org/bitcoinj/wallet/WalletFiles.java | 16 ++++++++----- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index 6c620ecc3..d481648ec 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -113,6 +113,7 @@ import java.io.OutputStream; import java.math.BigInteger; import java.math.RoundingMode; import java.nio.ByteBuffer; +import java.time.Duration; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -1600,29 +1601,26 @@ public class Wallet extends BaseTaggableObject * consistency. After connecting to a file, you no longer need to save the wallet manually, it will do it * whenever necessary. Protocol buffer serialization will be used.

* - *

If delayTime is set, a background thread will be created and the wallet will only be saved to - * disk every so many time units. If no changes have occurred for the given time period, nothing will be written. + *

A background thread will be created and the wallet will only be saved to + * disk every periodically. If no changes have occurred for the given time period, nothing will be written. * In this way disk IO can be rate limited. It's a good idea to set this as otherwise the wallet can change very * frequently, eg if there are a lot of transactions in it or during block sync, and there will be a lot of redundant * writes. Note that when a new key is added, that always results in an immediate save regardless of * delayTime. You should still save the wallet manually using {@link Wallet#saveToFile(File)} when your program * is about to shut down as the JVM will not wait for the background thread.

* - *

An event listener can be provided. If a delay greater than 0 was specified, it will be called on a background thread - * with the wallet locked when an auto-save occurs. If delay is zero or you do something that always triggers - * an immediate save, like adding a key, the event listener will be invoked on the calling threads.

+ *

An event listener can be provided. It will be called on a background thread + * with the wallet locked when an auto-save occurs.

* * @param f The destination file to save to. - * @param delayTime How many time units to wait until saving the wallet on a background thread. - * @param timeUnit the unit of measurement for delayTime. + * @param delay How much time to wait until saving the wallet on a background thread. * @param eventListener callback to be informed when the auto-save thread does things, or null */ - public WalletFiles autosaveToFile(File f, long delayTime, TimeUnit timeUnit, - @Nullable WalletFiles.Listener eventListener) { + public WalletFiles autosaveToFile(File f, Duration delay, @Nullable WalletFiles.Listener eventListener) { lock.lock(); try { checkState(vFileManager == null, "Already auto saving this wallet."); - WalletFiles manager = new WalletFiles(this, f, delayTime, timeUnit); + WalletFiles manager = new WalletFiles(this, f, delay); if (eventListener != null) manager.setListener(eventListener); vFileManager = manager; @@ -1632,6 +1630,12 @@ public class Wallet extends BaseTaggableObject } } + /** @deprecated use {@link #autosaveToFile(File, Optional, WalletFiles.Listener)} */ + @Deprecated + public WalletFiles autosaveToFile(File f, long delayTime, TimeUnit timeUnit, @Nullable WalletFiles.Listener eventListener) { + return autosaveToFile(f, Duration.ofMillis(timeUnit.toMillis(delayTime)), eventListener); + } + /** *

* Disables auto-saving, after it had been enabled with diff --git a/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java b/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java index d1652505d..7160414f5 100644 --- a/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java +++ b/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java @@ -50,8 +50,7 @@ public class WalletFiles { private final ScheduledThreadPoolExecutor executor; private final File file; private final AtomicBoolean savePending; - private final long delay; - private final TimeUnit delayTimeUnit; + private final Duration delay; private final Callable saver; private volatile Listener vListener; @@ -77,7 +76,7 @@ public class WalletFiles { * saved automatically. The {@link Wallet} calls {@link #saveNow()} or {@link #saveLater()} as wallet state changes, * depending on the urgency of the changes. */ - public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) { + public WalletFiles(final Wallet wallet, File file, Duration delay) { // An executor that starts up threads when needed and shuts them down later. this.executor = new ScheduledThreadPoolExecutor(1, new ContextPropagatingThreadFactory("Wallet autosave thread", Thread.MIN_PRIORITY)); this.executor.setKeepAliveTime(5, TimeUnit.SECONDS); @@ -87,8 +86,7 @@ public class WalletFiles { // File must only be accessed from the auto-save executor from now on, to avoid simultaneous access. this.file = checkNotNull(file); this.savePending = new AtomicBoolean(); - this.delay = delay; - this.delayTimeUnit = checkNotNull(delayTimeUnit); + this.delay = checkNotNull(delay); this.saver = () -> { // Runs in an auto save thread. @@ -106,6 +104,12 @@ public class WalletFiles { }; } + /** @deprecated use {@link #WalletFiles(Wallet, File, Duration)} */ + @Deprecated + public WalletFiles(final Wallet wallet, File file, long delayTime, TimeUnit timeUnit) { + this(wallet, file, Duration.ofMillis(timeUnit.toMillis(delayTime))); + } + /** Get the {@link Wallet} this {@link WalletFiles} is managing. */ public Wallet getWallet() { return wallet; @@ -151,7 +155,7 @@ public class WalletFiles { public void saveLater() { if (executor.isShutdown() || savePending.getAndSet(true)) return; // Already pending. - executor.schedule(saver, delay, delayTimeUnit); + executor.schedule(saver, delay.toMillis(), TimeUnit.MILLISECONDS); } /** Shut down auto-saving. */