Develope game branch #33

Merged
schrom01 merged 23 commits from develope_Game_branch into main 2022-05-12 13:44:47 +02:00
2 changed files with 23 additions and 7 deletions
Showing only changes of commit 84b5f2464d - Show all commits

View File

@ -81,7 +81,7 @@ public class FileIO {
* @throws IOException File not readable * @throws IOException File not readable
* @throws FileNotFoundException File not found * @throws FileNotFoundException File not found
*/ */
public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException, FileNotFoundException { public synchronized Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException, FileNotFoundException {
if (tournamentFile == null) { if (tournamentFile == null) {
logger.warning("Given tournament file is empty"); logger.warning("Given tournament file is empty");
throw new IllegalArgumentException("Tournament File is null"); throw new IllegalArgumentException("Tournament File is null");
@ -125,7 +125,7 @@ public class FileIO {
* @throws IOException File not readable * @throws IOException File not readable
* @throws FileNotFoundException File not found * @throws FileNotFoundException File not found
*/ */
public void saveTournament(Tournament tournament) throws IOException, FileNotFoundException { public synchronized void saveTournament(Tournament tournament) throws IOException, FileNotFoundException {
if (tournament == null) { if (tournament == null) {
logger.warning("Given tournament file is empty"); logger.warning("Given tournament file is empty");
throw new IllegalArgumentException("Null tournament received"); throw new IllegalArgumentException("Null tournament received");

View File

@ -4,11 +4,14 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TournamentDecorator implements IsObservable{ public class TournamentDecorator implements IsObservable{
private Tournament tournament; private Tournament tournament;
private FileIO fileIO; private FileIO fileIO;
private List<IsObserver> listener = new ArrayList<>(); private List<IsObserver> listener = new ArrayList<>();
private ExecutorService executorService;
public TournamentDecorator(FileIO fileIO){ public TournamentDecorator(FileIO fileIO){
setFileIO(fileIO); setFileIO(fileIO);
@ -16,10 +19,12 @@ public class TournamentDecorator implements IsObservable{
@Override @Override
public void update() { public void update() {
if(tournament != null){ if(tournament != null){
saveTournament(); saveTournament();
} }
} }
}); });
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
} }
public void setFileIO(FileIO fileIO) { public void setFileIO(FileIO fileIO) {
@ -44,14 +49,13 @@ public class TournamentDecorator implements IsObservable{
listener.remove(observer); listener.remove(observer);
} }
public void saveTournament(){ public void saveTournament(){
try { executorService.execute(new saveTask());
fileIO.saveTournament(tournament);
} catch (IOException e) {
e.printStackTrace(); //TODO handle and logging
}
} }
public void createTournament(String name, Tournament.Type type){ public void createTournament(String name, Tournament.Type type){
if(fileIO.tournamentExists(name)){ if(fileIO.tournamentExists(name)){
System.out.println("Tournament with same name exists already."); System.out.println("Tournament with same name exists already.");
@ -133,4 +137,16 @@ public class TournamentDecorator implements IsObservable{
} }
} }
private class saveTask implements Runnable {
@Override
public void run() {
try {
fileIO.saveTournament(tournament);
} catch (IOException e) {
e.printStackTrace(); //TODO handle and logging
}
}
}
} }