/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* ImageCache - A fixed pixel count sized cache of Images keyed by arbitrary set of arguments. All images are held with
* SoftReferences so they will be dropped by the GC if heap memory gets tight. When our size hits max pixel count least
* recently requested images are removed first.
*
* @author Created by Jasper Potts (Aug 7, 2007)
*/
class ImageCache {
// Ordered Map keyed by args hash, ordered by most recent accessed entry.
// Maximum number of pixels to cache, this is used if maxCount
private final int maxPixelCount;
// Maximum cached image size in pxiels
private final int maxSingleImagePixelSize;
// The current number of pixels stored in the cache
// Lock for concurrent access to map
// Reference queue for tracking lost softreferences to images in the cache
// Singleton Instance
/** Get static singleton instance */
return instance;
}
public ImageCache() {
}
this.maxPixelCount = maxPixelCount;
}
/** Clear the cache */
public void flush() {
try {
} finally {
}
}
/**
* Check if the image size is to big to be stored in the cache
*
* @param w The image width
* @param h The image height
* @return True if the image size is less than max
*/
public boolean isImageCachable(int w, int h) {
return (w * h) < maxSingleImagePixelSize;
}
/**
* Get the cached image for given keys
*
* @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
* @param w The image width, used as part of cache key
* @param h The image height, used as part of cache key
* @param args Other arguments to use as part of the cache key
* @return Returns the cached Image, or null there is no cached image for key
*/
try {
// check reference has not been lost and the key truly matches, in case of false positive hash match
} else {
return null;
}
} finally {
}
}
/**
* Sets the cached image for the specified constraints.
*
* @param image The image to store in cache
* @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
* @param w The image width, used as part of cache key
* @param h The image height, used as part of cache key
* @param args Other arguments to use as part of the cache key
* @return true if the image could be cached or false if the image is too big
*/
if (!isImageCachable(w, h)) return false;
try {
// check if currently in map
return true;
}
// clear out old
}
// add new image to pixel count
// clean out lost references if not enough space
if (currentPixelCount > maxPixelCount) {
//reference lost
}
}
// remove old items till there is enough free space
if (currentPixelCount > maxPixelCount) {
}
}
// finaly put new in map
map.put(hash, new PixelCountSoftReference(image, referenceQueue, newPixelCount,hash, config, w, h, args));
return true;
} finally {
}
}
/** Create a unique hash from all the input */
int hash;
return hash;
}
/** Extended SoftReference that stores the pixel count even after the image is lost */
private final int pixelCount;
private final int hash;
// key parts
private final int w;
private final int h;
public PixelCountSoftReference(Image referent, ReferenceQueue<? super Image> q, int pixelCount, int hash,
super(referent, q);
this.pixelCount = pixelCount;
this.w = w;
this.h = h;
}
w == this.w &&
h == this.h &&
}
}
}