KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > rendered > TileMap


1 /*
2
3    Copyright 2001,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 package org.apache.batik.ext.awt.image.rendered;
19
20 import java.awt.Point JavaDoc;
21 import java.awt.image.Raster JavaDoc;
22 import java.lang.ref.SoftReference JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.apache.batik.util.CleanerThread;
26 import org.apache.batik.util.HaltingThread;
27
28 public class TileMap implements TileStore {
29     private static final boolean DEBUG = false;
30     private static final boolean COUNT = false;
31
32     private HashMap JavaDoc rasters=new HashMap JavaDoc();
33
34     static class TileMapLRUMember extends TileLRUMember {
35         public Point JavaDoc pt;
36         public SoftReference JavaDoc parent;
37
38         class RasterSoftRef extends CleanerThread.SoftReferenceCleared {
39             RasterSoftRef(Object JavaDoc o) { super(o); }
40             public void cleared() {
41                 if (DEBUG) System.err.println("Cleaned: " + this);
42                 TileMap tm = (TileMap)parent.get();
43                 if (tm != null)
44                     tm.rasters.remove(pt);
45             }
46         }
47
48         TileMapLRUMember(TileMap parent, Point JavaDoc pt, Raster JavaDoc ras) {
49             super(ras);
50             this.parent = new SoftReference JavaDoc(parent);
51             this.pt = pt;
52         }
53
54         public void setRaster(Raster JavaDoc ras) {
55             hRaster = ras;
56             wRaster = new RasterSoftRef(ras);
57         }
58     }
59
60     private TileGenerator source = null;
61     private LRUCache cache = null;
62
63     public TileMap(TileGenerator source,
64            LRUCache cache) {
65         this.cache = cache;
66         this.source = source;
67     }
68
69     public void setTile(int x, int y, Raster JavaDoc ras) {
70         Point JavaDoc pt = new Point JavaDoc(x, y);
71
72         if (ras == null) {
73             // Clearing entry...
74
Object JavaDoc o = rasters.remove(pt);
75             if (o != null)
76                 cache.remove((TileMapLRUMember)o);
77             return;
78         }
79
80         Object JavaDoc o = rasters.get(pt);
81         TileMapLRUMember item;
82         if (o == null) {
83             item = new TileMapLRUMember(this, pt, ras);
84             rasters.put(pt, item);
85         } else {
86             item = (TileMapLRUMember)o;
87             item.setRaster(ras);
88         }
89         
90         cache.add(item);
91         if (DEBUG) System.out.println("Setting: (" + x + ", " + y + ")");
92     }
93
94     // Returns Raster if the tile is _currently_ in the cache.
95
// If it is not currently in the cache it returns null.
96
public Raster JavaDoc getTileNoCompute(int x, int y) {
97         Point JavaDoc pt = new Point JavaDoc(x, y);
98         Object JavaDoc o = rasters.get(pt);
99         if (o == null)
100             return null;
101
102         TileMapLRUMember item = (TileMapLRUMember)o;
103         Raster JavaDoc ret = item.retrieveRaster();
104         if (ret != null)
105             cache.add(item);
106         return ret;
107     }
108
109     public Raster JavaDoc getTile(int x, int y) {
110         if (DEBUG) System.out.println("Fetching: (" + (x) + ", " +
111                                       (y) + ")");
112         if (COUNT) synchronized (TileMap.class) { requests++; }
113
114         Raster JavaDoc ras = null;
115         Point JavaDoc pt = new Point JavaDoc(x, y);
116         Object JavaDoc o = rasters.get(pt);
117         TileMapLRUMember item = null;
118         if (o != null) {
119             item = (TileMapLRUMember)o;
120             ras = item.retrieveRaster();
121         }
122         
123         if (ras == null) {
124             if (DEBUG) System.out.println("Generating: ("+(x)+", "+
125                                           (y) + ")");
126             if (COUNT) synchronized (TileMap.class) { misses++; }
127             ras = source.genTile(x, y);
128
129             // In all likelyhood the contents of this tile is junk!
130
// So don't cache it (returning is probably fine since it
131
// shouldn't come back to haunt us...)
132
if (HaltingThread.hasBeenHalted())
133                 return ras;
134
135             if (item != null)
136                 item.setRaster(ras);
137             else {
138                 item = new TileMapLRUMember(this, pt, ras);
139                 rasters.put(pt, item);
140             }
141         }
142
143         // Update the item's position in the cache..
144
cache.add(item);
145
146         return ras;
147     }
148
149     static int requests;
150     static int misses;
151 }
152
Popular Tags