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;
13 import java.util.HashMap;
16 import net.sourceforge.phpdt.core.IJavaElement;
19 * The cache of java elements to their respective info.
21 public class JavaModelCache {
22 public static final int PKG_CACHE_SIZE = 500;
24 public static final int OPENABLE_CACHE_SIZE = 2000;
27 * Active Java Model Info
29 protected JavaModelInfo modelInfo;
32 * Cache of open projects and package fragment roots.
34 protected Map projectAndRootCache;
37 * Cache of open package fragments
39 protected Map pkgCache;
42 * Cache of open compilation unit and class files
44 protected OverflowingLRUCache openableCache;
47 * Cache of open children of openable Java Model Java elements
49 protected Map childrenCache;
51 public JavaModelCache() {
52 this.projectAndRootCache = new HashMap(50);
53 this.pkgCache = new HashMap(PKG_CACHE_SIZE);
54 this.openableCache = new ElementCache(OPENABLE_CACHE_SIZE);
55 this.childrenCache = new HashMap(OPENABLE_CACHE_SIZE * 20); // average
62 public double openableFillingRatio() {
63 return this.openableCache.fillingRatio();
66 public int pkgSize() {
67 return this.pkgCache.size();
71 * Returns the info for the element.
73 public Object getInfo(IJavaElement element) {
74 switch (element.getElementType()) {
75 case IJavaElement.JAVA_MODEL:
76 return this.modelInfo;
77 case IJavaElement.JAVA_PROJECT:
78 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
79 return this.projectAndRootCache.get(element);
80 case IJavaElement.PACKAGE_FRAGMENT:
81 return this.pkgCache.get(element);
82 case IJavaElement.COMPILATION_UNIT:
83 case IJavaElement.CLASS_FILE:
84 return this.openableCache.get(element);
86 return this.childrenCache.get(element);
91 * Returns the info for this element without disturbing the cache ordering.
93 protected Object peekAtInfo(IJavaElement element) {
94 switch (element.getElementType()) {
95 case IJavaElement.JAVA_MODEL:
96 return this.modelInfo;
97 case IJavaElement.JAVA_PROJECT:
98 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
99 return this.projectAndRootCache.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.peek(element);
106 return this.childrenCache.get(element);
111 * Remember the info for the element.
113 protected void putInfo(IJavaElement element, Object info) {
114 switch (element.getElementType()) {
115 case IJavaElement.JAVA_MODEL:
116 this.modelInfo = (JavaModelInfo) info;
118 case IJavaElement.JAVA_PROJECT:
119 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
120 this.projectAndRootCache.put(element, info);
122 case IJavaElement.PACKAGE_FRAGMENT:
123 this.pkgCache.put(element, info);
125 case IJavaElement.COMPILATION_UNIT:
126 case IJavaElement.CLASS_FILE:
127 this.openableCache.put(element, info);
130 this.childrenCache.put(element, info);
135 * Removes the info of the element from the cache.
137 protected void removeInfo(IJavaElement element) {
138 switch (element.getElementType()) {
139 case IJavaElement.JAVA_MODEL:
140 this.modelInfo = null;
142 case IJavaElement.JAVA_PROJECT:
143 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
144 this.projectAndRootCache.remove(element);
146 case IJavaElement.PACKAGE_FRAGMENT:
147 this.pkgCache.remove(element);
149 case IJavaElement.COMPILATION_UNIT:
150 case IJavaElement.CLASS_FILE:
151 this.openableCache.remove(element);
154 this.childrenCache.remove(element);