using ProjectElectroTrans.Exceptions;
namespace ProjectElectroTrans.CollectionGenericObjects;
///
/// Параметризованный набор объектов
///
/// Параметр: ограничение - ссылочный тип
public class ListGenericObjects : ICollectionGenericObjects
where T : class
{
///
/// Список объектов, которые храним
///
private readonly List _collection;
///
/// Максимально допустимое число объектов в списке
///
private int _maxCount;
public int Count => _collection.Count;
public int MaxCount
{
get { return Count; }
set
{
if (value > 0)
{
_maxCount = value;
}
}
}
public CollectionType GetCollectionType => CollectionType.List;
///
/// Конструктор
///
public ListGenericObjects()
{
_collection = new();
}
public T Get(int position)
{
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
return _collection[position];
}
public int Insert(T obj)
{
if (Count == _maxCount) throw new CollectionOverflowException(Count);
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position)
{
if (Count == _maxCount) throw new CollectionOverflowException(Count);
if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position);
_collection.Insert(position, obj);
return position;
}
public T Remove(int position)
{
if (position >= _collection.Count || position < 0) throw new PositionOutOfCollectionException(position);
T obj = _collection[position];
_collection.RemoveAt(position);
return obj;
}
public IEnumerable GetItems()
{
for (int i = 0; i < Count; ++i)
{
yield return _collection[i];
}
}
}