001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside.cache;
003
004import java.net.URL;
005import java.util.HashMap;
006
007import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
008import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
009import org.openstreetmap.josm.data.imagery.TileJobOptions;
010import org.openstreetmap.josm.plugins.streetside.utils.StreetsideURL.VirtualEarth;
011
012/**
013 * Stores the downloaded pictures locally.
014 *
015 * @author nokutu
016 *
017 */
018public class StreetsideCache extends JCSCachedTileLoaderJob<String, BufferedImageCacheEntry> {
019
020        private final URL url;
021        private final String id;
022
023        /**
024         * Types of images.
025         *
026         * @author nokutu
027         */
028        public enum Type {
029                /** Full quality image */
030                FULL_IMAGE,
031                /** Low quality image */
032                THUMBNAIL
033        }
034
035        /**
036         * Main constructor.
037         *
038         * @param id
039         *          The id of the image.
040         * @param type
041         *          The type of image that must be downloaded (THUMBNAIL or
042         *          FULL_IMAGE).
043         */
044        public StreetsideCache(final String id, final Type type) {
045                super(Caches.ImageCache.getInstance().getCache(),new TileJobOptions(50000, 50000, new HashMap<String,String>(),50000l));
046
047                if (id == null || type == null) {
048                        this.id = null;
049                        url = null;
050                } else {
051                        this.id = id;
052                        url = VirtualEarth.streetsideTile(id, type == Type.THUMBNAIL);
053                }
054        }
055
056        @Override
057        public String getCacheKey() {
058                return id;
059        }
060
061        @Override
062        public URL getUrl() {
063                return url;
064        }
065
066        @Override
067        protected BufferedImageCacheEntry createCacheEntry(byte[] content) {
068                return new BufferedImageCacheEntry(content);
069        }
070
071        @Override
072        protected boolean isObjectLoadable() {
073                if (cacheData == null) {
074                        return false;
075                }
076                final byte[] content = cacheData.getContent();
077                return content != null && content.length > 0;
078        }
079}