1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12 import java.util.HashMap;
15 import net.sourceforge.phpdt.core.IJavaElement;
18 * The cache of java elements to their respective info.
20 public class JavaModelCache {
21 public static final int PKG_CACHE_SIZE = 500;
22 public static final int OPENABLE_CACHE_SIZE = 2000;
25 * Active Java Model Info
27 protected JavaModelInfo modelInfo;
30 * Cache of open projects and package fragment roots.
32 protected Map projectAndRootCache;
35 * Cache of open package fragments
37 protected Map pkgCache;
40 * Cache of open compilation unit and class files
42 protected OverflowingLRUCache openableCache;
45 * Cache of open children of openable Java Model Java elements
47 protected Map childrenCache;
49 public JavaModelCache() {
50 this.projectAndRootCache = new HashMap(50);
51 this.pkgCache = new HashMap(PKG_CACHE_SIZE);
52 this.openableCache = new ElementCache(OPENABLE_CACHE_SIZE);
53 this.childrenCache = new HashMap(OPENABLE_CACHE_SIZE*20); // average 20 chilren per openable
56 public double openableFillingRatio() {
57 return this.openableCache.fillingRatio();
59 public int pkgSize() {
60 return this.pkgCache.size();
64 * Returns the info for the element.
66 public Object getInfo(IJavaElement element) {
67 switch (element.getElementType()) {
68 case IJavaElement.JAVA_MODEL:
69 return this.modelInfo;
70 case IJavaElement.JAVA_PROJECT:
71 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
72 return this.projectAndRootCache.get(element);
73 case IJavaElement.PACKAGE_FRAGMENT:
74 return this.pkgCache.get(element);
75 case IJavaElement.COMPILATION_UNIT:
76 case IJavaElement.CLASS_FILE:
77 return this.openableCache.get(element);
79 return this.childrenCache.get(element);
84 * Returns the info for this element without
85 * disturbing the cache ordering.
87 protected Object peekAtInfo(IJavaElement element) {
88 switch (element.getElementType()) {
89 case IJavaElement.JAVA_MODEL:
90 return this.modelInfo;
91 case IJavaElement.JAVA_PROJECT:
92 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
93 return this.projectAndRootCache.get(element);
94 case IJavaElement.PACKAGE_FRAGMENT:
95 return this.pkgCache.get(element);
96 case IJavaElement.COMPILATION_UNIT:
97 case IJavaElement.CLASS_FILE:
98 return this.openableCache.peek(element);
100 return this.childrenCache.get(element);
105 * Remember the info for the element.
107 protected void putInfo(IJavaElement element, Object info) {
108 switch (element.getElementType()) {
109 case IJavaElement.JAVA_MODEL:
110 this.modelInfo = (JavaModelInfo) info;
112 case IJavaElement.JAVA_PROJECT:
113 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
114 this.projectAndRootCache.put(element, info);
116 case IJavaElement.PACKAGE_FRAGMENT:
117 this.pkgCache.put(element, info);
119 case IJavaElement.COMPILATION_UNIT:
120 case IJavaElement.CLASS_FILE:
121 this.openableCache.put(element, info);
124 this.childrenCache.put(element, info);
128 * Removes the info of the element from the cache.
130 protected void removeInfo(IJavaElement element) {
131 switch (element.getElementType()) {
132 case IJavaElement.JAVA_MODEL:
133 this.modelInfo = null;
135 case IJavaElement.JAVA_PROJECT:
136 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
137 this.projectAndRootCache.remove(element);
139 case IJavaElement.PACKAGE_FRAGMENT:
140 this.pkgCache.remove(element);
142 case IJavaElement.COMPILATION_UNIT:
143 case IJavaElement.CLASS_FILE:
144 this.openableCache.remove(element);
147 this.childrenCache.remove(element);