198 lines
8.0 KiB
Java
198 lines
8.0 KiB
Java
package ProjectElectricLocomotive;
|
||
|
||
import com.sun.nio.sctp.InvalidStreamException;
|
||
|
||
import java.io.*;
|
||
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;
|
||
|
||
/*import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;*/
|
||
|
||
public class LocomotivesGenericStorage {
|
||
|
||
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";
|
||
|
||
final HashMap<String, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotiveStorage;
|
||
|
||
public List<String> Keys() {
|
||
return _locomotiveStorage.keySet().stream().toList();
|
||
}
|
||
|
||
private final int _pictureWidth;
|
||
private final int _pictureHeight;
|
||
|
||
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) {
|
||
_locomotiveStorage = new HashMap<>();
|
||
_pictureWidth = pictureWidth;
|
||
_pictureHeight = pictureHeight;
|
||
}
|
||
|
||
public void AddSet(String name) {
|
||
if (!(_locomotiveStorage.containsKey(name))) {
|
||
_locomotiveStorage.put(name, new LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
|
||
}
|
||
}
|
||
|
||
public void DelSet(String name) {
|
||
if (_locomotiveStorage.keySet().contains(name)) {
|
||
_locomotiveStorage.remove(name);
|
||
}
|
||
}
|
||
|
||
public LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> get(String ind) {
|
||
if (_locomotiveStorage.keySet().contains(ind)) {
|
||
return _locomotiveStorage.get(ind);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public DrawingObjectLocomotive get(String name, int ind) {
|
||
if (_locomotiveStorage.keySet().contains(ind)) {
|
||
return _locomotiveStorage.get(name).GetU(ind);
|
||
}
|
||
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 (DrawingLocomotive elem : _locomotiveStorage.get(key).GetLocomotives())
|
||
data.append(elem != null ? ExtentionDrawingLoco.GetDataForSave(elem, _separatorForObjectSingle) + "\n" : "");
|
||
if (data.isEmpty()) {
|
||
throw new InvalidStreamException("Нет данных для сохранения");
|
||
}
|
||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
||
writer.write(_keywordSingle + System.lineSeparator() + data);
|
||
} catch (Exception ex) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public boolean LoadDataSingle(String filename) throws IOException {
|
||
if (!new File(filename).exists()) {
|
||
throw new FileNotFoundException("Файл не найден");
|
||
}
|
||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||
String s = reader.readLine();
|
||
if (s == null || s.isEmpty())
|
||
throw new NullPointerException("Нет данных для загрузки");
|
||
if (!s.startsWith(_keywordSingle))
|
||
throw new IllegalArgumentException("Неверный формат данных");
|
||
String key = reader.readLine();
|
||
if (key == null || key.isEmpty())
|
||
throw new NullPointerException("Нет данных для загрузки");
|
||
LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> collection;
|
||
if (_locomotiveStorage.containsKey(key)){
|
||
collection = _locomotiveStorage.get(key);
|
||
collection.clear();
|
||
}
|
||
else
|
||
collection = new LocomotiveGenericCollection<>(_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) {
|
||
DrawingLocomotive plane = ExtentionDrawingLoco.CreateDrawingLocomotive(
|
||
elem,
|
||
_separatorForObjectSingle,
|
||
_pictureWidth,
|
||
_pictureHeight
|
||
);
|
||
if (plane == null || collection.AddOverload(plane) == -1)
|
||
throw new NoSuchElementException("Не найден объект");
|
||
}
|
||
_locomotiveStorage.put(key, collection);
|
||
} catch (IOException e) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public boolean SaveData(String filename) {
|
||
var file = new File(filename);
|
||
if (file.exists()) {
|
||
file.delete();
|
||
}
|
||
StringBuilder data = new StringBuilder();
|
||
for (Map.Entry<String, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> record : _locomotiveStorage.entrySet()) {
|
||
StringBuilder records = new StringBuilder();
|
||
for (DrawingLocomotive elem : record.getValue().GetLocomotives()) {
|
||
records.append(elem != null ? ExtentionDrawingLoco.GetDataForSave(elem, _separatorForObject) + _separatorRecords : "");
|
||
}
|
||
data.append(record.getKey()).append(_separatorForKeyValue).append(records).append("\n");
|
||
}
|
||
if (data.isEmpty()) {
|
||
throw new InvalidStreamException("Нет данных для сохранения");
|
||
}
|
||
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 FileNotFoundException {
|
||
var file = new File(filename);
|
||
if (!file.exists()) {
|
||
throw new FileNotFoundException("Файл не найден");
|
||
}
|
||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||
String s = reader.readLine();
|
||
if (s == null || s.isEmpty())
|
||
throw new NullPointerException("Нет данных для загрузки");
|
||
|
||
if (!s.startsWith(_keyword)) {
|
||
throw new IllegalArgumentException("Неверный формат данных");
|
||
}
|
||
_locomotiveStorage.clear();
|
||
s = reader.readLine();
|
||
while (s != null && !s.isEmpty()) {
|
||
String[] record = s.split(_separatorForKeyValue);
|
||
s = reader.readLine();
|
||
if (record.length != 2) {
|
||
continue;
|
||
}
|
||
LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> collection = new LocomotiveGenericCollection<>(_pictureWidth, _pictureHeight);
|
||
String[] set = record[1].split(_separatorRecords);
|
||
List<String> reversedSet = Arrays.asList(set);
|
||
Collections.reverse(reversedSet);
|
||
for (String elem : reversedSet) {
|
||
DrawingLocomotive plane = ExtentionDrawingLoco.CreateDrawingLocomotive(
|
||
elem,
|
||
_separatorForObject,
|
||
_pictureWidth, _pictureHeight
|
||
);
|
||
if (plane == null || collection.AddOverload(plane) == -1)
|
||
throw new NoSuchElementException("Не найден объект");
|
||
}
|
||
_locomotiveStorage.put(record[0], collection);
|
||
}
|
||
} catch (IOException e) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
}
|