69 lines
1.4 KiB
Java
69 lines
1.4 KiB
Java
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.Objects;
|
|
|
|
public class SetArtilleriesGeneric<T> {
|
|
private final ArrayList<T> _places;
|
|
private final int _maxCount;
|
|
|
|
public int getCount() {
|
|
return _places.size();
|
|
}
|
|
|
|
public SetArtilleriesGeneric(int count) {
|
|
_maxCount = count;
|
|
_places = new ArrayList<>(count);
|
|
}
|
|
|
|
public int insert(T artillery) {
|
|
return insert(artillery, 0);
|
|
}
|
|
|
|
public int insert(T artillery, int position) {
|
|
if (getCount() == _maxCount) {
|
|
throw new StorageOverflowException(_maxCount);
|
|
}
|
|
|
|
if (position < 0 || position > getCount()) {
|
|
return -1;
|
|
}
|
|
|
|
_places.add(position, artillery);
|
|
|
|
return position;
|
|
}
|
|
|
|
public T remove(int position) {
|
|
if (position < 0 || position >= getCount())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
T result = _places.get(position);
|
|
|
|
if (result == null) {
|
|
throw new ArtilleryNotFoundException(position);
|
|
}
|
|
|
|
_places.remove(position);
|
|
|
|
return result;
|
|
}
|
|
|
|
public T get(int position) {
|
|
if (position < 0 || position >= getCount()) {
|
|
return null;
|
|
}
|
|
|
|
return _places.get(position);
|
|
}
|
|
|
|
public Iterable<T> getArtilleries() {
|
|
return () -> _places.stream().filter(Objects::nonNull).iterator();
|
|
}
|
|
|
|
public void clear() {
|
|
_places.clear();
|
|
}
|
|
}
|