2024-03-28 09:57:08 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ProjectElectricLocomotive.CollectionGenericObjects
|
|
|
|
|
{
|
|
|
|
|
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|
|
|
|
where T: class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список объектов, которые храним
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly List<T?> _collection;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Максимально допустимое число объектов в списке
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int _maxCount;
|
2024-04-25 17:03:43 +04:00
|
|
|
|
|
|
|
|
|
public int MaxCount
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
|
|
|
|
_maxCount = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-28 09:57:08 +04:00
|
|
|
|
|
|
|
|
|
public int Count => _collection.Count;
|
|
|
|
|
|
2024-04-25 17:03:43 +04:00
|
|
|
|
public CollectionType GetCollectionType => CollectionType.List;
|
|
|
|
|
|
2024-03-28 09:57:08 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ListGenericObjects()
|
|
|
|
|
{
|
|
|
|
|
_collection = new();
|
|
|
|
|
}
|
|
|
|
|
public T? Get(int position)
|
|
|
|
|
{
|
2024-04-01 17:28:49 +04:00
|
|
|
|
if(position >= 0 && position < Count)
|
|
|
|
|
{
|
|
|
|
|
return _collection[position];
|
|
|
|
|
}
|
2024-03-28 09:57:08 +04:00
|
|
|
|
// TODO проверка позиции
|
2024-04-01 17:28:49 +04:00
|
|
|
|
return null;
|
2024-03-28 09:57:08 +04:00
|
|
|
|
}
|
|
|
|
|
public int Insert(T obj)
|
|
|
|
|
{
|
2024-04-01 17:28:49 +04:00
|
|
|
|
if(Count <= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
_collection.Add(obj);
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
2024-03-28 09:57:08 +04:00
|
|
|
|
// TODO проверка, что не превышено максимальное количество элементов
|
|
|
|
|
// TODO вставка в конец набора
|
2024-04-01 17:28:49 +04:00
|
|
|
|
return -1;
|
2024-03-28 09:57:08 +04:00
|
|
|
|
}
|
|
|
|
|
public int Insert(T obj, int position)
|
|
|
|
|
{
|
2024-04-01 17:28:49 +04:00
|
|
|
|
if(Count <= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
_collection.Insert(position, obj);
|
|
|
|
|
return position;
|
|
|
|
|
}
|
2024-03-28 09:57:08 +04:00
|
|
|
|
// TODO проверка, что не превышено максимальное количество элементов
|
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO вставка по позиции
|
2024-04-01 17:28:49 +04:00
|
|
|
|
return -1;
|
2024-03-28 09:57:08 +04:00
|
|
|
|
}
|
|
|
|
|
public T Remove(int position)
|
|
|
|
|
{
|
2024-04-01 17:28:49 +04:00
|
|
|
|
if(position >= 0 && position <= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
T ret = _collection[position];
|
|
|
|
|
_collection.RemoveAt(position);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2024-03-28 09:57:08 +04:00
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO удаление объекта из списка
|
2024-04-01 17:28:49 +04:00
|
|
|
|
return null;
|
2024-03-28 09:57:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 17:03:43 +04:00
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _collection[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-28 09:57:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|