author | prr |
Sat, 19 Sep 2015 15:45:59 -0700 | |
changeset 32865 | f9cb6e427f9e |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package sun.swing; |
|
26 |
||
27 |
import java.awt.GraphicsConfiguration; |
|
28 |
import java.awt.Image; |
|
29 |
import java.lang.ref.SoftReference; |
|
30 |
import java.util.Iterator; |
|
31 |
import java.util.LinkedList; |
|
32 |
||
33 |
/** |
|
34 |
* Cache is used to cache an image based on a set of arguments. |
|
35 |
*/ |
|
36 |
public class ImageCache { |
|
37 |
// Maximum number of entries to cache |
|
38 |
private int maxCount; |
|
39 |
// The entries. |
|
32865
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
25859
diff
changeset
|
40 |
private final LinkedList<SoftReference<Entry>> entries; |
2 | 41 |
|
42 |
public ImageCache(int maxCount) { |
|
43 |
this.maxCount = maxCount; |
|
44 |
entries = new LinkedList<SoftReference<Entry>>(); |
|
45 |
} |
|
46 |
||
47 |
void setMaxCount(int maxCount) { |
|
48 |
this.maxCount = maxCount; |
|
49 |
} |
|
50 |
||
51 |
public void flush() { |
|
52 |
entries.clear(); |
|
53 |
} |
|
54 |
||
55 |
private Entry getEntry(Object key, GraphicsConfiguration config, |
|
56 |
int w, int h, Object[] args) { |
|
57 |
Entry entry; |
|
58 |
Iterator<SoftReference<Entry>> iter = entries.listIterator(); |
|
59 |
while (iter.hasNext()) { |
|
60 |
SoftReference<Entry> ref = iter.next(); |
|
61 |
entry = ref.get(); |
|
62 |
if (entry == null) { |
|
63 |
// SoftReference was invalidated, remove the entry |
|
64 |
iter.remove(); |
|
65 |
} |
|
66 |
else if (entry.equals(config, w, h, args)) { |
|
67 |
// Put most recently used entries at the head |
|
68 |
iter.remove(); |
|
69 |
entries.addFirst(ref); |
|
70 |
return entry; |
|
71 |
} |
|
72 |
} |
|
73 |
// Entry doesn't exist |
|
74 |
entry = new Entry(config, w, h, args); |
|
75 |
if (entries.size() >= maxCount) { |
|
76 |
entries.removeLast(); |
|
77 |
} |
|
78 |
entries.addFirst(new SoftReference<Entry>(entry)); |
|
79 |
return entry; |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Returns the cached Image, or null, for the specified arguments. |
|
84 |
*/ |
|
85 |
public Image getImage(Object key, GraphicsConfiguration config, |
|
86 |
int w, int h, Object[] args) { |
|
87 |
Entry entry = getEntry(key, config, w, h, args); |
|
88 |
return entry.getImage(); |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Sets the cached image for the specified constraints. |
|
93 |
*/ |
|
94 |
public void setImage(Object key, GraphicsConfiguration config, |
|
95 |
int w, int h, Object[] args, Image image) { |
|
96 |
Entry entry = getEntry(key, config, w, h, args); |
|
97 |
entry.setImage(image); |
|
98 |
} |
|
99 |
||
100 |
||
101 |
/** |
|
102 |
* Caches set of arguments and Image. |
|
103 |
*/ |
|
104 |
private static class Entry { |
|
32865
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
25859
diff
changeset
|
105 |
private final GraphicsConfiguration config; |
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
25859
diff
changeset
|
106 |
private final int w; |
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
25859
diff
changeset
|
107 |
private final int h; |
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
25859
diff
changeset
|
108 |
private final Object[] args; |
2 | 109 |
private Image image; |
110 |
||
111 |
Entry(GraphicsConfiguration config, int w, int h, Object[] args) { |
|
112 |
this.config = config; |
|
113 |
this.args = args; |
|
114 |
this.w = w; |
|
115 |
this.h = h; |
|
116 |
} |
|
117 |
||
118 |
public void setImage(Image image) { |
|
119 |
this.image = image; |
|
120 |
} |
|
121 |
||
122 |
public Image getImage() { |
|
123 |
return image; |
|
124 |
} |
|
125 |
||
126 |
public String toString() { |
|
127 |
String value = super.toString() + |
|
128 |
"[ graphicsConfig=" + config + |
|
129 |
", image=" + image + |
|
130 |
", w=" + w + ", h=" + h; |
|
131 |
if (args != null) { |
|
132 |
for (int counter = 0; counter < args.length; counter++) { |
|
133 |
value += ", " + args[counter]; |
|
134 |
} |
|
135 |
} |
|
136 |
value += "]"; |
|
137 |
return value; |
|
138 |
} |
|
139 |
||
140 |
public boolean equals(GraphicsConfiguration config, |
|
141 |
int w, int h, Object[] args) { |
|
142 |
if (this.w == w && this.h == h && |
|
143 |
((this.config != null && this.config.equals(config)) || |
|
144 |
(this.config == null && config == null))) { |
|
145 |
if (this.args == null && args == null) { |
|
146 |
return true; |
|
147 |
} |
|
148 |
if (this.args != null && args != null && |
|
149 |
this.args.length == args.length) { |
|
150 |
for (int counter = args.length - 1; counter >= 0; |
|
151 |
counter--) { |
|
152 |
Object a1 = this.args[counter]; |
|
153 |
Object a2 = args[counter]; |
|
154 |
if ((a1 == null && a2 != null) || |
|
155 |
(a1 != null && !a1.equals(a2))) { |
|
156 |
return false; |
|
157 |
} |
|
158 |
} |
|
159 |
return true; |
|
160 |
} |
|
161 |
} |
|
162 |
return false; |
|
163 |
} |
|
164 |
} |
|
165 |
} |