2024-06-16 19:46:37 +04:00
|
|
|
|
using ProjectCruiser.Exceptions;
|
2024-06-13 13:58:43 +04:00
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
// Список объектов, которые храним
|
2024-06-15 18:06:35 +04:00
|
|
|
|
private List<T?> _collection;
|
2024-06-13 13:58:43 +04:00
|
|
|
|
|
|
|
|
|
// Максимально допустимое число объектов в списке
|
|
|
|
|
private int _maxCount;
|
|
|
|
|
public int Count => _collection.Count;
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
public int MaxCount
|
|
|
|
|
{
|
2024-06-15 18:06:35 +04:00
|
|
|
|
get { return _maxCount; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_collection.Count == 0) _collection = new List<T>(value);
|
|
|
|
|
else _collection.Capacity = value; // instead of resizing
|
|
|
|
|
|
|
|
|
|
_maxCount = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-15 13:28:42 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CollectionType GetCollectionType => CollectionType.List;
|
2024-06-13 13:58:43 +04:00
|
|
|
|
|
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-16 19:46:37 +04:00
|
|
|
|
if (position > _maxCount)
|
|
|
|
|
throw new CollectionOverflowException(position);
|
|
|
|
|
if (position < 0)
|
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
if (_collection[position] == null)
|
|
|
|
|
throw new ObjectNotFoundException(position);
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
return _collection[position];
|
2024-06-13 13:58:43 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 18:06:35 +04:00
|
|
|
|
public int Insert(T? obj)
|
2024-06-13 13:58:43 +04:00
|
|
|
|
{
|
2024-06-16 19:46:37 +04:00
|
|
|
|
if (obj == null)
|
|
|
|
|
throw new NullReferenceException("> Inserting object is null");
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
// выход за границы, курируется CollectionOverflowException
|
|
|
|
|
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
_collection.Add(obj);
|
|
|
|
|
return Count;
|
2024-06-13 13:58:43 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 18:06:35 +04:00
|
|
|
|
public int Insert(T? obj, int position)
|
2024-06-13 13:58:43 +04:00
|
|
|
|
{
|
2024-06-16 19:46:37 +04:00
|
|
|
|
if (position < 0 || position >= _maxCount)
|
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
|
|
|
|
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
if (obj == null)
|
|
|
|
|
throw new NullReferenceException("> Inserting object (at position) is null");
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
_collection.Insert(position, obj);
|
|
|
|
|
return position;
|
2024-06-13 13:58:43 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Remove(int position)
|
|
|
|
|
{
|
2024-06-16 19:46:37 +04:00
|
|
|
|
if (position >= _maxCount || position < 0)
|
|
|
|
|
// on the other positions items don't exist
|
|
|
|
|
{
|
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
|
|
|
|
}
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
T? item = _collection[position];
|
|
|
|
|
_collection.RemoveAt(position);
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
if (item == null) throw new ObjectNotFoundException(position);
|
2024-06-15 23:48:53 +04:00
|
|
|
|
|
2024-06-16 19:46:37 +04:00
|
|
|
|
return item;
|
2024-06-13 13:58:43 +04:00
|
|
|
|
}
|
2024-06-15 13:28:42 +04:00
|
|
|
|
|
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _collection.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _collection[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-13 13:58:43 +04:00
|
|
|
|
}
|