PIbd-23_Polevoy_S.V._SelfPr.../MapsCollection.java

170 lines
5.3 KiB
Java

import java.io.*;
import java.util.HashMap;
import java.util.Set;
public class MapsCollection {
private final HashMap<String, MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>> _mapsStorage;
private final int _pictureWidth;
private final int _pictureHeight;
private final char separatorDict = '|';
private final char separatorData = ';';
public Set<String> getKeys() {
return _mapsStorage.keySet();
}
public MapsCollection(int pictureWidth, int pictureHeight) {
_mapsStorage = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void addMap(String name, AbstractMap map) {
if (!_mapsStorage.containsKey(name)) {
_mapsStorage.put(name, new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map));
}
}
public void deleteMap(String name) {
_mapsStorage.remove(name);
}
public MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> getMap(String name) {
return _mapsStorage.getOrDefault(name, null);
}
public IDrawingObject getArtillery(String mapName, int index) {
var map = _mapsStorage.getOrDefault(mapName, null);
if (map != null) {
return map._setArtilleries.get(index);
}
return null;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public boolean saveData(String filename) throws IOException {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
file.createNewFile();
try (PrintWriter writer = new PrintWriter(file)) {
writer.println("MapsCollection");
for (var storage : _mapsStorage.entrySet()) {
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
}
}
return true;
}
public boolean loadData(String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
return false;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String currentLine = reader.readLine();
if (currentLine == null || !currentLine.contains("MapsCollection")) {
return false;
}
_mapsStorage.clear();
while ((currentLine = reader.readLine()) != null) {
var elements = currentLine.split(String.format("\\%c", separatorDict));
AbstractMap map = switch (elements[1]) {
case "SimpleMap" -> new SimpleMap();
case "ForestMap" -> new ForestMap();
default -> null;
};
_mapsStorage.put(elements[0], new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map));
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?"));
}
}
return true;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public boolean saveMap(String mapName, String filename) throws IOException {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
file.createNewFile();
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map = _mapsStorage.getOrDefault(mapName, null);
if (map == null) {
return false;
}
try (PrintWriter writer = new PrintWriter(file)) {
writer.println("Map");
writer.println(mapName);
writer.println(map.getMap().getClass().getSimpleName());
for (var artillery : map._setArtilleries.getArtilleries()) {
writer.println(artillery.getInfo());
}
}
return true;
}
public boolean loadMap(String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
return false;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String currentLine = reader.readLine();
if (currentLine == null || !currentLine.contains("Map")) {
return false;
}
String mapName = reader.readLine();
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map;
if (_mapsStorage.containsKey(mapName)) {
map = _mapsStorage.get(mapName);
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
return false;
}
} else {
map = switch (reader.readLine()) {
case "SimpleMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap());
case "ForestMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new ForestMap());
default -> null;
};
}
map._setArtilleries.clear();
while ((currentLine = reader.readLine()) != null) {
map._setArtilleries.insert(DrawingObjectArtillery.create(currentLine));
}
_mapsStorage.put(mapName, map);
}
return true;
}
}