122 lines
3.6 KiB
Java
122 lines
3.6 KiB
Java
package laba1Loco;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Iterator;
|
||
import java.util.NoSuchElementException;
|
||
|
||
public class SetGeneric<T extends Object> {
|
||
/// <summary>
|
||
/// Массив объектов, которые храним
|
||
/// </summary>
|
||
private ArrayList<T>_places;
|
||
/// <summary>
|
||
/// Количество объектов в массиве
|
||
/// </summary>
|
||
public int Count () { return _places.size();};
|
||
/// <summary>
|
||
/// Максимальное количество объектов в списке
|
||
/// </summary>
|
||
private int _maxCount;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="count"></param>
|
||
public SetGeneric(int count)
|
||
{
|
||
_maxCount = count;
|
||
_places = new ArrayList<T>(count);
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="train">Добавляемый поезд</param>
|
||
/// <returns></returns>
|
||
public int Insert(T train)
|
||
{
|
||
if (_places.size() >= _maxCount)
|
||
return -1;
|
||
_places.add(0, train);
|
||
return 0;
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="train">Добавляемый поезд</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public boolean Insert(T train, int position)
|
||
{
|
||
if (_places.size() >= _maxCount)
|
||
return false;
|
||
|
||
if (position < 0 || position > _places.size())
|
||
return false;
|
||
|
||
if (position == _places.size())
|
||
_places.add(train);
|
||
else
|
||
_places.add(position, train);
|
||
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public boolean Remove(int position)
|
||
{
|
||
if (position < 0 || position >= _places.size())
|
||
return false;
|
||
_places.remove(position);
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта из набора по позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T Get(int position)
|
||
{
|
||
if (position < 0 || position >= _places.size())
|
||
return null;
|
||
return _places.get(position);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Проход по списку
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
|
||
public Iterable<T> GetTrains(final Integer maxTrains) {
|
||
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() && (maxTrains == null || count < maxTrains);
|
||
}
|
||
|
||
@Override
|
||
public T next() {
|
||
if (hasNext()) {
|
||
count++;
|
||
return _places.get(currentIndex++);
|
||
}
|
||
throw new NoSuchElementException();
|
||
}
|
||
|
||
@Override
|
||
public void remove() {
|
||
throw new UnsupportedOperationException();
|
||
}
|
||
};
|
||
}
|
||
};
|
||
}
|
||
}
|