diff --git a/ProjectStormtrooper/PlanesGenericStorage.java b/ProjectStormtrooper/PlanesGenericStorage.java index a86d59d..bed1591 100644 --- a/ProjectStormtrooper/PlanesGenericStorage.java +++ b/ProjectStormtrooper/PlanesGenericStorage.java @@ -1,11 +1,19 @@ package ProjectStormtrooper; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +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> _planeStorages; + private static final String _separatorForKeyValue = "|"; + private static final String _separatorRecords = ";"; + private static final String _separatorForObject = ":"; + private static final String _keyword = "PlanesStorage"; public List Keys() { return _planeStorages.keySet().stream().toList(); @@ -33,10 +41,77 @@ public class PlanesGenericStorage { 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 SaveData(String filename) { + var file = new File(filename); + if (file.exists()) { + file.delete(); + } + StringBuilder data = new StringBuilder(); + for (Map.Entry> 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()) { + 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; + } + _planeStorages.clear(); + s = reader.readLine(); + while (s != null && !s.isEmpty()) { + String[] record = s.split(_separatorForKeyValue); + s = reader.readLine(); + if (record.length != 2) { + continue; + } + PlanesGenericCollection collection = new PlanesGenericCollection<>(_pictureWidth, _pictureHeight); + String[] set = record[1].split(_separatorRecords); + List reversedSet = Arrays.asList(set); + Collections.reverse(reversedSet); + for (String elem : reversedSet) { + DrawingPlane train = ExtensionDrawingPlane.CreateDrawingPlane( + elem, + _separatorForObject, + _pictureWidth, _pictureHeight + ); + if (train == null || collection.Add(train) == -1) + return false; + } + _planeStorages.put(record[0], collection); + } + } catch (IOException e) { + return false; + } + return true; + } }