2022-11-07 21:03:30 +04:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
public class SetPlanesGeneric<T extends Object> implements Iterable<T>
|
2022-11-05 21:26:27 +04:00
|
|
|
|
{
|
|
|
|
|
//массив объектов, которые храним
|
2022-11-07 21:03:30 +04:00
|
|
|
|
private ArrayList<T> _places;
|
|
|
|
|
|
|
|
|
|
//максимальное кол-во элементов в списке
|
|
|
|
|
private final int _maxCount;
|
2022-11-05 21:26:27 +04:00
|
|
|
|
|
|
|
|
|
//количество объектов в массиве
|
|
|
|
|
public int Count()
|
|
|
|
|
{
|
2022-11-07 21:03:30 +04:00
|
|
|
|
return _places.size();
|
2022-11-05 21:26:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//конструктор
|
|
|
|
|
public SetPlanesGeneric(int count)
|
|
|
|
|
{
|
2022-11-07 21:03:30 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new ArrayList<>();
|
2022-11-05 21:26:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//добавление объекта в набор
|
2022-12-02 17:21:08 +04:00
|
|
|
|
public int Insert(T plane) throws StorageOverflowException {
|
2022-11-05 21:26:27 +04:00
|
|
|
|
return Insert(plane, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//добавление объекта в набор на конкретную позицию
|
2022-12-02 17:21:08 +04:00
|
|
|
|
public int Insert(T plane, int position) throws StorageOverflowException {
|
|
|
|
|
//проверка на переполнение хранилища
|
|
|
|
|
if(_maxCount == Count())
|
|
|
|
|
{
|
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-06 12:53:10 +04:00
|
|
|
|
//проверка на корректность значения индекса
|
2022-11-07 21:03:30 +04:00
|
|
|
|
if (position >= _maxCount|| position < 0)
|
2022-11-05 21:26:27 +04:00
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 21:03:30 +04:00
|
|
|
|
_places.add(plane);
|
2022-11-05 21:26:27 +04:00
|
|
|
|
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//удаление объекта из набора с конкретной позиции
|
2022-12-02 17:21:08 +04:00
|
|
|
|
public T Remove(int position) throws PlaneNotFoundException
|
2022-11-05 21:26:27 +04:00
|
|
|
|
{
|
|
|
|
|
// проверка позиции
|
2022-12-02 18:13:52 +04:00
|
|
|
|
if (position >= _places.size() || position < 0)
|
2022-11-05 21:26:27 +04:00
|
|
|
|
{
|
2022-12-02 17:21:08 +04:00
|
|
|
|
throw new PlaneNotFoundException(position);
|
2022-11-05 21:26:27 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 18:27:25 +04:00
|
|
|
|
if (_places.get(position) == null)
|
|
|
|
|
{
|
|
|
|
|
throw new PlaneNotFoundException(position);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 21:26:27 +04:00
|
|
|
|
// удаление объекта из массива, присовив элементу массива значение null
|
2022-11-07 21:03:30 +04:00
|
|
|
|
T temp = _places.get(position);
|
|
|
|
|
_places.remove(position);
|
2022-11-05 21:26:27 +04:00
|
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//получение объекта из набора по позиции
|
|
|
|
|
public T Get(int position)
|
|
|
|
|
{
|
2022-11-07 21:03:30 +04:00
|
|
|
|
if (position >= _maxCount || position < 0)
|
2022-11-05 21:26:27 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-11-07 21:03:30 +04:00
|
|
|
|
|
|
|
|
|
return _places.get(position);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 17:21:08 +04:00
|
|
|
|
public void Set(int position, T value) throws StorageOverflowException
|
2022-11-07 21:03:30 +04:00
|
|
|
|
{
|
|
|
|
|
if(position >= _maxCount || position < 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Insert(value, position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Iterator<T> iterator()
|
|
|
|
|
{
|
|
|
|
|
return _places.iterator();
|
2022-11-05 21:26:27 +04:00
|
|
|
|
}
|
|
|
|
|
}
|