package ProjectElectricLocomotive; 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; 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> _locomotiveStorage; public List 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(_pictureWidth, _pictureHeight)); } } public void DelSet(String name) { if (_locomotiveStorage.keySet().contains(name)) { _locomotiveStorage.remove(name); } } public LocomotiveGenericCollection 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()) { 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; LocomotiveGenericCollection collection; if (_locomotiveStorage.containsKey(key)){ collection = _locomotiveStorage.get(key); collection.clear(); } else collection = new LocomotiveGenericCollection<>(_pictureWidth, _pictureHeight); List 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) return false; } _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> 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()) { return false; } 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) { var file = new File(filename); if (!file.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(_keyword)) { return false; } _locomotiveStorage.clear(); s = reader.readLine(); while (s != null && !s.isEmpty()) { String[] record = s.split(_separatorForKeyValue); s = reader.readLine(); if (record.length != 2) { continue; } LocomotiveGenericCollection collection = new LocomotiveGenericCollection<>(_pictureWidth, _pictureHeight); String[] set = record[1].split(_separatorRecords); List 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) return false; } _locomotiveStorage.put(record[0], collection); } } catch (IOException e) { return false; } return true; } }