1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery.vectortile.mapbox;
|
---|
3 |
|
---|
4 | import java.util.concurrent.ThreadPoolExecutor;
|
---|
5 |
|
---|
6 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
7 | import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
|
---|
8 | import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
|
---|
9 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
12 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
13 | import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
|
---|
14 | import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
|
---|
15 | import org.openstreetmap.josm.data.imagery.TileJobOptions;
|
---|
16 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
17 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
18 |
|
---|
19 | import org.apache.commons.jcs3.access.behavior.ICacheAccess;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * A TileLoader class for MVT tiles
|
---|
23 | * @author Taylor Smock
|
---|
24 | * @since xxx
|
---|
25 | */
|
---|
26 | public class MapboxVectorCachedTileLoader implements TileLoader, CachedTileLoader {
|
---|
27 | protected final ICacheAccess<String, BufferedImageCacheEntry> cache;
|
---|
28 | protected final TileLoaderListener listener;
|
---|
29 | protected final TileJobOptions options;
|
---|
30 | private static final IntegerProperty THREAD_LIMIT =
|
---|
31 | new IntegerProperty("imagery.vector.mvtloader.maxjobs", TMSCachedTileLoader.THREAD_LIMIT.getDefaultValue());
|
---|
32 | private static final ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER =
|
---|
33 | TMSCachedTileLoader.getNewThreadPoolExecutor("MVT-downloader-%d", THREAD_LIMIT.get());
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructor
|
---|
37 | * @param listener called when tile loading has finished
|
---|
38 | * @param cache of the cache
|
---|
39 | * @param options tile job options
|
---|
40 | */
|
---|
41 | public MapboxVectorCachedTileLoader(TileLoaderListener listener, ICacheAccess<String, BufferedImageCacheEntry> cache,
|
---|
42 | TileJobOptions options) {
|
---|
43 | CheckParameterUtil.ensureParameterNotNull(cache, "cache");
|
---|
44 | this.cache = cache;
|
---|
45 | this.options = options;
|
---|
46 | this.listener = listener;
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void clearCache(TileSource source) {
|
---|
51 | this.cache.remove(source.getName() + ':');
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public TileJob createTileLoaderJob(Tile tile) {
|
---|
56 | return new MapboxVectorCachedTileLoaderJob(
|
---|
57 | listener,
|
---|
58 | tile,
|
---|
59 | cache,
|
---|
60 | options,
|
---|
61 | getDownloadExecutor());
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void cancelOutstandingTasks() {
|
---|
66 | final ThreadPoolExecutor executor = getDownloadExecutor();
|
---|
67 | executor.getQueue().stream().filter(executor::remove).filter(MapboxVectorCachedTileLoaderJob.class::isInstance)
|
---|
68 | .map(MapboxVectorCachedTileLoaderJob.class::cast).forEach(JCSCachedTileLoaderJob::handleJobCancellation);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public boolean hasOutstandingTasks() {
|
---|
73 | return getDownloadExecutor().getTaskCount() > getDownloadExecutor().getCompletedTaskCount();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private static ThreadPoolExecutor getDownloadExecutor() {
|
---|
77 | return DEFAULT_DOWNLOAD_JOB_DISPATCHER;
|
---|
78 | }
|
---|
79 | }
|
---|