2022-11-22 00:21:46 +04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2022-11-21 21:34:45 +04:00
|
|
|
public class SetTracktorGeneric<T> {
|
2022-11-22 00:21:46 +04:00
|
|
|
private final ArrayList<T> _places;
|
|
|
|
private final int _maxCount;
|
2022-11-21 21:34:45 +04:00
|
|
|
|
|
|
|
public int getCount() {
|
2022-11-22 00:21:46 +04:00
|
|
|
return _places.size();
|
2022-11-21 21:34:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public SetTracktorGeneric(int count) {
|
2022-11-22 00:21:46 +04:00
|
|
|
_maxCount = count;
|
|
|
|
_places = new ArrayList<>(count);
|
2022-11-21 21:34:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public int insert(T tracktor) {
|
|
|
|
return insert(tracktor, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int insert(T tracktor, int position) {
|
2022-11-22 00:21:46 +04:00
|
|
|
if (position < 0 || position > getCount() || getCount() == _maxCount) {
|
2022-11-21 21:34:45 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2022-11-22 00:21:46 +04:00
|
|
|
_places.add(position, tracktor);
|
|
|
|
return position;
|
2022-11-21 21:34:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public T remove(int position) {
|
2022-11-22 00:21:46 +04:00
|
|
|
if (position < 0 || position >= getCount())
|
2022-11-21 21:34:45 +04:00
|
|
|
{
|
2022-11-22 00:21:46 +04:00
|
|
|
return null;
|
2022-11-21 21:34:45 +04:00
|
|
|
}
|
2022-11-22 00:21:46 +04:00
|
|
|
T result = _places.get(position);
|
|
|
|
_places.remove(position);
|
|
|
|
return result;
|
2022-11-21 21:34:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public T get(int position) {
|
|
|
|
if (position < 0 || position >= getCount()) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-22 00:21:46 +04:00
|
|
|
return _places.get(position);
|
|
|
|
}
|
2022-11-21 21:34:45 +04:00
|
|
|
|
2022-11-22 00:21:46 +04:00
|
|
|
public Iterable<T> getTracktors()
|
|
|
|
{
|
|
|
|
return _places;
|
2022-11-21 21:34:45 +04:00
|
|
|
}
|
2022-12-06 15:01:05 +04:00
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
_places.clear();
|
|
|
|
}
|
2022-11-21 21:34:45 +04:00
|
|
|
}
|