1 11 package org.eclipse.jdt.internal.core; 12 import java.util.HashMap ; 13 import java.util.Map ; 14 15 import org.eclipse.jdt.core.IJavaElement; 16 import org.eclipse.jdt.internal.core.util.LRUCache; 17 18 21 public class JavaModelCache { 22 public static boolean VERBOSE = false; 23 24 public static final int DEFAULT_PROJECT_SIZE = 5; public static final int DEFAULT_ROOT_SIZE = 50; public static final int DEFAULT_PKG_SIZE = 500; public static final int DEFAULT_OPENABLE_SIZE = 500; public static final int DEFAULT_CHILDREN_SIZE = 500*20; 30 public static final Object NON_EXISTING_JAR_TYPE_INFO = new Object (); 31 32 35 protected double memoryRatio = -1; 36 37 40 protected JavaModelInfo modelInfo; 41 42 45 protected HashMap projectCache; 46 47 50 protected ElementCache rootCache; 51 52 55 protected ElementCache pkgCache; 56 57 60 protected ElementCache openableCache; 61 62 65 protected Map childrenCache; 66 67 70 protected LRUCache jarTypeCache; 71 72 public JavaModelCache() { 73 double ratio = getMemoryRatio(); 75 this.projectCache = new HashMap (DEFAULT_PROJECT_SIZE); if (VERBOSE) { 77 this.rootCache = new VerboseElementCache((int) (DEFAULT_ROOT_SIZE * ratio), "Root cache"); this.pkgCache = new VerboseElementCache((int) (DEFAULT_PKG_SIZE * ratio), "Package cache"); this.openableCache = new VerboseElementCache((int) (DEFAULT_OPENABLE_SIZE * ratio), "Openable cache"); } else { 81 this.rootCache = new ElementCache((int) (DEFAULT_ROOT_SIZE * ratio)); 82 this.pkgCache = new ElementCache((int) (DEFAULT_PKG_SIZE * ratio)); 83 this.openableCache = new ElementCache((int) (DEFAULT_OPENABLE_SIZE * ratio)); 84 } 85 this.childrenCache = new HashMap ((int) (DEFAULT_CHILDREN_SIZE * ratio)); 86 resetJarTypeCache(); 87 } 88 89 92 public Object getInfo(IJavaElement element) { 93 switch (element.getElementType()) { 94 case IJavaElement.JAVA_MODEL: 95 return this.modelInfo; 96 case IJavaElement.JAVA_PROJECT: 97 return this.projectCache.get(element); 98 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 99 return this.rootCache.get(element); 100 case IJavaElement.PACKAGE_FRAGMENT: 101 return this.pkgCache.get(element); 102 case IJavaElement.COMPILATION_UNIT: 103 case IJavaElement.CLASS_FILE: 104 return this.openableCache.get(element); 105 case IJavaElement.TYPE: 106 Object result = this.jarTypeCache.get(element); 107 if (result != null) 108 return result; 109 else 110 return this.childrenCache.get(element); 111 default: 112 return this.childrenCache.get(element); 113 } 114 } 115 116 protected double getMemoryRatio() { 117 if (this.memoryRatio == -1) { 118 long maxMemory = Runtime.getRuntime().maxMemory(); 119 this.memoryRatio = maxMemory == Long.MAX_VALUE ? 4d : ((double) maxMemory) / (64 * 0x100000); } 123 return this.memoryRatio; 124 } 125 126 130 protected Object peekAtInfo(IJavaElement element) { 131 switch (element.getElementType()) { 132 case IJavaElement.JAVA_MODEL: 133 return this.modelInfo; 134 case IJavaElement.JAVA_PROJECT: 135 return this.projectCache.get(element); 136 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 137 return this.rootCache.peek(element); 138 case IJavaElement.PACKAGE_FRAGMENT: 139 return this.pkgCache.peek(element); 140 case IJavaElement.COMPILATION_UNIT: 141 case IJavaElement.CLASS_FILE: 142 return this.openableCache.peek(element); 143 case IJavaElement.TYPE: 144 Object result = this.jarTypeCache.peek(element); 145 if (result != null) 146 return result; 147 else 148 return this.childrenCache.get(element); 149 default: 150 return this.childrenCache.get(element); 151 } 152 } 153 154 157 protected void putInfo(IJavaElement element, Object info) { 158 switch (element.getElementType()) { 159 case IJavaElement.JAVA_MODEL: 160 this.modelInfo = (JavaModelInfo) info; 161 break; 162 case IJavaElement.JAVA_PROJECT: 163 this.projectCache.put(element, info); 164 this.rootCache.ensureSpaceLimit(((JavaElementInfo) info).children.length, element); 165 break; 166 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 167 this.rootCache.put(element, info); 168 this.pkgCache.ensureSpaceLimit(((JavaElementInfo) info).children.length, element); 169 break; 170 case IJavaElement.PACKAGE_FRAGMENT: 171 this.pkgCache.put(element, info); 172 this.openableCache.ensureSpaceLimit(((JavaElementInfo) info).children.length, element); 173 break; 174 case IJavaElement.COMPILATION_UNIT: 175 case IJavaElement.CLASS_FILE: 176 this.openableCache.put(element, info); 177 break; 178 default: 179 this.childrenCache.put(element, info); 180 } 181 } 182 185 protected void removeInfo(JavaElement element) { 186 switch (element.getElementType()) { 187 case IJavaElement.JAVA_MODEL: 188 this.modelInfo = null; 189 break; 190 case IJavaElement.JAVA_PROJECT: 191 this.projectCache.remove(element); 192 this.rootCache.resetSpaceLimit((int) (DEFAULT_ROOT_SIZE * getMemoryRatio()), element); 193 break; 194 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 195 this.rootCache.remove(element); 196 this.pkgCache.resetSpaceLimit((int) (DEFAULT_PKG_SIZE * getMemoryRatio()), element); 197 break; 198 case IJavaElement.PACKAGE_FRAGMENT: 199 this.pkgCache.remove(element); 200 this.openableCache.resetSpaceLimit((int) (DEFAULT_OPENABLE_SIZE * getMemoryRatio()), element); 201 break; 202 case IJavaElement.COMPILATION_UNIT: 203 case IJavaElement.CLASS_FILE: 204 this.openableCache.remove(element); 205 break; 206 default: 207 this.childrenCache.remove(element); 208 } 209 } 210 protected void resetJarTypeCache() { 211 this.jarTypeCache = new LRUCache((int) (DEFAULT_OPENABLE_SIZE * getMemoryRatio())); 212 } 213 public String toString() { 214 return toStringFillingRation(""); } 216 public String toStringFillingRation(String prefix) { 217 StringBuffer buffer = new StringBuffer (); 218 buffer.append(prefix); 219 buffer.append("Project cache: "); buffer.append(this.projectCache.size()); 221 buffer.append(" projects\n"); buffer.append(prefix); 223 buffer.append(this.rootCache.toStringFillingRation("Root cache")); buffer.append('\n'); 225 buffer.append(prefix); 226 buffer.append(this.pkgCache.toStringFillingRation("Package cache")); buffer.append('\n'); 228 buffer.append(prefix); 229 buffer.append(this.openableCache.toStringFillingRation("Openable cache")); buffer.append('\n'); 231 buffer.append(prefix); 232 buffer.append(this.jarTypeCache.toStringFillingRation("Jar type cache")); buffer.append('\n'); 234 return buffer.toString(); 235 } 236 } 237 | Popular Tags |