//$HeadURL$
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2010 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 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

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 Occam Labs UG (haftungsbeschränkt)
 Godesberger Allee 139, 53175 Bonn
 Germany
 http://www.occamlabs.de/

 e-mail: info@deegree.org
 ----------------------------------------------------------------------------*/
package org.deegree.tile;

import static org.slf4j.LoggerFactory.getLogger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.deegree.geometry.Envelope;
import org.slf4j.Logger;

/**
 * Default implementation of {@link TileDataSet}.
 * <p>
 * Selects tile matrices based on tile matrix metadata. Can be used in conjunction with any implementation of
 * {@link TileMatrixSet}.
 * </p>
 * 
 * @author <a href="mailto:schmitz@occamlabs.de">Andreas Schmitz</a>
 * @author last edited by: $Author: mschneider $
 * 
 * @version $Revision: 31882 $, $Date: 2011-09-15 02:05:04 +0200 (Thu, 15 Sep 2011) $
 */
public class DefaultTileDataSet implements TileDataSet {

    private static final Logger LOG = getLogger( DefaultTileDataSet.class );

    private final Map<String, TileDataLevel> levels;

    private final TileMatrixSet metadata;

    private final String format;

    /**
     * Creates a new {@link DefaultTileDataSet} instance.
     * 
     * @param levels
     *            data levels, must not be <code>null</code>
     * @param tileMatrixSet
     *            corresponding matrix set metadata, must not be <code>null</code>
     * @param format
     *            native image format, must not be <code>null</code>
     */
    public DefaultTileDataSet( List<TileDataLevel> levels, TileMatrixSet tileMatrixSet, String format ) {
        this.levels = new LinkedHashMap<String, TileDataLevel>();
        for ( TileDataLevel m : levels ) {
            this.levels.put( m.getMetadata().getIdentifier(), m );
        }
        this.metadata = tileMatrixSet;
        this.format = format;
    }

    @Override
    public Iterator<Tile> getTiles( Envelope envelope, double resolution ) {
        // select correct matrix
        Iterator<TileDataLevel> iter = levels.values().iterator();
        TileDataLevel matrix = iter.next();
        while ( matrix.getMetadata().getResolution() > resolution && iter.hasNext() ) {
            matrix = iter.next();
        }
        if ( iter.hasNext() )
            matrix = iter.next();

        final long[] idxs = Tiles.getTileIndexRange( matrix, envelope );

        if ( idxs == null ) {
            return Collections.<Tile> emptyList().iterator();
        }

        final TileDataLevel fmatrix = matrix;

        LOG.debug( "Selected tile matrix with resolution {}, from {}x{} to {}x{}.",
                   new Object[] { matrix.getMetadata().getResolution(), idxs[0], idxs[1], idxs[2], idxs[3] } );

        // fetch tiles lazily
        return new Iterator<Tile>() {
            long x = idxs[0], y = idxs[1];

            @Override
            public boolean hasNext() {
                return x <= idxs[2];
            }

            @Override
            public Tile next() {
                Tile t = fmatrix.getTile( x, y );
                if ( y == idxs[3] ) {
                    y = idxs[1];
                    ++x;
                } else {
                    ++y;
                }
                return t;
            }

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

    @Override
    public List<TileDataLevel> getTileDataLevels() {
        return new ArrayList<TileDataLevel>( levels.values() );
    }

    @Override
    public TileMatrixSet getTileMatrixSet() {
        return metadata;
    }

    @Override
    public TileDataLevel getTileDataLevel( String identifier ) {
        return levels.get( identifier );
    }

    @Override
    public String getNativeImageFormat() {
        return format;
    }

}

@@ -1639,16 +1639,42 @@
 til.Map;
+%0Aimport java.util.TreeMap;
 %0A%0Aimport
@@ -3284,16 +3284,372 @@
         
+List%3CTileDataLevel%3E levels = new ArrayList%3CTileDataLevel%3E();%0A        TreeMap%3CDouble, TileDataLevel%3E map = new TreeMap%3CDouble, TileDataLevel%3E();%0A        for ( TileDataLevel l : this.levels.values() ) %7B%0A            map.put( l.getMetadata().getResolution(), l );%0A        %7D%0A        levels.addAll( map.values() );%0A        Collections.reverse( levels );%0A        
 Iterator
@@ -3677,25 +3677,16 @@
 = levels
-.values()
 .iterato
@@ -3848,32 +3848,32 @@
  = iter.next();%0A
+
         %7D%0A      
@@ -3869,72 +3869,8 @@
    %7D
-%0A        if ( iter.hasNext() )%0A            matrix = iter.next();
 %0A%0A  

order matrix sets properly before getting tiles