001/**
002 * 
003 */
004package icy.image.cache;
005
006import java.util.ArrayList;
007import java.util.Collection;
008import java.util.HashMap;
009import java.util.List;
010import java.util.Map;
011
012import icy.image.IcyBufferedImage;
013import icy.preferences.ApplicationPreferences;
014
015/**
016 * Image Cache static util class.<br>
017 * The cache store and return 1D array data corresponding to the internal {@link IcyBufferedImage#getDataXY(int)} image data.
018 * 
019 * @author Stephane
020 */
021public class ImageCache
022{
023    public final static AbstractCache cache = new EHCache2(ApplicationPreferences.getCacheMemoryMB(),
024            ApplicationPreferences.getCachePath() + "/icy_cache");
025
026    private static Integer getKey(IcyBufferedImage image)
027    {
028        return Integer.valueOf(System.identityHashCode(image));
029    }
030
031    private static IcyBufferedImage getImage(Integer key)
032    {
033        return IcyBufferedImage.getIcyBufferedImage(key);
034    }
035
036    private static Collection<IcyBufferedImage> getImages(Collection<Integer> keys, boolean getNull)
037    {
038        final List<IcyBufferedImage> result = new ArrayList<IcyBufferedImage>(keys.size());
039
040        for (Integer key : keys)
041        {
042            final IcyBufferedImage image = getImage(key);
043
044            if (getNull || (image != null))
045                result.add(image);
046        }
047
048        return result;
049    }
050    
051    public static boolean isEnabled()
052    {
053        return cache.isEnabled();
054    }
055
056    public static boolean isInCache(IcyBufferedImage image)
057    {
058        return cache.isInCache(getKey(image));
059    }
060
061    public static boolean isOnMemoryCache(IcyBufferedImage image)
062    {
063        return cache.isOnMemoryCache(getKey(image));
064    }
065
066    public static boolean isOnDiskCache(IcyBufferedImage image)
067    {
068        return cache.isOnDiskCache(getKey(image));
069    }
070
071    /**
072     * Return used memory for cache (in bytes)
073     */
074    public static long usedMemory()
075    {
076        return cache.usedMemory();
077    }
078
079    /**
080     * Return used disk space for cache (in bytes)
081     */
082    public static long usedDisk()
083    {
084        return cache.usedDisk();
085    }
086
087    /**
088     * Get all data {@link IcyBufferedImage} in the cache
089     */
090    public static Collection<IcyBufferedImage> getAllKeys() throws CacheException
091    {
092        return getImages(cache.getAllKeys(), false);
093    }
094
095    /**
096     * Get the corresponding data array (2D native array) from cache from a given {@link IcyBufferedImage}
097     */
098    public static Object get(IcyBufferedImage key) throws CacheException
099    {
100        return cache.get(getKey(key));
101    }
102
103    /**
104     * Get all data array from cache from a given Collection of {@link IcyBufferedImage}
105     */
106    public static Map<IcyBufferedImage, Object> get(Collection<IcyBufferedImage> keys) throws CacheException
107    {
108        final Map<IcyBufferedImage, Object> result = new HashMap<IcyBufferedImage, Object>();
109
110        for (IcyBufferedImage key : keys)
111            result.put(key, get(key));
112
113        return result;
114    }
115
116    /**
117     * Put the specified data array (2D native array) into cache with its associated key
118     */
119    public static void set(IcyBufferedImage key, Object object, boolean eternal) throws CacheException
120    {
121        cache.set(getKey(key), object, eternal);
122    }
123
124    /**
125     * Remove an object from the cache from its key
126     */
127    public static void remove(IcyBufferedImage key) throws CacheException
128    {
129        cache.remove(getKey(key));
130    }
131
132    /**
133     * Call it when you're done with the cache (release all resources and cleanup)
134     */
135    public static void end()
136    {
137        cache.end();
138    }
139}