PIbd-14_Pruidze_I.K_Simple_.../ProjectCruiser/CollectionGenericObj/ListGenObj.cs

159 lines
4.1 KiB
C#
Raw Normal View History

using System;
2024-06-15 18:06:35 +04:00
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using ProjectCruiser.Exceptions;
2024-06-15 09:05:36 +04:00
namespace ProjectCruiser.CollectionGenericObj;
// Параметризованный набор объектов
2024-06-15 09:05:36 +04:00
public class ListGenObj<T> : ICollectionGenObj<T>
where T : class
{
// Список объектов, которые храним
2024-06-15 18:06:35 +04:00
private List<T?> _collection;
// Максимально допустимое число объектов в списке
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-15 09:05:36 +04:00
public ListGenObj()
{
_collection = new();
}
public T? GetItem(int position)
{
try
{
if (position > Count)
throw new CollectionOverflowException(position);
if (position < 0)
throw new PositionOutOfCollectionException(position);
if (_collection[position] == null)
throw new ObjectNotFoundException(position);
return _collection[position];
}
catch (CollectionOverflowException ex)
{
Console.WriteLine(ex.Message);
return null;
}
catch (ObjectNotFoundException ex)
{
Console.WriteLine(ex.Message);
return null;
}
catch (PositionOutOfCollectionException ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
2024-06-15 18:06:35 +04:00
public int Insert(T? obj)
{
if (obj == null) { return -1; }
try // выход за границы, курируется CollectionOverflowException
{
if (Count >= _maxCount)
{
throw new CollectionOverflowException(Count);
}
_collection.Add(obj);
return Count;
}
catch (CollectionOverflowException ex)
{
Console.WriteLine(ex.Message);
return -1;
}
}
2024-06-15 18:06:35 +04:00
public int Insert(T? obj, int position)
{
try
{
if (position < 0 || position >= _maxCount) throw new PositionOutOfCollectionException(position);
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
if (obj == null) throw new ObjectNotFoundException(position);
_collection.Insert(position, obj);
return position;
}
catch (CollectionOverflowException ex)
{
Console.WriteLine(ex.Message);
return -1;
}
catch (ObjectNotFoundException ex)
{
Console.WriteLine(ex.Message);
return -1;
}
catch (PositionOutOfCollectionException ex)
{
Console.WriteLine(ex.Message);
return -1;
}
}
public T? Remove(int position)
{
try {
if (position >= _maxCount || position < 0)
// on the other positions items don't exist
{
throw new CollectionOverflowException(position);
}
T? item = _collection[position];
_collection.RemoveAt(position);
if (item == null) throw new ObjectNotFoundException(position);
return item;
}
catch (CollectionOverflowException ex)
{
Console.WriteLine(ex.Message);
return null;
}
catch (ObjectNotFoundException ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
2024-06-15 13:28:42 +04:00
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Count; ++i)
{
yield return _collection[i];
}
}
}