2022-11-05 13:03:36 +04:00
|
|
|
import java.util.ArrayList;
|
2022-11-20 20:37:09 +04:00
|
|
|
import java.util.Collections;
|
2022-11-05 13:03:36 +04:00
|
|
|
|
2022-10-21 16:04:51 +04:00
|
|
|
public class SetLocomotivesGeneric <T>
|
|
|
|
{
|
2022-11-05 13:03:36 +04:00
|
|
|
/// Список хранимых объектов
|
|
|
|
private final ArrayList<T> _places;
|
2022-10-21 16:04:51 +04:00
|
|
|
|
|
|
|
public int Count() {
|
2022-11-05 13:03:36 +04:00
|
|
|
return _places.size();
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-11-05 13:03:36 +04:00
|
|
|
// Ограничение на количество
|
|
|
|
private final int _maxCount;
|
|
|
|
|
2022-10-21 16:04:51 +04:00
|
|
|
public SetLocomotivesGeneric(int count) {
|
2022-11-05 13:03:36 +04:00
|
|
|
_maxCount = count;
|
|
|
|
_places = new ArrayList<>();
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-12-02 18:34:48 +04:00
|
|
|
public int Insert (T locomotive) throws StorageOverflowException{
|
2022-10-21 16:04:51 +04:00
|
|
|
return Insert(locomotive, 0);
|
|
|
|
}
|
|
|
|
|
2022-12-02 18:34:48 +04:00
|
|
|
public int Insert (T locomotive, int position) throws StorageOverflowException{
|
|
|
|
if (position < 0) return -1;
|
2022-12-02 21:20:08 +04:00
|
|
|
if (Count() >= _maxCount) {
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
_places.add(position, locomotive);
|
|
|
|
return position;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-12-02 18:34:48 +04:00
|
|
|
public T Remove (int position) throws LocomotiveNotFoundException {
|
2022-11-05 13:03:36 +04:00
|
|
|
if (position >= _maxCount || position < 0) return null;
|
2022-12-02 21:20:08 +04:00
|
|
|
if (_places.get(position) == null) {
|
|
|
|
throw new LocomotiveNotFoundException(position);
|
|
|
|
}
|
2022-11-05 13:03:36 +04:00
|
|
|
T result = _places.get(position);
|
2022-12-02 18:34:48 +04:00
|
|
|
_places.set(position, null);
|
2022-10-21 21:59:22 +04:00
|
|
|
return result;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-11-19 21:53:44 +04:00
|
|
|
public void Clear() {
|
|
|
|
_places.clear();
|
|
|
|
}
|
|
|
|
|
2022-10-21 16:04:51 +04:00
|
|
|
public T Get(int position)
|
|
|
|
{
|
2022-11-05 13:03:36 +04:00
|
|
|
if (position >= _maxCount || position < 0)
|
2022-10-21 16:04:51 +04:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-05 13:03:36 +04:00
|
|
|
return _places.get(position);
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
2022-11-05 13:03:36 +04:00
|
|
|
public Iterable<T> GetLocomotives()
|
|
|
|
{
|
|
|
|
return _places;
|
|
|
|
}
|
2022-11-20 20:37:09 +04:00
|
|
|
public void ReversePlaces() {
|
|
|
|
Collections.reverse(_places);
|
|
|
|
}
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|