Added exceptions to MapsCollection
This commit is contained in:
parent
60bc67a8f5
commit
36a610081e
@ -1,4 +1,5 @@
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.file.NoSuchFileException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ public class MapsCollection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public boolean saveData(String filename) throws IOException {
|
public void saveData(String filename) throws IOException {
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
|
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
@ -60,22 +61,20 @@ public class MapsCollection {
|
|||||||
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
|
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean loadData(String filename) throws IOException {
|
public void loadData(String filename) throws IOException {
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
return false;
|
throw new FileNotFoundException("Файл не найден");
|
||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
String currentLine = reader.readLine();
|
String currentLine = reader.readLine();
|
||||||
|
|
||||||
if (currentLine == null || !currentLine.contains("MapsCollection")) {
|
if (currentLine == null || !currentLine.contains("MapsCollection")) {
|
||||||
return false;
|
throw new FileFormatException("Неверный формат файла");
|
||||||
}
|
}
|
||||||
|
|
||||||
_mapsStorage.clear();
|
_mapsStorage.clear();
|
||||||
@ -92,12 +91,10 @@ public class MapsCollection {
|
|||||||
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?"));
|
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
public boolean saveMap(String mapName, String filename) throws IOException {
|
public void saveMap(String mapName, String filename) throws Exception {
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
|
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
@ -109,7 +106,7 @@ public class MapsCollection {
|
|||||||
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map = _mapsStorage.getOrDefault(mapName, null);
|
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map = _mapsStorage.getOrDefault(mapName, null);
|
||||||
|
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
return false;
|
throw new IndexOutOfBoundsException();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (PrintWriter writer = new PrintWriter(file)) {
|
try (PrintWriter writer = new PrintWriter(file)) {
|
||||||
@ -121,22 +118,20 @@ public class MapsCollection {
|
|||||||
writer.println(artillery.getInfo());
|
writer.println(artillery.getInfo());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean loadMap(String filename) throws IOException {
|
public void loadMap(String filename) throws IOException {
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
return false;
|
throw new FileNotFoundException("Файл не найден");
|
||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
String currentLine = reader.readLine();
|
String currentLine = reader.readLine();
|
||||||
|
|
||||||
if (currentLine == null || !currentLine.contains("Map")) {
|
if (currentLine == null || !currentLine.contains("Map")) {
|
||||||
return false;
|
throw new FileFormatException("Неверный формат файла");
|
||||||
}
|
}
|
||||||
|
|
||||||
String mapName = reader.readLine();
|
String mapName = reader.readLine();
|
||||||
@ -145,7 +140,7 @@ public class MapsCollection {
|
|||||||
if (_mapsStorage.containsKey(mapName)) {
|
if (_mapsStorage.containsKey(mapName)) {
|
||||||
map = _mapsStorage.get(mapName);
|
map = _mapsStorage.get(mapName);
|
||||||
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
|
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
|
||||||
return false;
|
throw new FileFormatException("Неверный формат файла");
|
||||||
}
|
}
|
||||||
map._setArtilleries.clear();
|
map._setArtilleries.clear();
|
||||||
} else {
|
} else {
|
||||||
@ -162,7 +157,5 @@ public class MapsCollection {
|
|||||||
|
|
||||||
_mapsStorage.put(mapName, map);
|
_mapsStorage.put(mapName, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user