Добавлен метод Clear
This commit is contained in:
parent
9bb8540007
commit
585712ebf9
@ -79,4 +79,8 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
_collection.clear();
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,13 @@ import java.io.IOException;
|
||||
public class PlanesGenericStorage {
|
||||
final HashMap<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _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<String> 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<DrawingPlane, DrawingObjectPlane> collection;
|
||||
if (_planeStorages.containsKey(key)){
|
||||
collection = _planeStorages.get(key);
|
||||
collection.clear();
|
||||
}
|
||||
else
|
||||
collection = new PlanesGenericCollection<>(_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) {
|
||||
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<String> 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);
|
||||
|
@ -63,4 +63,8 @@ public class SetGeneric<T extends DrawingPlane> {
|
||||
public ArrayList<T> GetEnumerator() {
|
||||
return _places;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
_places.clear();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user