PIbd-23_Minhasapov_R.H._Exc.../MapsCollection.java

160 lines
5.0 KiB
Java
Raw Permalink Normal View History

import java.io.*;
import java.util.HashMap;
import java.util.Set;
public class MapsCollection {
private final HashMap<String, MapWithSetTracktorGeneric<IDrawningObject, 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 MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, map));
}
}
public void deleteMap(String name) {
_mapsStorage.remove(name);
}
public MapWithSetTracktorGeneric<IDrawningObject, AbstractMap> getMap(String name) {
return _mapsStorage.getOrDefault(name, 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 "DumpMap" -> new DumpMap();
default -> null;
};
_mapsStorage.put(elements[0], new MapWithSetTracktorGeneric<>(_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();
MapWithSetTracktorGeneric<IDrawningObject, 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 tracktor : map._setTracktor.getTracktors()) {
writer.println(tracktor.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();
MapWithSetTracktorGeneric<IDrawningObject, AbstractMap> map;
if (_mapsStorage.containsKey(mapName)) {
map = _mapsStorage.get(mapName);
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
return false;
}
map._setTracktor.clear();
} else {
map = switch (reader.readLine()) {
case "SimpleMap" -> new MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap());
case "DumpMap" -> new MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, new DumpMap());
default -> null;
};
_mapsStorage.put(mapName, map);
}
while ((currentLine = reader.readLine()) != null) {
map._setTracktor.insert(DrawningObjectExcavator.create(currentLine));
}
}
return true;
}
}