Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the way loading of data is handled
- Loading branch information
1 parent
843922a
commit 1cb1a25
Showing
6 changed files
with
152 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 12 additions & 2 deletions
14
src/main/java/com/bgsoftware/superiorskyblock/core/database/loader/DatabaseLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
package com.bgsoftware.superiorskyblock.core.database.loader; | ||
|
||
import com.bgsoftware.superiorskyblock.core.errors.ManagerLoadException; | ||
|
||
public interface DatabaseLoader { | ||
|
||
void loadData(); | ||
void setState(State state) throws ManagerLoadException; | ||
|
||
enum State { | ||
|
||
INITIALIZE, | ||
POST_INITIALIZE, | ||
PRE_LOAD_DATA, | ||
POST_LOAD_DATA, | ||
SHUTDOWN | ||
|
||
void saveData(); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...java/com/bgsoftware/superiorskyblock/core/database/loader/MachineStateDatabaseLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bgsoftware.superiorskyblock.core.database.loader; | ||
|
||
import com.bgsoftware.superiorskyblock.core.errors.ManagerLoadException; | ||
|
||
import java.util.Collections; | ||
import java.util.EnumMap; | ||
import java.util.Map; | ||
|
||
public abstract class MachineStateDatabaseLoader implements DatabaseLoader { | ||
|
||
private final Map<State, StateAction> STATE_MACHINE_HANDLES = Collections.unmodifiableMap(new EnumMap<State, StateAction>(State.class) {{ | ||
put(State.INITIALIZE, MachineStateDatabaseLoader.this::handleInitialize); | ||
put(State.POST_INITIALIZE, MachineStateDatabaseLoader.this::handlePostInitialize); | ||
put(State.PRE_LOAD_DATA, MachineStateDatabaseLoader.this::handlePreLoadData); | ||
put(State.POST_LOAD_DATA, MachineStateDatabaseLoader.this::handlePostLoadData); | ||
put(State.SHUTDOWN, MachineStateDatabaseLoader.this::handleShutdown); | ||
}}); | ||
|
||
@Override | ||
public void setState(State state) throws ManagerLoadException { | ||
StateAction action = STATE_MACHINE_HANDLES.get(state); | ||
if (action != null) { | ||
try { | ||
action.run(); | ||
} catch (Throwable error) { | ||
if (error instanceof ManagerLoadException) | ||
throw error; | ||
|
||
throw new ManagerLoadException(error, ManagerLoadException.ErrorLevel.SERVER_SHUTDOWN); | ||
} | ||
} | ||
} | ||
|
||
protected abstract void handleInitialize() throws ManagerLoadException; | ||
|
||
protected abstract void handlePostInitialize() throws ManagerLoadException; | ||
|
||
protected abstract void handlePreLoadData() throws ManagerLoadException; | ||
|
||
protected abstract void handlePostLoadData() throws ManagerLoadException; | ||
|
||
protected abstract void handleShutdown() throws ManagerLoadException; | ||
|
||
private interface StateAction { | ||
|
||
void run() throws ManagerLoadException; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters