KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > URLImageCache


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.ext.awt.image;
20
21 import org.apache.batik.ext.awt.image.renderable.Filter;
22 import org.apache.batik.util.ParsedURL;
23 import org.apache.batik.util.SoftReferenceCache;
24
25 /**
26  * This class manages a cache of soft references to Images that
27  * we have already loaded. Adding an image is two fold.
28  * First you add the ParsedURL, this lets the cache know that someone is
29  * working on this ParsedURL. Then when the completed RenderedImage is
30  * ready you put it into the cache.<P>
31  *
32  * If someone requests a ParsedURL after it has been added but before it has
33  * been put they will be blocked until the put.
34  */

35
36 public class URLImageCache extends SoftReferenceCache{
37
38     static URLImageCache theCache = new URLImageCache();
39
40     public static URLImageCache getDefaultCache() { return theCache; }
41
42     /**
43      * Let people create there own caches.
44      */

45     public URLImageCache() { }
46
47     /**
48      * Check if <tt>request(url)</tt> will return with a Filter
49      * (not putting you on the hook for it). Note that it is possible
50      * that this will return true but between this call and the call
51      * to request the soft-reference will be cleared. So it
52      * is still possible for request to return NULL, just much less
53      * likely (you can always call 'clear' in that case).
54      */

55     public synchronized boolean isPresent(ParsedURL purl) {
56         return super.isPresentImpl(purl);
57     }
58
59     /**
60      * Check if <tt>request(url)</tt> will return immediately with the
61      * Filter. Note that it is possible that this will return
62      * true but between this call and the call to request the
63      * soft-reference will be cleared.
64      */

65     public synchronized boolean isDone(ParsedURL purl) {
66         return super.isDoneImpl(purl);
67     }
68
69     /**
70      * If this returns null then you are now 'on the hook'.
71      * to put the Filter associated with ParsedURL into the
72      * cache. */

73     public synchronized Filter request(ParsedURL purl) {
74         return (Filter)super.requestImpl(purl);
75     }
76
77     /**
78      * Clear the entry for ParsedURL.
79      * This is the easiest way to 'get off the hook'.
80      * if you didn't indend to get on it.
81      */

82     public synchronized void clear(ParsedURL purl) {
83         super.clearImpl(purl);
84     }
85
86     /**
87      * Associate bi with purl. bi is only referenced through
88      * a soft reference so don't rely on the cache to keep it
89      * around. If the map no longer contains our purl it was
90      * probably cleared or flushed since we were put on the hook
91      * for it, so in that case we will do nothing.
92      */

93     public synchronized void put(ParsedURL purl, Filter filt) {
94         super.putImpl(purl, filt);
95     }
96 }
97
Popular Tags