diff --git a/laba1Loco/SetGeneric.java b/laba1Loco/SetGeneric.java index 1583f6c..d592135 100644 --- a/laba1Loco/SetGeneric.java +++ b/laba1Loco/SetGeneric.java @@ -2,90 +2,120 @@ package laba1Loco; import java.util.ArrayList; import java.util.Iterator; +import java.util.NoSuchElementException; public class SetGeneric { /// - /// Массив объектов, которые храним - /// - private ArrayList_places; - /// - /// Количество объектов в массиве - /// - public int Count () { return _places.size();}; - /// - /// Максимальное количество объектов в списке - /// - private int _maxCount; - /// - /// Конструктор - /// - /// - public SetGeneric(int count) - { - _maxCount = count; - _places = new ArrayList(count); - } - /// - /// Добавление объекта в набор - /// - /// Добавляемый поезд - /// - public int Insert(T train) - { - if (_places.size() >= _maxCount) - return -1; - _places.add(0, train); - return 0; - } - /// - /// Добавление объекта в набор на конкретную позицию - /// - /// Добавляемый поезд - /// Позиция - /// - public boolean Insert(T train, int position) - { - if (_places.size() >= _maxCount) - return false; + /// Массив объектов, которые храним + /// + private ArrayList_places; + /// + /// Количество объектов в массиве + /// + public int Count () { return _places.size();}; + /// + /// Максимальное количество объектов в списке + /// + private int _maxCount; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _maxCount = count; + _places = new ArrayList(count); + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый поезд + /// + public int Insert(T train) + { + if (_places.size() >= _maxCount) + return -1; + _places.add(0, train); + return 0; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый поезд + /// Позиция + /// + public boolean Insert(T train, int position) + { + if (_places.size() >= _maxCount) + return false; - if (position < 0 || position > _places.size()) - return false; + if (position < 0 || position > _places.size()) + return false; - if (position == _places.size()) - _places.add(train); - else - _places.add(position, train); + if (position == _places.size()) + _places.add(train); + else + _places.add(position, train); - return true; - } - /// - /// Удаление объекта из набора с конкретной позиции - /// - /// - /// - public boolean Remove(int position) - { - if (position < 0 || position >= _places.size()) - return false; - _places.remove(position); - return true; - } - /// - /// Получение объекта из набора по позиции - /// - /// - /// - public T Get(int position) - { - if (position < 0 || position >= _places.size()) - return null; - return _places.get(position); - } - /// - /// Проход по списку - /// - /// - public Iterator iterator() { - return _places.iterator(); - } + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public boolean Remove(int position) + { + if (position < 0 || position >= _places.size()) + return false; + _places.remove(position); + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= _places.size()) + return null; + return _places.get(position); + } + + /// + /// Проход по списку + /// + /// + + public Iterable GetTrains(final Integer maxTrains) { + return new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + 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(); + } + }; + } + }; + } } diff --git a/laba1Loco/TrainsGenericCollection.java b/laba1Loco/TrainsGenericCollection.java index 23324ad..12461bc 100644 --- a/laba1Loco/TrainsGenericCollection.java +++ b/laba1Loco/TrainsGenericCollection.java @@ -112,17 +112,18 @@ public class TrainsGenericCollection private void DrawObjects(Graphics2D g) { - for (int i = 0; i < _collection.Count(); i++) + int i = 0; + for (T train : _collection.GetTrains(100)) { - T t = _collection.Get(i); - if (t != null) + if (train != null) { - t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); - if (t instanceof DrawingLoco) - ((DrawingLoco) t).DrawTransport(g); + train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (train instanceof DrawingLoco) + ((DrawingLoco)train).DrawTransport(g); else - t.DrawTransport(g); + train.DrawTransport(g); } + i++; } } }