Changeset 29822 in osm for applications/viewer/jmapviewer/src
- Timestamp:
- 2013-08-04T15:18:31+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/MemoryTileCache.java
r29731 r29822 3 3 //License: GPL. Copyright 2008 by Jan Peter Stotz 4 4 5 import java.util.Hashtable; 5 import java.util.HashMap; 6 import java.util.Map; 6 7 import java.util.logging.Logger; 7 8 … … 25 26 protected int cacheSize = 200; 26 27 27 protected Hashtable<String, CacheEntry> hashtable;28 protected final Map<String, CacheEntry> hash; 28 29 29 30 /** 30 31 * List of all tiles in their last recently used order 31 32 */ 32 protected CacheLinkedListElement lruTiles;33 protected final CacheLinkedListElement lruTiles; 33 34 34 35 public MemoryTileCache() { 35 hash table = new Hashtable<String, CacheEntry>(cacheSize);36 hash = new HashMap<String, CacheEntry>(cacheSize); 36 37 lruTiles = new CacheLinkedListElement(); 37 38 } 38 39 39 40 @Override 40 public void addTile(Tile tile) {41 public synchronized void addTile(Tile tile) { 41 42 CacheEntry entry = createCacheEntry(tile); 42 hashtable.put(tile.getKey(), entry); 43 lruTiles.addFirst(entry); 44 if (hashtable.size() > cacheSize) 45 removeOldEntries(); 43 hash.put(tile.getKey(), entry); 44 lruTiles.addFirst(entry); 45 if (hash.size() > cacheSize) { 46 removeOldEntries(); 47 } 46 48 } 47 49 48 50 @Override 49 public Tile getTile(TileSource source, int x, int y, int z) {50 CacheEntry entry = hash table.get(Tile.getTileKey(source, x, y, z));51 public synchronized Tile getTile(TileSource source, int x, int y, int z) { 52 CacheEntry entry = hash.get(Tile.getTileKey(source, x, y, z)); 51 53 if (entry == null) 52 54 return null; … … 61 63 * Removes the least recently used tiles 62 64 */ 63 protected void removeOldEntries() { 64 synchronized (lruTiles) { 65 try { 66 while (lruTiles.getElementCount() > cacheSize) { 67 removeEntry(lruTiles.getLastElement()); 68 } 69 } catch (Exception e) { 70 log.warning(e.getMessage()); 71 } 72 } 73 } 74 75 protected void removeEntry(CacheEntry entry) { 76 hashtable.remove(entry.tile.getKey()); 65 protected synchronized void removeOldEntries() { 66 try { 67 while (lruTiles.getElementCount() > cacheSize) { 68 removeEntry(lruTiles.getLastElement()); 69 } 70 } catch (Exception e) { 71 log.warning(e.getMessage()); 72 } 73 } 74 75 protected synchronized void removeEntry(CacheEntry entry) { 76 hash.remove(entry.tile.getKey()); 77 77 lruTiles.removeEntry(entry); 78 78 } … … 85 85 * Clears the cache deleting all tiles from memory 86 86 */ 87 public void clear() { 88 synchronized (lruTiles) { 89 hashtable.clear(); 90 lruTiles.clear(); 91 } 87 public synchronized void clear() { 88 hash.clear(); 89 lruTiles.clear(); 92 90 } 93 91 94 92 @Override 95 93 public int getTileCount() { 96 return hash table.size();94 return hash.size(); 97 95 } 98 96 … … 107 105 * new maximum number of tiles 108 106 */ 109 public void setCacheSize(int cacheSize) {107 public synchronized void setCacheSize(int cacheSize) { 110 108 this.cacheSize = cacheSize; 111 if (hash table.size() > cacheSize)109 if (hash.size() > cacheSize) 112 110 removeOldEntries(); 113 111 } … … 157 155 } 158 156 159 public synchronizedvoid clear() {157 public void clear() { 160 158 elementCount = 0; 161 159 firstElement = null; … … 168 166 * @param element new element to be added 169 167 */ 170 public synchronizedvoid addFirst(CacheEntry element) {168 public void addFirst(CacheEntry element) { 171 169 if (element == null) return; 172 170 if (elementCount == 0) { … … 189 187 * @param element element to be removed 190 188 */ 191 public synchronizedvoid removeEntry(CacheEntry element) {189 public void removeEntry(CacheEntry element) { 192 190 if (element == null) return; 193 191 if (element.next != null) { … … 206 204 } 207 205 208 public synchronizedvoid moveElementToFirstPos(CacheEntry entry) {206 public void moveElementToFirstPos(CacheEntry entry) { 209 207 if (firstElement == entry) 210 208 return;
Note:
See TracChangeset
for help on using the changeset viewer.