/** * Version 1.0 is to handle single variable 2DSquareCelled raster data. * Copyright (C) 2005 Andy Turner, CCG, University of Leeds, UK. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ package uk.ac.leeds.ccg.andyt.grids.core; import java.util.HashMap; import java.util.Iterator; import java.util.NoSuchElementException; import uk.ac.leeds.ccg.andyt.grids.utilities.AbstractIterator; /** * For iterating through the values in a Grid2DSquareCellInt instance. The * values are not returned in any particular order. */ public class Grid2DSquareCellIntIterator extends AbstractIterator { private Iterator grid2DSquareCellIntHashMapIterator; private AbstractGrid2DSquareCellIntChunk grid2DSquareCellIntChunk; private AbstractIterator grid2DSquareCellIntChunkIterator; /** Creates a new instance of Grid2DSquareIntIterator */ public Grid2DSquareCellIntIterator() {} /** * Creates a new instance of Grid2DSquareIntIterator * @param grid2DSquareCellInt The Grid2DSquareCellInt to iterate over. */ public Grid2DSquareCellIntIterator( Grid2DSquareCellInt grid2DSquareCellInt ) { this.grid2DSquareCellIntHashMapIterator = grid2DSquareCellInt._ChunkID_AbstractGrid2DSquareCellChunk_HashMap.values().iterator(); if ( grid2DSquareCellIntHashMapIterator.hasNext() ) { this.grid2DSquareCellIntChunk = ( AbstractGrid2DSquareCellIntChunk ) this.grid2DSquareCellIntHashMapIterator.next(); initGrid2DSquareCellIntChunkIterator(); } } /** * Initialises this.grid2DSquareCellIntChunkIterator from this.grid2DSquareCellIntChunk */ private void initGrid2DSquareCellIntChunkIterator() { try { this.grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunk64CellMapIterator( ( Grid2DSquareCellIntChunk64CellMap ) this.grid2DSquareCellIntChunk ); } catch ( ClassCastException cce0 ) { try { this.grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunkArrayIterator( ( Grid2DSquareCellIntChunkArray ) this.grid2DSquareCellIntChunk ); } catch ( ClassCastException cce1 ) { try { this.grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunkJAIIterator( ( Grid2DSquareCellIntChunkJAI ) this.grid2DSquareCellIntChunk ); } catch ( ClassCastException cce2 ) { this.grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunkMapIterator( ( Grid2DSquareCellIntChunkMap ) this.grid2DSquareCellIntChunk ); } } } } /** * Initialises this.grid2DSquareCellIntChunkIterator from this.grid2DSquareCellIntChunk */ public AbstractIterator initGrid2DSquareCellIntChunkIterator( AbstractGrid2DSquareCellIntChunk grid2DSquareCellIntChunk ) { AbstractIterator grid2DSquareCellIntChunkIterator; try { grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunk64CellMapIterator( ( Grid2DSquareCellIntChunk64CellMap ) grid2DSquareCellIntChunk ); } catch ( ClassCastException cce0 ) { try { grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunkArrayIterator( ( Grid2DSquareCellIntChunkArray ) grid2DSquareCellIntChunk ); } catch ( ClassCastException cce1 ) { try { grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunkJAIIterator( ( Grid2DSquareCellIntChunkJAI ) grid2DSquareCellIntChunk ); } catch ( ClassCastException cce2 ) { grid2DSquareCellIntChunkIterator = new Grid2DSquareCellIntChunkMapIterator( ( Grid2DSquareCellIntChunkMap ) grid2DSquareCellIntChunk ); } } } return grid2DSquareCellIntChunkIterator; } /** * Returns true if the iteration has more elements. (In other * words, returns true if next would return an element * rather than throwing an exception.) * * @return true if the iterator has more elements. */ @Override public boolean hasNext() { if ( this.grid2DSquareCellIntChunkIterator.hasNext() ) { return true; } else { while ( grid2DSquareCellIntHashMapIterator.hasNext() ) { AbstractGrid2DSquareCellIntChunk grid2DSquareCellIntChunk = ( AbstractGrid2DSquareCellIntChunk ) this.grid2DSquareCellIntHashMapIterator.next(); AbstractIterator grid2DSquareCellIntChunkIterator = initGrid2DSquareCellIntChunkIterator( grid2DSquareCellIntChunk ); if ( grid2DSquareCellIntChunkIterator.hasNext() ) { return true; } } } return false; } /** * Returns the next element in the iteration. * * @return the next element in the iteration. * @exception NoSuchElementException iteration has no more elements. */ @Override public Object next() { try { if ( this.grid2DSquareCellIntChunkIterator.hasNext() ) { return this.grid2DSquareCellIntChunkIterator.next(); } else { while ( grid2DSquareCellIntHashMapIterator.hasNext() ) { this.grid2DSquareCellIntChunk = ( AbstractGrid2DSquareCellIntChunk ) this.grid2DSquareCellIntHashMapIterator.next(); this.grid2DSquareCellIntChunkIterator = initGrid2DSquareCellIntChunkIterator( grid2DSquareCellIntChunk ); if ( this.grid2DSquareCellIntChunkIterator.hasNext() ) { return this.grid2DSquareCellIntChunkIterator.next(); } } } } catch ( NoSuchElementException nsee0 ) { nsee0.printStackTrace(); System.out.println( nsee0.toString() ); } return null; } /** * * Removes from the underlying collection the last element returned by the * iterator (optional operation). This method can be called only once per * call to next. The behavior of an iterator is unspecified if * the underlying collection is modified while the iteration is in * progress in any way other than by calling this method. * * @exception UnsupportedOperationException if the remove * operation is not supported by this Iterator. * * @exception IllegalStateException if the next method has not * yet been called, or the remove method has already * been called after the last call to the next * method. */ @Override public void remove() {} }