64 lines
1.6 KiB
Java
64 lines
1.6 KiB
Java
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
|
|
public class SetLocomotivesGeneric <T>
|
|
{
|
|
/// Список хранимых объектов
|
|
private final ArrayList<T> _places;
|
|
|
|
public int Count() {
|
|
return _places.size();
|
|
}
|
|
|
|
// Ограничение на количество
|
|
private final int _maxCount;
|
|
|
|
public SetLocomotivesGeneric(int count) {
|
|
_maxCount = count;
|
|
_places = new ArrayList<>();
|
|
}
|
|
|
|
public int Insert (T locomotive) throws StorageOverflowException{
|
|
return Insert(locomotive, 0);
|
|
}
|
|
|
|
public int Insert (T locomotive, int position) throws StorageOverflowException{
|
|
if (position < 0) return -1;
|
|
if (Count() >= _maxCount) {
|
|
throw new StorageOverflowException(_maxCount);
|
|
}
|
|
_places.add(position, locomotive);
|
|
return position;
|
|
}
|
|
|
|
public T Remove (int position) throws LocomotiveNotFoundException {
|
|
if (position >= _maxCount || position < 0) return null;
|
|
if (_places.get(position) == null) {
|
|
throw new LocomotiveNotFoundException(position);
|
|
}
|
|
T result = _places.get(position);
|
|
_places.set(position, null);
|
|
return result;
|
|
}
|
|
|
|
public void Clear() {
|
|
_places.clear();
|
|
}
|
|
|
|
public T Get(int position)
|
|
{
|
|
if (position >= _maxCount || position < 0)
|
|
{
|
|
return null;
|
|
}
|
|
return _places.get(position);
|
|
}
|
|
public Iterable<T> GetLocomotives()
|
|
{
|
|
return _places;
|
|
}
|
|
public void ReversePlaces() {
|
|
Collections.reverse(_places);
|
|
}
|
|
}
|