Re: Библиотеку, делающую эмуляцию yield не подскажете?
От: mazurkin http://mazurkin.info
Дата: 23.07.14 14:48
Оценка: +1 :)
Зачем тут yield?

public class BFSIterator implements Iterator<BFSIterator.Item> {

    private Queue<Item> queue;

    public BFSIterator(Item root) {
        this.queue = new LinkedList<Item>();
        this.queue.add(root);
    }

    @Override
    public boolean hasNext() {
        return queue.size() > 0;
    }

    @Override
    public Item next() {
        Item item = queue.remove();
        for(Item child : item.children()) {
            queue.add(child);
        }
        return item;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();

    }

    public interface Item {

        Collection<Item> children();
    }

}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.