PIbd-21 Potapov N.S. LabWork06 Hard #6
@ -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<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _planeStorages;
|
||||
private static final String _separatorForKeyValue = "|";
|
||||
private static final String _separatorRecords = ";";
|
||||
private static final String _separatorForObject = ":";
|
||||
private static final String _keyword = "PlanesStorage";
|
||||
|
||||
public List<String> 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<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()) {
|
||||
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<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 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user