// (c): Tuomas J. Lukka package gzz.gfx.gl; import gzz.client.GraphicsAPI; /** A papermill instance that caches a predetermined * number of papers. */ public class CachingPaperMill extends PaperMill { private PaperMill realPaperMill; private IntCache realCache; private IntCache optCache; public CachingPaperMill(PaperMill realPaperMill, int n) { this.realPaperMill = realPaperMill; this.realCache = new IntCache(n); this.optCache = new IntCache(n); } private class IntCache { private int[] keys; private Paper[] values; int ind(int n) { int i = n % keys.length; if(i < 0) i += keys.length; return i; } IntCache(int n) { keys = new int[n]; values = new Paper[n]; } Paper get(int val) { int i = ind(val); if(keys[i] == val) return values[i]; return null; } void put(int val, Paper p) { int i = ind(val); keys[i] = val; values[i] = p; } } public Paper getOptimizedPaper(int seed, GraphicsAPI.RenderingSurface w) { Paper p = optCache.get(seed); if(p == null) { p = realPaperMill.getOptimizedPaper(seed, w); optCache.put(seed, p); } return p; } public Paper getPaper(int seed) { Paper p = realCache.get(seed); if(p == null) { p = realPaperMill.getPaper(seed); realCache.put(seed, p); } return p; } }