159 lines
6.1 KiB
Java
159 lines
6.1 KiB
Java
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.util.Map;
|
|
import java.util.Scanner;
|
|
public class BomberGenericStorage {
|
|
public HashMap<String, BomberGenericCollection<DrawingAir, DrawingObjectPlane>> planeStorages;
|
|
private final int _pictureWidth;
|
|
private final int _pictureHeight;
|
|
|
|
private static final char _separatorForKeyValue = '|';
|
|
private final char _separatorRecords = ';';
|
|
private static final char _separatorForObject = ':';
|
|
|
|
public void SaveData(File f) throws IOException {
|
|
if(f.exists()) {
|
|
f.delete();
|
|
}
|
|
f.createNewFile();
|
|
StringBuilder data = new StringBuilder();
|
|
data.append("PlaneStorage\n");
|
|
for(Map.Entry<String, BomberGenericCollection<DrawingAir, DrawingObjectPlane>> record : planeStorages.entrySet()){
|
|
StringBuilder records = new StringBuilder();
|
|
for(DrawingAir elem : record.getValue().GetPlanes())
|
|
{
|
|
records.append(String.format("%s%c", ExtentionDrawingBomber.GetDataForSave(elem, _separatorForObject),
|
|
_separatorRecords));
|
|
}
|
|
data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString()));
|
|
}
|
|
if(data.length() == 0)
|
|
return;
|
|
FileWriter writer = new FileWriter(f);
|
|
writer.write(data.toString());
|
|
writer.flush();
|
|
writer.close();
|
|
}
|
|
public boolean LoadData(File f) throws FileNotFoundException {
|
|
if(!f.exists())
|
|
return false;
|
|
StringBuilder bufferTextFromFile = new StringBuilder();
|
|
Scanner s = new Scanner(f);
|
|
while(s.hasNext())
|
|
bufferTextFromFile.append(s.next() + "\n");
|
|
s.close();
|
|
var strs = bufferTextFromFile.toString().split("\n");
|
|
if(strs == null || strs.length == 0)
|
|
return false;
|
|
if (!strs[0].startsWith("PlaneStorage"))
|
|
return false;
|
|
planeStorages.clear();
|
|
for(String data : strs){
|
|
String st = new String("\\" + Character.toString( _separatorForKeyValue));
|
|
String[]record = data.split(st);
|
|
if (record.length != 2)
|
|
continue;
|
|
BomberGenericCollection<DrawingAir, DrawingObjectPlane> collection =
|
|
new BomberGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
String[] set = record[1].split(Character.toString(_separatorRecords));
|
|
|
|
for(int i = set.length -1; i >=0; i--){
|
|
String elem = set[i];
|
|
DrawingAir plane = ExtentionDrawingBomber.CreateDrawingPlane(elem,
|
|
_separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (plane != null)
|
|
{
|
|
if (!(collection.Insert(plane)))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
planeStorages.put(record[0], collection);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean LoadCollection(File f) throws FileNotFoundException {
|
|
if(!f.exists())
|
|
return false;
|
|
StringBuilder bufferTextFromFile = new StringBuilder();
|
|
Scanner s = new Scanner(f);
|
|
while(s.hasNext())
|
|
bufferTextFromFile.append(s.next() + "\n");
|
|
s.close();
|
|
var strs = bufferTextFromFile.toString().split("\n");
|
|
if(strs == null || strs.length == 0)
|
|
return false;
|
|
if (!strs[0].startsWith("PlaneCollection"))
|
|
return false;
|
|
String collectionName = strs[1];
|
|
BomberGenericCollection<DrawingAir, DrawingObjectPlane> collection = getCollection(collectionName);
|
|
if(collection == null)
|
|
collection = new BomberGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
else
|
|
collection.Clear();
|
|
String[] planesInfo = strs[2].split(Character.toString(BomberGenericCollection._separatorRecords));
|
|
for(int i = planesInfo.length-1; i >= 0; i--){
|
|
String data = planesInfo[i];
|
|
DrawingAir plane = ExtentionDrawingBomber.CreateDrawingPlane(data,
|
|
BomberGenericCollection._separatorForObject, _pictureWidth, _pictureHeight);
|
|
if (plane != null)
|
|
{
|
|
if (!(collection.Insert(plane)))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
AddSetFromFile(collectionName, collection);
|
|
return true;
|
|
}
|
|
public BomberGenericStorage(int pictureWidth, int pictureHeight){
|
|
planeStorages = new HashMap<>();
|
|
_pictureWidth = pictureWidth;
|
|
_pictureHeight = pictureHeight;
|
|
}
|
|
public ArrayList<String> Keys(){
|
|
return new ArrayList<>(planeStorages.keySet());
|
|
}
|
|
public void AddSet(String name){
|
|
if(planeStorages.containsKey(name))
|
|
return;
|
|
planeStorages.put(name, new BomberGenericCollection<>(_pictureWidth, _pictureHeight));
|
|
}
|
|
|
|
public void AddSetFromFile(String name, BomberGenericCollection<DrawingAir, DrawingObjectPlane> toAdd){
|
|
if(planeStorages.containsKey(name)){
|
|
planeStorages.remove(name);
|
|
}
|
|
planeStorages.put(name, toAdd);
|
|
}
|
|
|
|
public void DelSet(String name, BomberTrashCollection<DrawingAir> trashBox){
|
|
if(!planeStorages.containsKey(name))
|
|
return;
|
|
BomberGenericCollection<DrawingAir, DrawingObjectPlane> cur = planeStorages.get(name);
|
|
for(int i = 0; i < cur.size(); i++)
|
|
trashBox.Push(cur.Get(i));
|
|
planeStorages.remove(name);
|
|
}
|
|
|
|
public BomberGenericCollection<DrawingAir, DrawingObjectPlane> GetCollection(String collectionName){
|
|
return planeStorages.get(collectionName);
|
|
}
|
|
public BomberGenericCollection<DrawingAir, DrawingObjectPlane> getCollection(String name){
|
|
if(!planeStorages.containsKey(name))
|
|
return null;
|
|
return planeStorages.get(name);
|
|
}
|
|
|
|
public DrawingAir getPlanes(String collectionName, int position){
|
|
return planeStorages.get(collectionName).Get(position);
|
|
}
|
|
}
|