Ignore:
Timestamp:
2017-09-11T17:59:33+02:00 (7 years ago)
Author:
bastiK
Message:

see #2212 - simplify DeepTileSet by making TileSetInfo part of TileSet

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java

    r12671 r12824  
    11431143    protected class TileSet extends TileRange {
    11441144
     1145        private volatile TileSetInfo info;
     1146
    11451147        protected TileSet(TileXY t1, TileXY t2, int zoom) {
    11461148            super(t1, t2, zoom);
     
    12691271        }
    12701272
     1273        /**
     1274         * Check if there is any tile fully loaded without error.
     1275         * @return true if there is any tile fully loaded without error
     1276         */
     1277        public boolean hasVisibleTiles() {
     1278            return getTileSetInfo().hasVisibleTiles;
     1279        }
     1280
     1281        /**
     1282         * Check if there there is a tile that is overzoomed.
     1283         * <p>
     1284         * I.e. the server response for one tile was "there is no tile here".
     1285         * This usually happens when zoomed in too much. The limit depends on
     1286         * the region, so at the edge of such a region, some tiles may be
     1287         * available and some not.
     1288         * @return true if there there is a tile that is overzoomed
     1289         */
     1290        public boolean hasOverzoomedTiles() {
     1291            return getTileSetInfo().hasOverzoomedTiles;
     1292        }
     1293
     1294        /**
     1295         * Check if there are tiles still loading.
     1296         * <p>
     1297         * This is the case if there is a tile not yet in the cache, or in the
     1298         * cache but marked as loading ({@link Tile#isLoading()}.
     1299         * @return true if there are tiles still loading
     1300         */
     1301        public boolean hasLoadingTiles() {
     1302            return getTileSetInfo().hasLoadingTiles;
     1303        }
     1304
     1305        /**
     1306         * Check if all tiles in the range are fully loaded.
     1307         * <p>
     1308         * A tile is considered to be fully loaded even if the result of loading
     1309         * the tile was an error.
     1310         * @return true if all tiles in the range are fully loaded
     1311         */
     1312        public boolean hasAllLoadedTiles() {
     1313            return getTileSetInfo().hasAllLoadedTiles;
     1314        }
     1315
     1316        private TileSetInfo getTileSetInfo() {
     1317            if (info == null) {
     1318                synchronized (this) {
     1319                    if (info == null) {
     1320                        List<Tile> allTiles = this.allExistingTiles();
     1321                        info = new TileSetInfo();
     1322                        info.hasLoadingTiles = allTiles.size() < this.size();
     1323                        info.hasAllLoadedTiles = true;
     1324                        for (Tile t : allTiles) {
     1325                            if ("no-tile".equals(t.getValue("tile-info"))) {
     1326                                info.hasOverzoomedTiles = true;
     1327                            }
     1328                            if (t.isLoaded()) {
     1329                                if (!t.hasError()) {
     1330                                    info.hasVisibleTiles = true;
     1331                                }
     1332                            } else {
     1333                                info.hasAllLoadedTiles = false;
     1334                                if (t.isLoading()) {
     1335                                    info.hasLoadingTiles = true;
     1336                                }
     1337                            }
     1338                        }
     1339                    }
     1340                }
     1341            }
     1342            return info;
     1343        }
     1344
    12711345        @Override
    12721346        public String toString() {
    12731347            return getClass().getName() + ": zoom: " + zoom + " X(" + minX + ", " + maxX + ") Y(" + minY + ", " + maxY + ") size: " + size();
    12741348        }
     1349    }
     1350
     1351    /**
     1352     * Data container to hold information about a {@link TileSet} class.
     1353     */
     1354    private static class TileSetInfo {
     1355        boolean hasVisibleTiles;
     1356        boolean hasOverzoomedTiles;
     1357        boolean hasLoadingTiles;
     1358        boolean hasAllLoadedTiles;
    12751359    }
    12761360
     
    13021386    }
    13031387
    1304     private static class TileSetInfo {
    1305         boolean hasVisibleTiles;
    1306         boolean hasOverzoomedTiles;
    1307         boolean hasLoadingTiles;
    1308         boolean hasAllLoadedTiles;
    1309     }
    1310 
    1311     private static <S extends AbstractTMSTileSource> TileSetInfo getTileSetInfo(AbstractTileSourceLayer<S>.TileSet ts) {
    1312         List<Tile> allTiles = ts.allExistingTiles();
    1313         TileSetInfo result = new TileSetInfo();
    1314         result.hasLoadingTiles = allTiles.size() < ts.size();
    1315         result.hasAllLoadedTiles = true;
    1316         for (Tile t : allTiles) {
    1317             if ("no-tile".equals(t.getValue("tile-info"))) {
    1318                 result.hasOverzoomedTiles = true;
    1319             }
    1320             if (t.isLoaded()) {
    1321                 if (!t.hasError()) {
    1322                     result.hasVisibleTiles = true;
    1323                 }
    1324             } else {
    1325                 result.hasAllLoadedTiles = false;
    1326                 if (t.isLoading()) {
    1327                     result.hasLoadingTiles = true;
    1328                 }
    1329             }
    1330         }
    1331         return result;
    1332     }
    1333 
    13341388    private class DeepTileSet {
    13351389        private final ProjectionBounds bounds;
    13361390        private final int minZoom, maxZoom;
    13371391        private final TileSet[] tileSets;
    1338         private final TileSetInfo[] tileSetInfos;
    13391392
    13401393        @SuppressWarnings("unchecked")
     
    13441397            this.maxZoom = maxZoom;
    13451398            this.tileSets = new AbstractTileSourceLayer.TileSet[maxZoom - minZoom + 1];
    1346             this.tileSetInfos = new TileSetInfo[maxZoom - minZoom + 1];
    13471399        }
    13481400
     
    13591411            }
    13601412        }
    1361 
    1362         public TileSetInfo getTileSetInfo(int zoom) {
    1363             if (zoom < minZoom)
    1364                 return new TileSetInfo();
    1365             synchronized (tileSetInfos) {
    1366                 TileSetInfo tsi = tileSetInfos[zoom-minZoom];
    1367                 if (tsi == null) {
    1368                     tsi = AbstractTileSourceLayer.getTileSetInfo(getTileSet(zoom));
    1369                     tileSetInfos[zoom-minZoom] = tsi;
    1370                 }
    1371                 return tsi;
    1372             }
    1373         }
    13741413    }
    13751414
     
    13861425
    13871426        DeepTileSet dts = new DeepTileSet(pb, getMinZoomLvl(), zoom);
    1388         TileSet ts = dts.getTileSet(zoom);
    13891427
    13901428        int displayZoomLevel = zoom;
     
    13931431        if (getDisplaySettings().isAutoZoom() && getDisplaySettings().isAutoLoad()) {
    13941432            // Auto-detection of tilesource maxzoom (currently fully works only for Bing)
    1395             TileSetInfo tsi = dts.getTileSetInfo(zoom);
    1396             if (!tsi.hasVisibleTiles && (!tsi.hasLoadingTiles || tsi.hasOverzoomedTiles)) {
     1433            TileSet ts0 = dts.getTileSet(zoom);
     1434            if (!ts0.hasVisibleTiles() && (!ts0.hasLoadingTiles() || ts0.hasOverzoomedTiles())) {
    13971435                noTilesAtZoom = true;
    13981436            }
    13991437            // Find highest zoom level with at least one visible tile
    14001438            for (int tmpZoom = zoom; tmpZoom > dts.minZoom; tmpZoom--) {
    1401                 if (dts.getTileSetInfo(tmpZoom).hasVisibleTiles) {
     1439                if (dts.getTileSet(tmpZoom).hasVisibleTiles()) {
    14021440                    displayZoomLevel = tmpZoom;
    14031441                    break;
     
    14051443            }
    14061444            // Do binary search between currentZoomLevel and displayZoomLevel
    1407             while (zoom > displayZoomLevel && !tsi.hasVisibleTiles && tsi.hasOverzoomedTiles) {
     1445            while (zoom > displayZoomLevel && !ts0.hasVisibleTiles() && ts0.hasOverzoomedTiles()) {
    14081446                zoom = (zoom + displayZoomLevel)/2;
    1409                 tsi = dts.getTileSetInfo(zoom);
     1447                ts0 = dts.getTileSet(zoom);
    14101448            }
    14111449
     
    14151453            // to make sure there're really no more zoom levels
    14161454            // loading is done in the next if section
    1417             if (zoom == displayZoomLevel && !tsi.hasLoadingTiles && zoom < dts.maxZoom) {
     1455            if (zoom == displayZoomLevel && !ts0.hasLoadingTiles() && zoom < dts.maxZoom) {
    14181456                zoom++;
    1419                 tsi = dts.getTileSetInfo(zoom);
     1457                ts0 = dts.getTileSet(zoom);
    14201458            }
    14211459            // When we have overzoomed tiles and all tiles at current zoomlevel is loaded,
    14221460            // load tiles at previovus zoomlevels until we have all tiles on screen is loaded.
    14231461            // loading is done in the next if section
    1424             while (zoom > dts.minZoom && tsi.hasOverzoomedTiles && !tsi.hasLoadingTiles) {
     1462            while (zoom > dts.minZoom && ts0.hasOverzoomedTiles() && !ts0.hasLoadingTiles()) {
    14251463                zoom--;
    1426                 tsi = dts.getTileSetInfo(zoom);
    1427             }
    1428             ts = dts.getTileSet(zoom);
     1464                ts0 = dts.getTileSet(zoom);
     1465            }
    14291466        } else if (getDisplaySettings().isAutoZoom()) {
    14301467            setZoomLevel(zoom, false);
    14311468        }
     1469        TileSet ts = dts.getTileSet(zoom);
    14321470
    14331471        // Too many tiles... refuse to download
     
    14401478        if (displayZoomLevel != zoom) {
    14411479            ts = dts.getTileSet(displayZoomLevel);
    1442             if (!dts.getTileSetInfo(displayZoomLevel).hasAllLoadedTiles && displayZoomLevel < zoom) {
     1480            if (!dts.getTileSet(displayZoomLevel).hasAllLoadedTiles() && displayZoomLevel < zoom) {
    14431481                // if we are showing tiles from lower zoom level, ensure that all tiles are loaded as they are few,
    14441482                // and should not trash the tile cache
Note: See TracChangeset for help on using the changeset viewer.