/* * This file is part of aion-emu <aion-emu.com>. * * aion-emu is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * aion-emu is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with aion-emu. If not, see <http://www.gnu.org/licenses/>. */ package com.aionemu.gameserver.utils.collections; import java.util.Iterator; import java.util.NoSuchElementException; /** * This class is representing an iterator, that is used to iterate through the collection that has format * Iterable<Iterable<V>>.<br> * * <pre> * <code> * Usage:<br> * List<List<Integer>> someList = .... * IteratorIterator<Integer> iterator = new IteratorIterator<Integer>(someList) * * OR: * * Map<Integer, Set<SomeClass>> mapOfSets = .... * IteratorIterator<SomeCLass> iterator = new IteratorIterator<SomeClass>(mapsOfSets.values()); * </code> * </pre> * * This iterator is not thread-safe. <br> * This iterator omits null values for first level collection, which means that if we have: * * <pre> * <code> * Set<Set<Integer>> setOfSets = .... * setOfSets.add(null); * setOfSets.add(someSetOfIntegers); // Where someSetsOfIntegers is a set containing 1 and 2 * * IteratorIterator<Integer> it = new IteratorIterator<Integer>(setOfSets); * </code> * </pre> * * This <code>it</code> iterator will return only 2 values ( 1 and 2 ) * * @author Luno * * @param <V> * Type of the values over which this iterator iterates * */ public class IteratorIterator<V> implements Iterator<V> { /** 1st Level iterator */ private Iterator<? extends Iterable<V>> firstLevelIterator; /** 2nd level iterator */ private Iterator<V> secondLevelIterator; /** * Constructor of <tt>IteratorIterator</tt> * * @param itit * an Iterator that iterate over Iterable<Value> */ public IteratorIterator(Iterable<? extends Iterable<V>> itit) { this.firstLevelIterator = itit.iterator(); } /** * {@inheritDoc} */ @Override public boolean hasNext() { if(secondLevelIterator != null && secondLevelIterator.hasNext()) return true; while(firstLevelIterator.hasNext()) { Iterable<V> iterable = firstLevelIterator.next(); if (iterable != null) { secondLevelIterator = iterable.iterator(); if(secondLevelIterator.hasNext()) return true; } } return false; } /** * Returns next value of collection.<br> * If there is no next value, then {@link NoSuchElementException} thrown. */ @Override public V next() { if (secondLevelIterator == null || !secondLevelIterator.hasNext()) throw new NoSuchElementException(); return secondLevelIterator.next(); } /** * <font color="red"><b>NOT IMPLEMENTED</b></font> */ @Override public void remove() { throw new UnsupportedOperationException("This operation is not supported."); } }