2024-06-13 13:58:43 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
2024-06-15 09:05:36 +04:00
|
|
|
|
namespace ProjectCruiser.CollectionGenericObj;
|
2024-06-13 13:58:43 +04:00
|
|
|
|
|
|
|
|
|
// Параметризованный набор объектов
|
2024-06-15 09:05:36 +04:00
|
|
|
|
public class ListGenObj<T> : ICollectionGenObj<T>
|
2024-06-13 13:58:43 +04:00
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
// Список объектов, которые храним
|
|
|
|
|
private readonly List<T?> _collection;
|
|
|
|
|
|
|
|
|
|
// Максимально допустимое число объектов в списке
|
|
|
|
|
private int _maxCount;
|
|
|
|
|
public int Count => _collection.Count;
|
|
|
|
|
|
|
|
|
|
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
|
|
|
|
|
|
2024-06-15 09:05:36 +04:00
|
|
|
|
public ListGenObj()
|
2024-06-13 13:58:43 +04:00
|
|
|
|
{
|
|
|
|
|
_collection = new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? GetItem(int position)
|
|
|
|
|
{
|
2024-06-15 09:05:36 +04:00
|
|
|
|
if (position >= Count || position < 0)
|
2024-06-13 13:58:43 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _collection[position];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj)
|
|
|
|
|
{
|
2024-06-15 09:05:36 +04:00
|
|
|
|
if (Count >= _maxCount || obj == null)
|
2024-06-13 13:58:43 +04:00
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-06-15 09:05:36 +04:00
|
|
|
|
|
2024-06-13 13:58:43 +04:00
|
|
|
|
_collection.Add(obj);
|
|
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj, int position)
|
|
|
|
|
{
|
2024-06-15 09:05:36 +04:00
|
|
|
|
if (position >= _maxCount || Count >= _maxCount ||
|
|
|
|
|
position < 0 || _collection[position] != null
|
|
|
|
|
|| obj == null)
|
2024-06-13 13:58:43 +04:00
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_collection.Insert(position, obj);
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position >= Count || position < 0)
|
2024-06-15 09:05:36 +04:00
|
|
|
|
// on the other positions items don't exist
|
2024-06-13 13:58:43 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T? item = _collection[position];
|
|
|
|
|
_collection.RemoveAt(position);
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|