186 lines
7.2 KiB
Java
186 lines
7.2 KiB
Java
package ProjectStormtrooper;
|
|
|
|
import java.util.*;
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
|
|
public class PlanesGenericStorage {
|
|
final HashMap<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _planeStorages;
|
|
private static final String _separatorForKeyValue = "@";
|
|
private static final String _separatorRecords = ";";
|
|
private static final String _separatorForObject = ":";
|
|
private static final String _separatorForObjectSingle = "::";
|
|
private static final String _keyword = "PlanesStorage";
|
|
private static final String _keywordSingle = "PlanesStorageSingle";
|
|
|
|
public List<String> Keys() {
|
|
return _planeStorages.keySet().stream().toList();
|
|
}
|
|
|
|
private final int _pictureWidth;
|
|
private final int _pictureHeight;
|
|
|
|
public PlanesGenericStorage(int pictureWidth, int pictureHeight) {
|
|
_planeStorages = new HashMap<>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
|
|
public void AddSet(String name) {
|
|
_planeStorages.put(name, new PlanesGenericCollection<>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
public void DelSet(String name) {
|
|
_planeStorages.remove(name);
|
|
}
|
|
|
|
public PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> Get(String ind) {
|
|
if (_planeStorages.containsKey(ind))
|
|
return _planeStorages.get(ind);
|
|
return null;
|
|
}
|
|
|
|
public DrawingObjectPlane GetByDoubleParameter(String storageName, int planeIndex) {
|
|
if (_planeStorages.containsKey(storageName)) {
|
|
return _planeStorages.get(storageName).GetU(planeIndex);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public boolean SaveDataSingle(String filename, String key) {
|
|
var file = new File(filename);
|
|
if (file.exists()) {
|
|
file.delete();
|
|
}
|
|
StringBuilder data = new StringBuilder();
|
|
data.append(key).append("\n");
|
|
for (DrawingPlane elem : _planeStorages.get(key).GetPlanes())
|
|
data.append(elem != null ? ExtensionDrawingPlane.GetDataForSave(elem, _separatorForObjectSingle) + "\n" : "");
|
|
if (data.isEmpty()) {
|
|
return false;
|
|
}
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
|
writer.write(_keywordSingle + System.lineSeparator() + data);
|
|
} catch (IOException e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean LoadDataSingle(String filename) {
|
|
if (!new File(filename).exists()) {
|
|
return false;
|
|
}
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
|
String s = reader.readLine();
|
|
if (s == null || s.isEmpty())
|
|
return false;
|
|
if (!s.startsWith(_keywordSingle))
|
|
return false;
|
|
String key = reader.readLine();
|
|
if (key == null || key.isEmpty())
|
|
return false;
|
|
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> collection;
|
|
if (_planeStorages.containsKey(key)) {
|
|
collection = _planeStorages.get(key);
|
|
collection.clear();
|
|
} else
|
|
collection = new PlanesGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
List<String> plainsStrings = new ArrayList<>();
|
|
s = reader.readLine();
|
|
while (s != null && !s.isEmpty()) {
|
|
plainsStrings.add(s);
|
|
s = reader.readLine();
|
|
}
|
|
Collections.reverse(plainsStrings);
|
|
for (String elem : plainsStrings) {
|
|
DrawingPlane plane = ExtensionDrawingPlane.CreateDrawingPlane(
|
|
elem,
|
|
_separatorForObjectSingle,
|
|
_pictureWidth,
|
|
_pictureHeight
|
|
);
|
|
if (plane == null || collection.Add(plane) == -1)
|
|
return false;
|
|
}
|
|
_planeStorages.put(key, collection);
|
|
} catch (IOException e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean SaveData(String filename) throws Exception {
|
|
var file = new File(filename);
|
|
if (file.exists()) {
|
|
file.delete();
|
|
}
|
|
StringBuilder data = new StringBuilder();
|
|
for (Map.Entry<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> record : _planeStorages.entrySet()) {
|
|
StringBuilder records = new StringBuilder();
|
|
for (DrawingPlane elem : record.getValue().GetPlanes()) {
|
|
records.append(elem != null ? ExtensionDrawingPlane.GetDataForSave(elem, _separatorForObject) + _separatorRecords : "");
|
|
}
|
|
data.append(record.getKey()).append(_separatorForKeyValue).append(records).append("\n");
|
|
}
|
|
if (data.isEmpty()) {
|
|
throw new Exception("Невалидная операция, нет данных для сохранения");
|
|
}
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
|
writer.write(_keyword + System.lineSeparator() + data);
|
|
} catch (IOException e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean LoadData(String filename) throws Exception {
|
|
var file = new File(filename);
|
|
if (!file.exists()) {
|
|
throw new Exception("Файл не найден");
|
|
}
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
|
String s = reader.readLine();
|
|
if (s == null || s.isEmpty())
|
|
throw new Exception("Нет данных для загрузки");
|
|
|
|
if (!s.startsWith(_keyword)) {
|
|
throw new Exception("Неверный формат данных");
|
|
}
|
|
_planeStorages.clear();
|
|
s = reader.readLine();
|
|
while (s != null && !s.isEmpty()) {
|
|
String[] record = s.split(_separatorForKeyValue);
|
|
s = reader.readLine();
|
|
if (record.length != 2) {
|
|
continue;
|
|
}
|
|
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> collection = new PlanesGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
String[] set = record[1].split(_separatorRecords);
|
|
List<String> reversedSet = Arrays.asList(set);
|
|
Collections.reverse(reversedSet);
|
|
for (String elem : reversedSet) {
|
|
DrawingPlane plane = ExtensionDrawingPlane.CreateDrawingPlane(
|
|
elem,
|
|
_separatorForObject,
|
|
_pictureWidth, _pictureHeight
|
|
);
|
|
if (plane != null) {
|
|
if (collection.Add(plane) == -1) {
|
|
throw new Exception("Ошибка добавления в коллекцию");
|
|
}
|
|
}
|
|
}
|
|
_planeStorages.put(record[0], collection);
|
|
}
|
|
} catch (IOException e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|