Path: blob/master/src/java.desktop/share/classes/sun/swing/ImageCache.java
41153 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.swing;2526import java.awt.GraphicsConfiguration;27import java.awt.Image;28import java.lang.ref.SoftReference;29import java.util.Iterator;30import java.util.LinkedList;3132/**33* Cache is used to cache an image based on a set of arguments.34*/35public class ImageCache {36// Maximum number of entries to cache37private int maxCount;38// The entries.39private final LinkedList<SoftReference<Entry>> entries;4041public ImageCache(int maxCount) {42this.maxCount = maxCount;43entries = new LinkedList<SoftReference<Entry>>();44}4546void setMaxCount(int maxCount) {47this.maxCount = maxCount;48}4950public void flush() {51entries.clear();52}5354private Entry getEntry(Object key, GraphicsConfiguration config,55int w, int h, Object[] args) {56Entry entry;57Iterator<SoftReference<Entry>> iter = entries.listIterator();58while (iter.hasNext()) {59SoftReference<Entry> ref = iter.next();60entry = ref.get();61if (entry == null) {62// SoftReference was invalidated, remove the entry63iter.remove();64}65else if (entry.equals(config, w, h, args)) {66// Put most recently used entries at the head67iter.remove();68entries.addFirst(ref);69return entry;70}71}72// Entry doesn't exist73entry = new Entry(config, w, h, args);74if (entries.size() >= maxCount) {75entries.removeLast();76}77entries.addFirst(new SoftReference<Entry>(entry));78return entry;79}8081/**82* Returns the cached Image, or null, for the specified arguments.83*/84public Image getImage(Object key, GraphicsConfiguration config,85int w, int h, Object[] args) {86Entry entry = getEntry(key, config, w, h, args);87return entry.getImage();88}8990/**91* Sets the cached image for the specified constraints.92*/93public void setImage(Object key, GraphicsConfiguration config,94int w, int h, Object[] args, Image image) {95Entry entry = getEntry(key, config, w, h, args);96entry.setImage(image);97}9899100/**101* Caches set of arguments and Image.102*/103private static class Entry {104private final GraphicsConfiguration config;105private final int w;106private final int h;107private final Object[] args;108private Image image;109110Entry(GraphicsConfiguration config, int w, int h, Object[] args) {111this.config = config;112this.args = args;113this.w = w;114this.h = h;115}116117public void setImage(Image image) {118this.image = image;119}120121public Image getImage() {122return image;123}124125public String toString() {126String value = super.toString() +127"[ graphicsConfig=" + config +128", image=" + image +129", w=" + w + ", h=" + h;130if (args != null) {131for (int counter = 0; counter < args.length; counter++) {132value += ", " + args[counter];133}134}135value += "]";136return value;137}138139public boolean equals(GraphicsConfiguration config,140int w, int h, Object[] args) {141if (this.w == w && this.h == h &&142((this.config != null && this.config.equals(config)) ||143(this.config == null && config == null))) {144if (this.args == null && args == null) {145return true;146}147if (this.args != null && args != null &&148this.args.length == args.length) {149for (int counter = args.length - 1; counter >= 0;150counter--) {151Object a1 = this.args[counter];152Object a2 = args[counter];153if ((a1 == null && a2 != null) ||154(a1 != null && !a1.equals(a2))) {155return false;156}157}158return true;159}160}161return false;162}163}164}165166167