package org.prevayler.implementation;
import java.io.*;
import java.text.*;
class NumberFileCreator {
public static final String SNAPSHOT_SUFFIX = "snapshot";
public static final DecimalFormat SNAPSHOT_FORMAT = new DecimalFormat("000000000000000000000'.'" + SNAPSHOT_SUFFIX); public static final DecimalFormat LOG_FORMAT = new DecimalFormat("000000000000000000000'.'commandLog");
private File directory;
private long nextFileNumber;
public NumberFileCreator(File directory, long firstFileNumber) {
this.directory = directory;
this.nextFileNumber = firstFileNumber;
}
File newLog() throws IOException {
File log = new File(directory, LOG_FORMAT.format(nextFileNumber));
if(!log.createNewFile()) throw new IOException("Attempt to create command log file that already existed: " + log);;
++nextFileNumber;
return log;
}
File newSnapshot() throws IOException {
File snapshot = new File(directory, SNAPSHOT_FORMAT.format(nextFileNumber - 1));
snapshot.delete(); return snapshot;
}
File newTempSnapshot() throws IOException {
return File.createTempFile("temp","generatingSnapshot",directory);
}
}