Program: 85934
Project: 85934
Buggy source:
/** 
 * <p>Sets up the wallet to auto-save itself to the given file, using temp files with atomic renames to ensure 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.</p> <p>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. 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 a busy key, 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.</p> <p>An event listener can be provided. If a delay >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. There is an important detail to get right here. The background thread that performs auto-saving keeps a weak reference to the wallet and shuts itself down if the wallet is garbage collected, so you don't have to think about it. If you provide an event listener however, it'd be very easy to accidentally hold a strong reference from your event listener to the wallet, meaning that the background thread will transitively keep your wallet object alive and un-collectable. So be careful to use a static inner class for this to avoid that problem, unless you don't care about keeping wallets alive indefinitely.</p>
 * @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 eventListener callback to be informed when the auto-save thread does things, or null
 * @throws IOException
 */
public synchronized void autosaveToFile(File f,long delayTime,TimeUnit timeUnit,AutosaveEventListener eventListener){
  Preconditions.checkArgument(delayTime >= 0);
  autosaveToFile=Preconditions.checkNotNull(f);
  if (delayTime > 0) {
    autosaveEventListener=eventListener;
    autosaveThread=new AutosaveThread(this,TimeUnit.MILLISECONDS.convert(delayTime,timeUnit));
  }
}

Fixed version (ground truth):
/** 
 * <p>Sets up the wallet to auto-save itself to the given file, using temp files with atomic renames to ensure 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.</p> <p>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. 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 a busy key, 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.</p> <p>An event listener can be provided. If a delay >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. There is an important detail to get right here. The background thread that performs auto-saving keeps a weak reference to the wallet and shuts itself down if the wallet is garbage collected, so you don't have to think about it. If you provide an event listener however, it'd be very easy to accidentally hold a strong reference from your event listener to the wallet, meaning that the background thread will transitively keep your wallet object alive and un-collectable. So be careful to use a static inner class for this to avoid that problem, unless you don't care about keeping wallets alive indefinitely.</p>
 * @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 eventListener callback to be informed when the auto-save thread does things, or null
 * @throws IOException
 */
public synchronized void autosaveToFile(File f,long delayTime,TimeUnit timeUnit,AutosaveEventListener eventListener){
  Preconditions.checkArgument(delayTime >= 0);
  autosaveToFile=Preconditions.checkNotNull(f);
  if (delayTime > 0) {
    autosaveEventListener=eventListener;
    autosaveDelayMs=TimeUnit.MILLISECONDS.convert(delayTime,timeUnit);
  }
}