namespace ProjectElectroTrans.CollectionGenericObjects;
///
/// Параметризованный набор объектов
///
/// Параметр: ограничение - ссылочный тип
public class ListGenericObjects : ICollectionGenericObjects
where T : class
{
///
/// Список объектов, которые храним
///
private readonly List _collection;
///
/// Максимально допустимое число объектов в списке
///
private int _maxCount;
public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
///
/// Конструктор
///
public ListGenericObjects()
{
_collection = new();
}
public T? Get(int position)
{
if (position < 0 || position > _collection.Count - 1)
{
return null;
}
return _collection[position];
}
public int Insert(T obj)
{
if (_collection.Count + 1 > _maxCount) { return 0; }
_collection.Add(obj);
return 1;
}
public int Insert(T obj, int position)
{
if (_collection.Count + 1 < _maxCount) { return 0; }
if (position > _collection.Count || position < 0)
{
return 0;
}
_collection.Insert(position, obj);
return 1;
}
public T Remove(int position)
{
if (position > _collection.Count || position < 0)
{
return null;
}
T temp = _collection[position];
_collection.RemoveAt(position);
return temp;
}
}