195 lines
6.3 KiB
C#
195 lines
6.3 KiB
C#
using ProjectPlane.Drawnings;
|
||
using ProjectPlane.Exceptions;
|
||
|
||
namespace ProjectPlane.CollectionGenericObjects;
|
||
|
||
public class StorageCollection<T>
|
||
where T : DrawningShip
|
||
{
|
||
private readonly string _collectionKey = "CollectionsStorage";
|
||
|
||
private readonly string _separatorForKeyValue = "|";
|
||
|
||
private readonly string _separatorItems = ";";
|
||
|
||
|
||
private Dictionary<CollectionInfo, ICollectionGenericObjects<T>> _storages;
|
||
|
||
public List<CollectionInfo> Keys => _storages.Keys.ToList();
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public StorageCollection()
|
||
{
|
||
_storages = new Dictionary<CollectionInfo, ICollectionGenericObjects<T>>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление коллекции в хранилище
|
||
/// </summary>
|
||
/// <param name="name">Название коллекции</param>
|
||
/// <param name="collectionType">Тип коллекции</param>
|
||
public void AddCollection(CollectionInfo info)
|
||
{
|
||
if (info == null || _storages.ContainsKey(info))
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (info.CollectionType == CollectionType.Massive)
|
||
{
|
||
_storages.Add(info, new MassiveGenericObjects<T>());
|
||
}
|
||
|
||
if (info.CollectionType == CollectionType.List)
|
||
{
|
||
_storages.Add(info, new ListGenericObjects<T>());
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление коллекции
|
||
/// </summary>
|
||
/// <param name="name">Название коллекции</param>
|
||
public void DelCollection(CollectionInfo info)
|
||
{
|
||
if (info == null || !_storages.ContainsKey(info))
|
||
{
|
||
return;
|
||
}
|
||
|
||
_storages.Remove(info);
|
||
}
|
||
|
||
public void SaveData(string filename)
|
||
{
|
||
if (_storages.Count == 0)
|
||
{
|
||
throw new NullReferenceException("В хранилище отсутствуют коллекции для сохранения");
|
||
}
|
||
|
||
if (File.Exists(filename))
|
||
{
|
||
File.Delete(filename);
|
||
}
|
||
|
||
using (StreamWriter sw = new StreamWriter(filename))
|
||
{
|
||
sw.Write(_collectionKey);
|
||
|
||
foreach (KeyValuePair<CollectionInfo, ICollectionGenericObjects<T>> value in _storages)
|
||
{
|
||
sw.Write(Environment.NewLine);
|
||
// не сохраняем пустые коллекции
|
||
if (value.Value.Count == 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
sw.Write(value.Key);
|
||
sw.Write(_separatorForKeyValue);
|
||
sw.Write(value.Value.MaxCount);
|
||
sw.Write(_separatorForKeyValue);
|
||
|
||
foreach (T? item in value.Value.GetItems())
|
||
{
|
||
string data = item?.GetDataForSave() ?? string.Empty;
|
||
|
||
if (string.IsNullOrEmpty(data))
|
||
{
|
||
continue;
|
||
}
|
||
sw.Write(data);
|
||
sw.Write(_separatorItems);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public void LoadData(string filename)
|
||
{
|
||
if (!File.Exists(filename))
|
||
{
|
||
throw new FileNotFoundException("Файл не существует");
|
||
}
|
||
|
||
using (StreamReader sr = new(filename))
|
||
{
|
||
string line = sr.ReadLine();
|
||
|
||
if (line == null || line.Length == 0)
|
||
{
|
||
throw new FileFormatException("В файле нет данных");
|
||
}
|
||
if (!line.Equals(_collectionKey))
|
||
{
|
||
throw new FileFormatException("В файле неверные данные");
|
||
}
|
||
_storages.Clear();
|
||
|
||
while ((line = sr.ReadLine()) != null)
|
||
{
|
||
string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
||
if (record.Length != 3)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ??
|
||
throw new Exception("Не удалось определить информацию коллекции: " + record[0]);
|
||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionInfo.CollectionType) ??
|
||
throw new Exception("Не удалось создать коллекцию");
|
||
collection.MaxCount = Convert.ToInt32(record[1]);
|
||
|
||
string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||
foreach (string elem in set)
|
||
{
|
||
if (elem?.CreateDrawningShip() is T ship)
|
||
{
|
||
try
|
||
{
|
||
if (collection.Insert(ship, new DrawningShipEqutables()) == -1)
|
||
{
|
||
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
|
||
}
|
||
}
|
||
catch (CollectionOverflowException ex)
|
||
{
|
||
throw new OverflowException("Коллекция переполнена", ex);
|
||
}
|
||
catch (ObjectAlreadyInCollectionException ex)
|
||
{
|
||
throw new InvalidOperationException("Объект уже присутствует в коллекции", ex);
|
||
}
|
||
}
|
||
}
|
||
_storages.Add(collectionInfo, collection);
|
||
}
|
||
}
|
||
}
|
||
|
||
public ICollectionGenericObjects<T>? this[CollectionInfo info]
|
||
{
|
||
get
|
||
{
|
||
if (_storages.ContainsKey(info))
|
||
{
|
||
return _storages[info];
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|
||
|
||
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
|
||
{
|
||
return collectionType switch
|
||
{
|
||
CollectionType.Massive => new MassiveGenericObjects<T>(),
|
||
CollectionType.List => new ListGenericObjects<T>(),
|
||
_ => null,
|
||
};
|
||
}
|
||
}
|