using ProjectAirplaneWithRadar.Exceptions;
namespace ProjectAirplaneWithRadar.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 _maxCount;
}
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, IEqualityComparer? comparer = null)
{
if (Count == _maxCount)
throw new CollectionOverflowException(Count);
if (_collection.Contains(obj, comparer))
{
throw new ObjectExistException();
}
_collection.Add(obj);
return Count;
}
public int Insert(T obj, int position, IEqualityComparer? comparer = null)
{
if (Count == _maxCount)
throw new CollectionOverflowException(Count);
if (position < 0 || position > Count)
throw new PositionOutOfCollectionException(position);
if (_collection.Contains(obj, comparer))
{
throw new ObjectExistException(position);
}
_collection.Insert(position, obj);
return position;
}
public T? Remove(int position)
{
if (position < 0 || position > Count)
throw new PositionOutOfCollectionException(position);
T? temp = _collection[position];
_collection.RemoveAt(position);
return temp;
}
public IEnumerable GetItems()
{
for (int i = 0; i < _collection.Count; ++i)
{
yield return _collection[i];
}
}
public void CollectionSort(IComparer comparer)
{
_collection.Sort(comparer);
}
}
}