From 585712ebf9c58dc21022df323db5a2a19d3efa60 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 17 Dec 2023 21:37:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20Clear?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PlanesGenericCollection.java | 4 + ProjectStormtrooper/PlanesGenericStorage.java | 74 ++++++++++++++++++- ProjectStormtrooper/SetGeneric.java | 4 + 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/ProjectStormtrooper/PlanesGenericCollection.java b/ProjectStormtrooper/PlanesGenericCollection.java index 116d617..c5858c3 100644 --- a/ProjectStormtrooper/PlanesGenericCollection.java +++ b/ProjectStormtrooper/PlanesGenericCollection.java @@ -79,4 +79,8 @@ public class PlanesGenericCollection> _planeStorages; private static final String _separatorForKeyValue = "@"; + private static final String _separatorForKeyValueSingle = "@@"; private static final String _separatorRecords = ";"; + private static final String _separatorRecordsSingle = ";;"; private static final String _separatorForObject = ":"; + private static final String _separatorForObjectSingle = "::"; private static final String _keyword = "PlanesStorage"; + private static final String _keywordSingle = "PlanesStorageSingle"; public List Keys() { return _planeStorages.keySet().stream().toList(); @@ -49,6 +53,72 @@ public class PlanesGenericStorage { 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 collection; + if (_planeStorages.containsKey(key)){ + collection = _planeStorages.get(key); + collection.clear(); + } + else + collection = new PlanesGenericCollection<>(_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) { + DrawingPlane plane = ExtensionDrawingPlane.CreateDrawingPlane( + elem, + _separatorForObject, + _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) { var file = new File(filename); if (file.exists()) { @@ -99,12 +169,12 @@ public class PlanesGenericStorage { List reversedSet = Arrays.asList(set); Collections.reverse(reversedSet); for (String elem : reversedSet) { - DrawingPlane train = ExtensionDrawingPlane.CreateDrawingPlane( + DrawingPlane plane = ExtensionDrawingPlane.CreateDrawingPlane( elem, _separatorForObject, _pictureWidth, _pictureHeight ); - if (train == null || collection.Add(train) == -1) + if (plane == null || collection.Add(plane) == -1) return false; } _planeStorages.put(record[0], collection); diff --git a/ProjectStormtrooper/SetGeneric.java b/ProjectStormtrooper/SetGeneric.java index 63fd612..9d9201b 100644 --- a/ProjectStormtrooper/SetGeneric.java +++ b/ProjectStormtrooper/SetGeneric.java @@ -63,4 +63,8 @@ public class SetGeneric { public ArrayList GetEnumerator() { return _places; } + + public void clear() { + _places.clear(); + } }