2024-04-14 12:18:57 +04:00
|
|
|
|
|
2024-04-20 10:04:26 +04:00
|
|
|
|
using ProjectAirplaneWithRadar.Exceptions;
|
|
|
|
|
|
2024-04-14 12:18:57 +04:00
|
|
|
|
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
2024-03-28 18:07:22 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
|
|
|
|
|
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список объектов, которые храним
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly List<T?> _collection;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Максимально допустимое число объектов в списке
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int _maxCount;
|
|
|
|
|
|
|
|
|
|
public int Count => _collection.Count;
|
|
|
|
|
|
2024-04-14 12:27:49 +04:00
|
|
|
|
public int MaxCount {
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _maxCount;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 12:25:50 +04:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
|
|
|
|
_maxCount = value;
|
|
|
|
|
}
|
2024-04-14 12:27:49 +04:00
|
|
|
|
}
|
2024-04-14 12:25:50 +04:00
|
|
|
|
}
|
2024-03-28 18:07:22 +04:00
|
|
|
|
|
2024-04-14 12:18:57 +04:00
|
|
|
|
public CollectionType GetCollectionType => CollectionType.List;
|
|
|
|
|
|
2024-03-28 18:07:22 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ListGenericObjects()
|
|
|
|
|
{
|
|
|
|
|
_collection = new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Get(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position >= Count || position < 0)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
2024-03-28 18:07:22 +04:00
|
|
|
|
|
|
|
|
|
return _collection[position];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj)
|
|
|
|
|
{
|
|
|
|
|
if (Count + 1 > _maxCount)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new CollectionOverflowException(Count);
|
|
|
|
|
|
2024-03-28 18:07:22 +04:00
|
|
|
|
_collection.Add(obj);
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj, int position)
|
|
|
|
|
{
|
|
|
|
|
if (Count + 1 > _maxCount)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new CollectionOverflowException(Count);
|
|
|
|
|
|
2024-03-28 18:07:22 +04:00
|
|
|
|
if (position < 0 || position > Count)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
|
|
|
|
|
2024-03-28 18:07:22 +04:00
|
|
|
|
_collection.Insert(position, obj);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position > Count)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
2024-03-28 18:07:22 +04:00
|
|
|
|
|
|
|
|
|
T? temp = _collection[position];
|
|
|
|
|
_collection.RemoveAt(position);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2024-04-14 12:18:57 +04:00
|
|
|
|
|
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _collection.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _collection[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-28 18:07:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|