2023-11-04 19:58:26 +04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
|
2023-11-04 14:33:13 +04:00
|
|
|
public class SetGeneric <T extends Object>{
|
2023-11-04 19:58:26 +04:00
|
|
|
private final ArrayList<T> _places;
|
|
|
|
public int Count() {return _places.size();}
|
|
|
|
private final int _maxCount;
|
2023-11-04 14:33:13 +04:00
|
|
|
public SetGeneric(int count)
|
|
|
|
{
|
2023-11-04 19:58:26 +04:00
|
|
|
_maxCount = count;
|
|
|
|
_places = new ArrayList<>();
|
2023-11-04 14:33:13 +04:00
|
|
|
}
|
|
|
|
public int Insert(T tanker)
|
|
|
|
{
|
|
|
|
return Insert(tanker, 0);
|
|
|
|
}
|
|
|
|
public int Insert(T tanker, int position)
|
|
|
|
{
|
2023-11-04 19:58:26 +04:00
|
|
|
if (position < 0 || position >= _maxCount)
|
2023-11-04 14:33:13 +04:00
|
|
|
return -1;
|
2023-11-04 19:58:26 +04:00
|
|
|
_places.add(position, tanker);
|
2023-11-04 14:33:13 +04:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:58:26 +04:00
|
|
|
public T Remove(int position)
|
2023-11-04 14:33:13 +04:00
|
|
|
{
|
2023-11-04 19:58:26 +04:00
|
|
|
if (position < 0 || position >= Count())
|
|
|
|
return null;
|
|
|
|
var returning = _places.get(position);
|
|
|
|
_places.remove(position);
|
|
|
|
return returning;
|
2023-11-04 14:33:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public T Get(int position)
|
|
|
|
{
|
|
|
|
if (position < 0 || position >= Count())
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-04 19:58:26 +04:00
|
|
|
return _places.get(position);
|
2023-11-04 14:33:13 +04:00
|
|
|
}
|
|
|
|
|
2023-11-04 19:58:26 +04:00
|
|
|
public Iterable<T> GetTankers(final Integer maxTankers) {
|
|
|
|
return new Iterable<T>() {
|
|
|
|
@Override
|
|
|
|
public Iterator<T> iterator() {
|
|
|
|
return new Iterator<T>() {
|
|
|
|
private int currentIndex = 0;
|
|
|
|
private int count = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return currentIndex < _places.size() && (maxTankers == null || count < maxTankers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T next() {
|
|
|
|
if (hasNext()) {
|
|
|
|
count++;
|
|
|
|
return _places.get(currentIndex++);
|
|
|
|
}
|
|
|
|
throw new NoSuchElementException();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-11-04 14:33:13 +04:00
|
|
|
}
|