57 lines
1.2 KiB
Java
57 lines
1.2 KiB
Java
import java.util.ArrayList;
|
|
import java.util.Objects;
|
|
|
|
public class SetTracktorGeneric<T> {
|
|
private final ArrayList<T> _places;
|
|
private final int _maxCount;
|
|
|
|
public int getCount() {
|
|
return _places.size();
|
|
}
|
|
|
|
public SetTracktorGeneric(int count) {
|
|
_maxCount = count;
|
|
_places = new ArrayList<>(count);
|
|
}
|
|
|
|
public int insert(T tracktor) {
|
|
return insert(tracktor, 0);
|
|
}
|
|
|
|
public int insert(T tracktor, int position) {
|
|
if (position < 0 || position > getCount() || getCount() == _maxCount) {
|
|
return -1;
|
|
}
|
|
_places.add(position, tracktor);
|
|
return position;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public T remove(int position) {
|
|
if (position < 0 || position >= getCount())
|
|
{
|
|
return null;
|
|
}
|
|
T result = _places.get(position);
|
|
_places.remove(position);
|
|
return result;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public T get(int position) {
|
|
if (position < 0 || position >= getCount()) {
|
|
return null;
|
|
}
|
|
return _places.get(position);
|
|
}
|
|
|
|
public Iterable<T> getTracktors()
|
|
{
|
|
return _places;
|
|
}
|
|
|
|
public void clear() {
|
|
_places.clear();
|
|
}
|
|
}
|