Skip to content

Commit

Permalink
Resolved issue xspanger3770#275: Playground mode earthquake shows in …
Browse files Browse the repository at this point in the history
…history.
  • Loading branch information
Peter-Kopko committed Dec 5, 2024
1 parent e50f5ef commit 4e7b758
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ public long currentTimeMillis() {
return playgroundStartMillis + (System.currentTimeMillis() - createdAtMillis);
}

/*
@Override
public EarthquakeArchive createArchive() {
return new EarthquakeArchive();
EarthquakeArchive archive = new EarthquakeArchive();
archive.setServesPlayground(isSimulation());
return archive.loadArchive();
}
*/

public Collection<Earthquake> getPlaygroundEarthquakes() {
return playgroundEarthquakes;
Expand All @@ -94,4 +98,5 @@ public String getWatermark() {
public boolean isSimulation() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public static GlobalQuake getInstance() {
}

public EarthquakeArchive createArchive() {
return new EarthquakeArchive().loadArchive();
EarthquakeArchive archive = new EarthquakeArchive();
archive.setServesPlayground(isSimulation());
return archive.loadArchive();
}

public GlobalQuake initStations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,36 @@

public class EarthquakeArchive {

public static final File ARCHIVE_FILE = new File(GlobalQuake.mainFolder, "volume/archive.dat");
public static final File TEMP_ARCHIVE_FILE = new File(GlobalQuake.mainFolder, "volume/temp_archive.dat");
private static final File ARCHIVE_FILE = new File(GlobalQuake.mainFolder, "volume/archive.dat");
private static final File PLAYGROUND_ARCHIVE_FILE = new File(GlobalQuake.mainFolder, "volume/playground_archive.dat");
private static final File TEMP_ARCHIVE_FILE = new File(GlobalQuake.mainFolder, "volume/temp_archive.dat");
private final ExecutorService executor;

private List<ArchivedQuake> archivedQuakes = new MonitorableCopyOnWriteArrayList<>();

private final Map<UUID, ArchivedQuake> uuidArchivedQuakeMap = new ConcurrentHashMap<>();
private boolean servesPlayground = false;

public EarthquakeArchive() {
executor = Executors.newSingleThreadExecutor();
}

private File chooseFile() {
if (servesPlayground) {
return PLAYGROUND_ARCHIVE_FILE;
} else {
return ARCHIVE_FILE;
}
}

@SuppressWarnings("unchecked")
public EarthquakeArchive loadArchive() {
if (!ARCHIVE_FILE.exists()) {
File currentFile = chooseFile();
if (!currentFile.exists()) {
Logger.info("Created new archive");
} else {
try {
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(ARCHIVE_FILE));
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(currentFile));
archivedQuakes = (MonitorableCopyOnWriteArrayList<ArchivedQuake>) oin.readObject();
oin.close();
Logger.info("Loaded " + archivedQuakes.size() + " quakes from archive.");
Expand All @@ -56,13 +67,14 @@ private void buildUUIDMap() {
}

public void saveArchive() {
File currentFile = chooseFile();
if (archivedQuakes != null) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(TEMP_ARCHIVE_FILE));
Logger.info("Saving " + archivedQuakes.size() + " quakes to " + ARCHIVE_FILE.getName());
Logger.info("Saving " + archivedQuakes.size() + " quakes to " + currentFile.getName());
out.writeObject(archivedQuakes);
out.close();
boolean res = (!ARCHIVE_FILE.exists() || ARCHIVE_FILE.delete()) && TEMP_ARCHIVE_FILE.renameTo(ARCHIVE_FILE);
boolean res = (!currentFile.exists() || currentFile.delete()) && TEMP_ARCHIVE_FILE.renameTo(currentFile);
if (!res) {
Logger.error("Unable to save archive!");
} else {
Expand Down Expand Up @@ -141,4 +153,8 @@ public void destroy() {
GlobalQuake.instance.stopService(executor);
}

public void setServesPlayground(boolean isForPlayground) {
servesPlayground = isForPlayground;
}

}

0 comments on commit 4e7b758

Please sign in to comment.