com.mucommander.file.icon
Class CachedFileIconProvider

java.lang.Object
  extended by com.mucommander.file.icon.CachedFileIconProvider
All Implemented Interfaces:
FileIconProvider
Direct Known Subclasses:
SwingFileIconProvider

public class CachedFileIconProvider
extends java.lang.Object
implements FileIconProvider

CachedFileIconProvider is a FileIconProvider with caching capabilities.

This class does not actually provide icons nor does it manage the contents of the cache ; it delegates these tasks to a CacheableFileIconProvider instance. All this class does is use the cache implementation to harness its befinits and take all the credit for it.
When an icon is requested, a cache lookup is performed. If a cached value is found, it is returned. If not, the icon is fetched from the underlying provider and added to the cache.

Author:
Maxence Bernard

Field Summary
protected  CacheableFileIconProvider cacheableFip
          The underlying icon provider and cache manager
protected static int cacheCapacity
          Current capacity of icon caches
static int DEFAULT_FILE_CACHE_CAPACITY
          Default capacity of icon caches
 
Constructor Summary
CachedFileIconProvider(CacheableFileIconProvider cacheableFip)
          Creates a new CachedFileIconProvider that uses the given CacheableFileIconProvider to access the cache and retrieve the icons.
 
Method Summary
static LRUCache createCache()
          Creates and returns an LRUCache instance with a capacity equal to getCacheCapacity().
static int getCacheCapacity()
          Returns the capacity of the LRUCache that caches frequently accessed icon instances.
 javax.swing.Icon getFileIcon(AbstractFile file, java.awt.Dimension preferredResolution)
          Implementation notes: this method first calls CacheableFileIconProvider.isCacheable(com.mucommander.file.AbstractFile, java.awt.Dimension) to determine if the icon cache is used.
static void setCacheCapacity(int capacity)
          Sets the capacity of the LRUCache that caches frequently accessed file instances.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cacheableFip

protected CacheableFileIconProvider cacheableFip
The underlying icon provider and cache manager


DEFAULT_FILE_CACHE_CAPACITY

public static final int DEFAULT_FILE_CACHE_CAPACITY
Default capacity of icon caches

See Also:
Constant Field Values

cacheCapacity

protected static int cacheCapacity
Current capacity of icon caches

Constructor Detail

CachedFileIconProvider

public CachedFileIconProvider(CacheableFileIconProvider cacheableFip)
Creates a new CachedFileIconProvider that uses the given CacheableFileIconProvider to access the cache and retrieve the icons.

Parameters:
cacheableFip - the underlying icon provider and cache manager
Method Detail

createCache

public static LRUCache createCache()
Creates and returns an LRUCache instance with a capacity equal to getCacheCapacity().

Returns:
an LRUCache instance

setCacheCapacity

public static void setCacheCapacity(int capacity)
Sets the capacity of the LRUCache that caches frequently accessed file instances. The more the capacity, the more frequent the cache is hit but the higher the memory usage. By default, the capacity is DEFAULT_FILE_CACHE_CAPACITY.

Note that calling this method does not change the capacity of existing caches so to be effective, this method must be called before createCache() is called.

Parameters:
capacity - the capacity of the LRU cache that caches frequently accessed icon instances
See Also:
LRUCache

getCacheCapacity

public static int getCacheCapacity()
Returns the capacity of the LRUCache that caches frequently accessed icon instances. By default, the capacity is DEFAULT_FILE_CACHE_CAPACITY.

Returns:
the capacity of the LRU cache that caches frequently accessed icon instances

getFileIcon

public javax.swing.Icon getFileIcon(AbstractFile file,
                                    java.awt.Dimension preferredResolution)
Implementation notes: this method first calls CacheableFileIconProvider.isCacheable(com.mucommander.file.AbstractFile, java.awt.Dimension) to determine if the icon cache is used.

If the file icon is cacheable, CacheableFileIconProvider.lookupCache(com.mucommander.file.AbstractFile, java.awt.Dimension) is called to look for a previously cached icon. If a value is found, it is returned. If not, getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension) is called on the CacheableFileIconProvider to retrieve the icon. This icon is then added to the cache by calling CacheableFileIconProvider.addToCache(com.mucommander.file.AbstractFile, javax.swing.Icon, java.awt.Dimension).

If the file icon is not cacheable, getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension) is simply called on the CacheableFileIconProvider and its value returned.

Specified by:
getFileIcon in interface FileIconProvider
Parameters:
file - the AbstractFile instance for which an icon is requested
preferredResolution - the preferred icon resolution
Returns:
an icon for the requested file


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard