Class ImageCache


  • public class ImageCache
    extends java.lang.Object
    Wrapper around ResourceManager for caching image resources.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageCache()
      Initialize this image cache.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear this cache.
      void clearLowPriorityCache()  
      private void debugPrintCacheSizes()  
      java.awt.image.BufferedImage getCachedImage​(ImageResource ir, java.awt.Dimension size, boolean grayscale, int variation)
      Get an image, checking first if it is in the cache.
      java.awt.image.BufferedImage getCachedImageOrGenerate​(java.lang.String key, java.awt.Dimension size, boolean grayscale, int variation, java.util.concurrent.Callable<java.awt.image.BufferedImage> imageCreator)  
      static ImageResource getImageResource​(java.lang.String key)
      Get an image resource by key from the resource manager.
      java.awt.image.BufferedImage getScaledImage​(java.lang.String key, float scale, boolean grayscale)
      Get the image specified by the given name, scale and grayscale.
      java.awt.image.BufferedImage getSizedImage​(java.lang.String key, java.awt.Dimension size, boolean grayscale)
      Get the image specified by the given name, size and grayscale.
      java.awt.image.BufferedImage getSizedImage​(java.lang.String key, java.awt.Dimension size, boolean grayscale, int seed)
      Get the image specified by the given name, size and grayscale.
      static long imageHash​(java.lang.String key, java.awt.Dimension size, boolean grayscale, int variation)
      Hash function for images.
      private void placeImageInCache​(long hashKey, java.awt.image.BufferedImage image)  
      private void placeImageInLowPriorityCache​(long hashKey, java.awt.image.BufferedImage image)  
      private java.awt.image.BufferedImage searchCaches​(long cacheKey)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • DEBUG_PRINT_CACHE_SIZES_TO_STDOUT

        private static final boolean DEBUG_PRINT_CACHE_SIZES_TO_STDOUT
        See Also:
        Constant Field Values
      • REPLACEMENT_IMAGE

        public static final java.lang.String REPLACEMENT_IMAGE
        See Also:
        Constant Field Values
      • cache

        private final java.util.Map<java.lang.Long,​java.awt.image.BufferedImage> cache
        A cache of scaled (and possibly greyed) images.
      • lowPriorityCache

        private final java.util.Map<java.lang.Long,​java.awt.image.BufferedImage> lowPriorityCache
      • cacheSize

        private long cacheSize
      • lowPriorityCacheSize

        private long lowPriorityCacheSize
    • Constructor Detail

      • ImageCache

        public ImageCache()
        Initialize this image cache.
    • Method Detail

      • getImageResource

        public static ImageResource getImageResource​(java.lang.String key)
        Get an image resource by key from the resource manager. Fall back to a replacement image on failure. Failure to find that is fatal. Image resource failure has already been logged.
        Parameters:
        key - The key to look up.
        Returns:
        The ImageResource found.
      • imageHash

        public static long imageHash​(java.lang.String key,
                                     java.awt.Dimension size,
                                     boolean grayscale,
                                     int variation)
        Hash function for images. We accept the standard 32-bit int hashCode of the key, and or it with 12 bits of the width and height, 6 bits for variations and one more bit for the grayscale boolean.
        Parameters:
        key - The image key.
        size - The size of the image.
        grayscale - True if grayscale.
        variation - The image variation.
        Returns:
        A unique hash of these parameters.
      • getCachedImage

        public java.awt.image.BufferedImage getCachedImage​(ImageResource ir,
                                                           java.awt.Dimension size,
                                                           boolean grayscale,
                                                           int variation)
        Get an image, checking first if it is in the cache. All requests for scaled images come through here, so this is where to maintain the image cache.
        Parameters:
        ir - The ImageResource to load from.
        size - The size of the requested image. Rescaling will be performed if necessary.
        grayscale - If true return a grayscale image.
        variation - The image variation.
        Returns:
        The image identified by resource.
      • getCachedImageOrGenerate

        public java.awt.image.BufferedImage getCachedImageOrGenerate​(java.lang.String key,
                                                                     java.awt.Dimension size,
                                                                     boolean grayscale,
                                                                     int variation,
                                                                     java.util.concurrent.Callable<java.awt.image.BufferedImage> imageCreator)
      • searchCaches

        private java.awt.image.BufferedImage searchCaches​(long cacheKey)
      • placeImageInCache

        private void placeImageInCache​(long hashKey,
                                       java.awt.image.BufferedImage image)
      • placeImageInLowPriorityCache

        private void placeImageInLowPriorityCache​(long hashKey,
                                                  java.awt.image.BufferedImage image)
      • debugPrintCacheSizes

        private void debugPrintCacheSizes()
      • getScaledImage

        public java.awt.image.BufferedImage getScaledImage​(java.lang.String key,
                                                           float scale,
                                                           boolean grayscale)
        Get the image specified by the given name, scale and grayscale.
        Parameters:
        key - The name of the resource to return.
        scale - A scaling to apply to the base image size.
        grayscale - If true return a grayscale image.
        Returns:
        The image found.
      • getSizedImage

        public java.awt.image.BufferedImage getSizedImage​(java.lang.String key,
                                                          java.awt.Dimension size,
                                                          boolean grayscale)
        Get the image specified by the given name, size and grayscale. Please, avoid using too many different sizes! For each is a scaled image cached here for a long time, which wastes memory if you are not careful.
        Parameters:
        key - The name of the resource to return.
        size - The size of the requested image.
        grayscale - If true return a grayscale image.
        Returns:
        The image found.
      • getSizedImage

        public java.awt.image.BufferedImage getSizedImage​(java.lang.String key,
                                                          java.awt.Dimension size,
                                                          boolean grayscale,
                                                          int seed)
        Get the image specified by the given name, size and grayscale. Please, avoid using too many different sizes! For each is a scaled image cached here for a long time, which wastes memory if you are not careful.
        Parameters:
        key - The name of the resource to return.
        size - The size of the requested image.
        grayscale - If true return a grayscale image.
        seed - A seed used for getting the same "random" picture every time.
        Returns:
        The image found.
      • clear

        public void clear()
        Clear this cache.
      • clearLowPriorityCache

        public void clearLowPriorityCache()