89 lines
2.5 KiB
Java

package ProjectElectricLocomotive;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends DrawingLocomotive>{
private ArrayList<T> _places;
public int Count()
{
return _places.size();
}
public int maxCount;
public SetGeneric(int count)
{
maxCount = count;
_places = new ArrayList<T>(count);
}
public int Insert(T loco)
{
return Insert(loco, 0);
}
public int Insert(T loco, int position)
{
int NoEmpty = 0, temp = 0;
if(position < 0 || position > Count())
return -1;
if(Count() > maxCount)
return -1;
else
{
_places.add(position, loco);
return position;
}
}
public T Remove(int position)
{
if (position >= Count() || position < 0)
return null;
else
{
_places.remove(position);
}
return _places.get(position);
}
public T Get(int position)
{
if (position < 0 || position >= Count()) return null;
return _places.get(position);
}
public Iterable<T> GetLocomotives(final Integer maxLocomotives) {
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() &&
(maxLocomotives == null || count < maxLocomotives);
}
//ну и, соответственно, переходим к следующему, если хэз некст == true
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}