Skip to content

Instantly share code, notes, and snippets.

@nicolas-raoul
Created February 14, 2017 11:11
Show Gist options
  • Save nicolas-raoul/029b52c6092ffc657e23d91432c6a8eb to your computer and use it in GitHub Desktop.
Save nicolas-raoul/029b52c6092ffc657e23d91432c6a8eb to your computer and use it in GitHub Desktop.
Java implementation of a journal (tasks journalization)
package fr.free.nrw.journal;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import org.apache.log4j.Logger;
/**
* Journal that keeps track of what tasks have been done, and which ones are still to do.
* Records tasks that have been been performed, so that they are not performed again if the program must be relaunched, for instance due to a crash.
*
* The tasks must always be the same, and occur in the same order. If tasks change, you must delete the journal file before running the app again.
*
* Terminology:
* - Task: Something to do. Each task is written as an item to the journal.
* - Previous run: The previous time the app was run. The journal matches tasks of the previous and current runs.
*/
public class Journal {
/**
* State
* 0: Reading from last run's journal
* 1: Adding to the journal
*/
private enum State {
READING,
WRITING
}
private State state = State.READING;
final static Logger log = Logger.getLogger(Journal.class);
private String journalFile;
PrintWriter journalWriter;
BufferedReader journalReader;
public Journal(String journalFile) {
this.journalFile = journalFile;
try {
journalReader = new BufferedReader(new FileReader(journalFile));
}
catch(Exception e) {
log.info("Journal file does not exist, creating a new one.");
switchToWritingState();
}
}
private void switchToWritingState() {
state = State.WRITING;
try {
FileWriter fw = new FileWriter(journalFile, true);
BufferedWriter bw = new BufferedWriter(fw);
journalWriter = new PrintWriter(bw);
}
catch(Exception e) {
log.error("Journal error: Opening " + journalFile + " for writing failed.", e);
}
}
public void markAsDone(String task) {
if (state == State.WRITING) {
journalWriter.println(task);
journalWriter.flush(); // Otherwise the line is not always written, even in cases where no error happen.
}
else {
log.error("Journal error: Tried to write task \"" + task + "\" to journal before reaching end of last time's journal. If data has changed, you must delete last time's journal.");
}
}
public boolean isDoneAlready(String task) {
if (state == State.READING) {
String lastRunTask = null;
try {
lastRunTask = journalReader.readLine();
} catch (Exception e) {
log.error("Journal error: Could not read journal when trying to match current task: " + task, e);
e.printStackTrace();
return false;
} // Task of the previous run.
if(lastRunTask == null) {
// End of the journal file reached.
switchToWritingState();
return false;
}
if (lastRunTask.equals(task)) {
return true;
}
else {
log.error("Journal error: Task \"" + task + "\" does not match task in journal: " + lastRunTask);
return false;
}
}
else {
return false; // We are in writing mode, which means there are only new tasks from now on.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment