01: package tide.syntaxtree;
02:
03: import java.util.*;
04: import tide.sources.FileItem;
05:
06: /** Caches some trees, but not too much cause they require a lot of memory.
07: * NOT USED NOW...
08: */
09: public final class SyntaxTreeCache {
10: int maxTrees = 20;
11: private final List<FileItem> cache = new ArrayList<FileItem>();
12:
13: private SyntaxTreeCache() {
14: }
15:
16: private static SyntaxTreeCache instance = null;
17:
18: public static synchronized SyntaxTreeCache getInstance() {
19: if (instance == null) {
20: instance = new SyntaxTreeCache();
21: }
22: return instance;
23: }
24:
25: public void clearCache() {
26: cache.clear();
27: }
28:
29: public synchronized void addToCache(FileItem fi) {
30: if (true)
31: return; // NOT USED NOW
32:
33: if (fi.getSimplifiedSyntaxTreeIfAlreadyMade() == null) {
34: cache.remove(fi);
35: return;
36: }
37:
38: if (cache.contains(fi))
39: return;
40:
41: cache.add(0, fi);
42: checkCacheSize();
43: }
44:
45: private synchronized void checkCacheSize() {
46: while (cache.size() > maxTrees) {
47: FileItem fi = cache.get(cache.size() - 1); // last element
48: // this liberates the cached one
49: fi.setSimplifiedSyntaxTree(null);
50: cache.remove(fi);
51: }
52: }
53:
54: }
|