185 lines
5.7 KiB
C#
185 lines
5.7 KiB
C#
using Catamaran.Drawings;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Catamaran.CollectionGenericObjects
|
|
{
|
|
public class StorageCollection<T>
|
|
where T : DrawingBoat
|
|
{
|
|
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
|
|
|
|
public List<string> Keys => _storages.Keys.ToList();
|
|
|
|
private readonly string _collectionKey = "CollectionsStorage";
|
|
|
|
private readonly string _separatorKeyValue = "|";
|
|
|
|
private readonly string _separatorItems = ";";
|
|
|
|
public StorageCollection()
|
|
{
|
|
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
|
|
}
|
|
|
|
public void AddCollection(string name, CollectionType collectionType)
|
|
{
|
|
if (!string.IsNullOrEmpty(name) && !_storages.ContainsKey(name))
|
|
{
|
|
switch (collectionType)
|
|
{
|
|
case CollectionType.None:
|
|
return;
|
|
case CollectionType.Array:
|
|
_storages[name] = new ArrayGenericObjects<T>();
|
|
return;
|
|
case CollectionType.List:
|
|
_storages[name] = new ListGenericObjects<T>();
|
|
return;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public void DelCollection(string name)
|
|
{
|
|
if (string.IsNullOrEmpty(name) || !_storages.ContainsKey(name))
|
|
{
|
|
return;
|
|
}
|
|
_storages.Remove(name);
|
|
}
|
|
|
|
public ICollectionGenericObjects<T>? this[string name]
|
|
{
|
|
get
|
|
{
|
|
if (!_storages.ContainsKey(name) || string.IsNullOrEmpty(name))
|
|
{
|
|
return null;
|
|
}
|
|
return _storages[name];
|
|
}
|
|
}
|
|
|
|
public bool SaveData(string filename)
|
|
{
|
|
if (_storages.Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (File.Exists(filename))
|
|
{
|
|
File.Delete(filename);
|
|
}
|
|
|
|
|
|
using (StreamWriter writer = new(filename))
|
|
{
|
|
writer.Write(_collectionKey);
|
|
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
|
{
|
|
writer.Write(Environment.NewLine);
|
|
if (value.Value.Count == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
writer.Write(value.Key);
|
|
writer.Write(_separatorKeyValue);
|
|
writer.Write(value.Value.GetCollectionType);
|
|
writer.Write(_separatorKeyValue);
|
|
writer.Write(value.Value.MaxCount);
|
|
writer.Write(_separatorKeyValue);
|
|
|
|
foreach (T? item in value.Value.GetItems())
|
|
{
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
if (string.IsNullOrEmpty(data))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
writer.Write(data);
|
|
writer.Write(_separatorItems);
|
|
}
|
|
}
|
|
writer.Close();
|
|
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool LoadData(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
using (StreamReader reader = new(filename))
|
|
{
|
|
string line = reader.ReadLine();
|
|
if (line == null || line.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!line.Equals(_collectionKey))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_storages.Clear();
|
|
while ((line = reader.ReadLine()) != null)
|
|
{
|
|
string[] record = line.Split(_separatorKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
if (record.Length != 4)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
|
if (collection == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
|
|
|
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string elem in set)
|
|
{
|
|
if (elem?.CreateDrawingBoat() is T boat)
|
|
{
|
|
if (collection.Insert(boat) < 0)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
_storages.Add(record[0], collection);
|
|
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
|
|
{
|
|
return collectionType switch
|
|
{
|
|
CollectionType.Array => new ArrayGenericObjects<T>(),
|
|
CollectionType.List => new ListGenericObjects<T>(),
|
|
_ => null,
|
|
};
|
|
}
|
|
|
|
}
|
|
}
|