PIbd-22_Tsukanova_I.V._Airc.../src/MapsCollection.java

171 lines
5.5 KiB
Java

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>> _mapStorages;
public ArrayList<String> Keys() {
return new ArrayList<>(_mapStorages.keySet());
}
private final int _pictureWidth;
private final int _pictureHeight;
private final char separatorDict = '|';
private final char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(String name, AbstractMap map)
{
if (_mapStorages.containsKey(name))
{
return;
}
_mapStorages.put(name, new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> get(String ind){
if (_mapStorages.containsKey(ind))
{
return _mapStorages.get(ind);
}
return null;
}
public DrawingObjectWarship Get(String name, int ind) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetWarshipInList(ind);
}
return null;
}
public boolean SaveData(String filename) {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
try (BufferedWriter br = new BufferedWriter(new FileWriter(filename)))
{
br.write("MapsCollection\n");
for (var storage : _mapStorages.entrySet()) {
br.write(storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict, separatorData) + "\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
public boolean LoadData(String filename)
{
if (!(new File(filename).exists()))
{
return false;
}
try (BufferedReader br = new BufferedReader(new FileReader(filename)))
{
String str = "";
if ((str = br.readLine()) == null || !str.contains("MapsCollection"))
{
return false;
}
//очищаем записи
_mapStorages.clear();
while ((str = br.readLine()) != null)
{
var elem = str.split(String.format("\\%c",separatorDict));
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "LineMap":
map = new LineMap();
break;
}
_mapStorages.put(elem[0], new MapWithSetWarshipsGeneric<>(_pictureWidth, _pictureHeight, map));
_mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData + "\n?"));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
//Сохранение отдельного объекта класса-хранилища
public boolean SaveMap(String filename, String index) {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
var elem = _mapStorages.get(index);
if (elem==null)
return false;
try (BufferedWriter br = new BufferedWriter(new FileWriter(filename)))
{
br.write("MapsCollection\n");
br.write(index + separatorDict);
br.write(elem.GetDataMap(separatorDict, separatorData));
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
//Загрузка отдельного объекта класса-хранилища
public boolean LoadMap(String filename) {
if (!(new File(filename).exists()))
{
return false;
}
try (BufferedReader br = new BufferedReader(new FileReader(filename)))
{
String str = "";
if ((str = br.readLine()) == null || !str.contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
if ((str = br.readLine()) != null)
{
var elem = str.split(String.format("\\%c",separatorDict));
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "LineMap":
map = new LineMap();
break;
}
if(_mapStorages.containsKey(elem[0])){
_mapStorages.get(elem[0]).Clear();
}
else{
_mapStorages.put(elem[0],new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
while((str = br.readLine()) != null) {
_mapStorages.get(elem[0]).LoadData(str.split(separatorData + "\n?"));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
}