KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > JavaModelCache


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.jdt.core.IJavaElement;
16 import org.eclipse.jdt.internal.core.util.LRUCache;
17
18 /**
19  * The cache of java elements to their respective info.
20  */

21 public class JavaModelCache {
22     public static boolean VERBOSE = false;
23
24     public static final int DEFAULT_PROJECT_SIZE = 5; // average 25552 bytes per project.
25
public static final int DEFAULT_ROOT_SIZE = 50; // average 2590 bytes per root -> maximum size : 25900*BASE_VALUE bytes
26
public static final int DEFAULT_PKG_SIZE = 500; // average 1782 bytes per pkg -> maximum size : 178200*BASE_VALUE bytes
27
public static final int DEFAULT_OPENABLE_SIZE = 500; // average 6629 bytes per openable (includes children) -> maximum size : 662900*BASE_VALUE bytes
28
public static final int DEFAULT_CHILDREN_SIZE = 500*20; // average 20 children per openable
29

30     public static final Object JavaDoc NON_EXISTING_JAR_TYPE_INFO = new Object JavaDoc();
31
32     /*
33      * The memory ratio that should be applied to the above constants.
34      */

35     protected double memoryRatio = -1;
36     
37     /**
38      * Active Java Model Info
39      */

40     protected JavaModelInfo modelInfo;
41     
42     /**
43      * Cache of open projects.
44      */

45     protected HashMap JavaDoc projectCache;
46     
47     /**
48      * Cache of open package fragment roots.
49      */

50     protected ElementCache rootCache;
51     
52     /**
53      * Cache of open package fragments
54      */

55     protected ElementCache pkgCache;
56
57     /**
58      * Cache of open compilation unit and class files
59      */

60     protected ElementCache openableCache;
61
62     /**
63      * Cache of open children of openable Java Model Java elements
64      */

65     protected Map JavaDoc childrenCache;
66     
67     /*
68      * Cache of open binary type (inside a jar) that have a non-open parent
69      */

70     protected LRUCache jarTypeCache;
71     
72 public JavaModelCache() {
73     // set the size of the caches in function of the maximum amount of memory available
74
double ratio = getMemoryRatio();
75     this.projectCache = new HashMap JavaDoc(DEFAULT_PROJECT_SIZE); // NB: Don't use a LRUCache for projects as they are constantly reopened (e.g. during delta processing)
76
if (VERBOSE) {
77         this.rootCache = new VerboseElementCache((int) (DEFAULT_ROOT_SIZE * ratio), "Root cache"); //$NON-NLS-1$
78
this.pkgCache = new VerboseElementCache((int) (DEFAULT_PKG_SIZE * ratio), "Package cache"); //$NON-NLS-1$
79
this.openableCache = new VerboseElementCache((int) (DEFAULT_OPENABLE_SIZE * ratio), "Openable cache"); //$NON-NLS-1$
80
} 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 JavaDoc((int) (DEFAULT_CHILDREN_SIZE * ratio));
86     resetJarTypeCache();
87 }
88
89 /**
90  * Returns the info for the element.
91  */

92 public Object JavaDoc 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 JavaDoc 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         // if max memory is infinite, set the ratio to 4d which corresponds to the 256MB that Eclipse defaults to
120
// (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=111299)
121
this.memoryRatio = maxMemory == Long.MAX_VALUE ? 4d : ((double) maxMemory) / (64 * 0x100000); // 64MB is the base memory for most JVM
122
}
123     return this.memoryRatio;
124 }
125
126 /**
127  * Returns the info for this element without
128  * disturbing the cache ordering.
129  */

130 protected Object JavaDoc 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 JavaDoc 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 /**
155  * Remember the info for the element.
156  */

157 protected void putInfo(IJavaElement element, Object JavaDoc 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 /**
183  * Removes the info of the element from the cache.
184  */

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 JavaDoc toString() {
214     return toStringFillingRation(""); //$NON-NLS-1$
215
}
216 public String JavaDoc toStringFillingRation(String JavaDoc prefix) {
217     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
218     buffer.append(prefix);
219     buffer.append("Project cache: "); //$NON-NLS-1$
220
buffer.append(this.projectCache.size());
221     buffer.append(" projects\n"); //$NON-NLS-1$
222
buffer.append(prefix);
223     buffer.append(this.rootCache.toStringFillingRation("Root cache")); //$NON-NLS-1$
224
buffer.append('\n');
225     buffer.append(prefix);
226     buffer.append(this.pkgCache.toStringFillingRation("Package cache")); //$NON-NLS-1$
227
buffer.append('\n');
228     buffer.append(prefix);
229     buffer.append(this.openableCache.toStringFillingRation("Openable cache")); //$NON-NLS-1$
230
buffer.append('\n');
231     buffer.append(prefix);
232     buffer.append(this.jarTypeCache.toStringFillingRation("Jar type cache")); //$NON-NLS-1$
233
buffer.append('\n');
234     return buffer.toString();
235 }
236 }
237
Popular Tags