93 lines
2.3 KiB
C#
93 lines
2.3 KiB
C#
|
|
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
|
{
|
|
/// <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;
|
|
|
|
public int MaxCount {
|
|
get
|
|
{
|
|
return _maxCount;
|
|
}
|
|
|
|
set
|
|
{
|
|
if (value > 0)
|
|
{
|
|
_maxCount = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CollectionType GetCollectionType => CollectionType.List;
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
public ListGenericObjects()
|
|
{
|
|
_collection = new();
|
|
}
|
|
|
|
public T? Get(int position)
|
|
{
|
|
if (position >= Count || position < 0)
|
|
return null;
|
|
|
|
return _collection[position];
|
|
}
|
|
|
|
public int Insert(T obj)
|
|
{
|
|
if (Count + 1 > _maxCount)
|
|
return -1;
|
|
_collection.Add(obj);
|
|
return Count;
|
|
}
|
|
|
|
public int Insert(T obj, int position)
|
|
{
|
|
if (Count + 1 > _maxCount)
|
|
return -1;
|
|
if (position < 0 || position > Count)
|
|
return -1;
|
|
_collection.Insert(position, obj);
|
|
return 1;
|
|
}
|
|
|
|
public T? Remove(int position)
|
|
{
|
|
if (position < 0 || position > Count)
|
|
return null;
|
|
|
|
T? temp = _collection[position];
|
|
_collection.RemoveAt(position);
|
|
return temp;
|
|
}
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
{
|
|
for (int i = 0; i < _collection.Count; ++i)
|
|
{
|
|
yield return _collection[i];
|
|
}
|
|
}
|
|
}
|
|
}
|