DataManeger:

- DataManeger is static global var in Makkah.
    - Save states in data/ dir.
    - Clear all bin files with constructor.
This commit is contained in:
HeshamTB 2020-12-16 16:26:40 +03:00
parent 9a610b65e6
commit ab3acdec9b
2 changed files with 17 additions and 12 deletions

View File

@ -9,20 +9,22 @@ public class DataManeger {
private File workingDir; private File workingDir;
public DataManeger(){ public DataManeger(){
workingDir = new File("."); workingDir = new File("./data/");
workingDir.mkdir();
clearData();
} }
public boolean stateAvailable(Date time) { public boolean stateAvailable(Date time) {
File f = new File(String.format("0x%016X.bin", time.getTime())); File f = new File(String.format("./data/%s.bin", time.getTime()));
return f.exists(); return f.exists();
} }
public State loadState(Date time){ public State loadState(Date time){
State state = null; State state = null;
if (stateAvailable(time)){ if (stateAvailable(time)){
try { try {
ObjectInputStream objectInputStream = new ObjectInputStream( ObjectInputStream objectInputStream = new ObjectInputStream(
new FileInputStream(String.format("%016X.bin", time.getTime()))); new FileInputStream(String.format("./data/%s.bin", time.getTime())));
state = (State)objectInputStream.readObject(); state = (State)objectInputStream.readObject();
objectInputStream.close(); objectInputStream.close();
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {
@ -34,7 +36,7 @@ public class DataManeger {
public boolean saveState(State state, Date time){ public boolean saveState(State state, Date time){
try { try {
FileOutputStream fs = new FileOutputStream(String.format("%016X.bin", time.getTime())); FileOutputStream fs = new FileOutputStream(String.format("./data/%s.bin", time.getTime()));
BufferedOutputStream bfs = new BufferedOutputStream(fs); BufferedOutputStream bfs = new BufferedOutputStream(fs);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bfs); ObjectOutputStream objectOutputStream = new ObjectOutputStream(bfs);
objectOutputStream.writeObject(state); objectOutputStream.writeObject(state);
@ -57,7 +59,7 @@ public class DataManeger {
if (file.getName().contains(".bin")){ if (file.getName().contains(".bin")){
String timeInName = file.getName().replaceAll(".bin", "").trim(); String timeInName = file.getName().replaceAll(".bin", "").trim();
if ("".equals(timeInName)) continue; if ("".equals(timeInName)) continue;
long time = Long.parseLong(timeInName, 16); long time = Long.parseLong(timeInName);
timesList.add(new HijriDate(time)); timesList.add(new HijriDate(time));
} }
} }
@ -65,6 +67,14 @@ public class DataManeger {
times = timesList.toArray(times); times = timesList.toArray(times);
return times; return times;
} }
private void clearData() {
for (File file : savedStateFiles()) {
if (file.getName().contains(".bin")){
file.delete();
}
}
}
} }
class State implements Serializable { class State implements Serializable {

View File

@ -31,6 +31,7 @@ public class MakkahCity {
private static final InputListener inputListener = new InputListener(); private static final InputListener inputListener = new InputListener();
private static final Thread t = new Thread(inputListener,"InputThread-Makkah"); private static final Thread t = new Thread(inputListener,"InputThread-Makkah");
private static boolean isAllRoutSet; private static boolean isAllRoutSet;
private static final DataManeger dataManeger = new DataManeger();
//GUI //GUI
private static boolean exit_flag; private static boolean exit_flag;
private static boolean pause_flag; private static boolean pause_flag;
@ -1162,7 +1163,6 @@ public class MakkahCity {
stdStreet, stdStreet,
allArrivedToArafatTime, allArrivedToArafatTime,
allArrivedToHotelsTime); allArrivedToHotelsTime);
DataManeger dataManeger = new DataManeger();
dataManeger.saveState(s, currenttimeManager.getCurrentTime()); dataManeger.saveState(s, currenttimeManager.getCurrentTime());
System.out.println(Arrays.toString(dataManeger.savedStatesTimes())); //TODO FOR DEBUG REMOVE System.out.println(Arrays.toString(dataManeger.savedStatesTimes())); //TODO FOR DEBUG REMOVE
@ -1170,11 +1170,6 @@ public class MakkahCity {
if (!result) System.out.println("Could not save state "+currenttimeManager.getCurrentTime().getTime()); if (!result) System.out.println("Could not save state "+currenttimeManager.getCurrentTime().getTime());
} }
private static State loadState(Date time){
DataManeger dataManeger = new DataManeger();
return dataManeger.loadState(time);
}
private static void updateStreetFrame() { private static void updateStreetFrame() {
Object[][] streetData = new Object[stdStreet.length][6]; Object[][] streetData = new Object[stdStreet.length][6];
for (int i = 0; i < stdStreet.length; i++) { for (int i = 0; i < stdStreet.length; i++) {