new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaModelCache.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import net.sourceforge.phpdt.core.IJavaElement;
16
17 /**
18  * The cache of java elements to their respective info.
19  */
20 public class JavaModelCache {
21         public static final int PKG_CACHE_SIZE = 500;
22         public static final int OPENABLE_CACHE_SIZE = 2000;
23         
24         /**
25          * Active Java Model Info
26          */
27         protected JavaModelInfo modelInfo;
28         
29         /**
30          * Cache of open projects and package fragment roots.
31          */
32         protected Map projectAndRootCache;
33         
34         /**
35          * Cache of open package fragments
36          */
37         protected Map pkgCache;
38
39         /**
40          * Cache of open compilation unit and class files
41          */
42         protected OverflowingLRUCache openableCache;
43
44         /**
45          * Cache of open children of openable Java Model Java elements
46          */
47         protected Map childrenCache;
48         
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
54 }
55
56 public double openableFillingRatio() {
57         return this.openableCache.fillingRatio();
58 }
59 public int pkgSize() {
60         return this.pkgCache.size();
61 }
62         
63 /**
64  *  Returns the info for the element.
65  */
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);
78                 default:
79                         return this.childrenCache.get(element);
80         }
81 }
82
83 /**
84  *  Returns the info for this element without
85  *  disturbing the cache ordering.
86  */
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);
99                 default:
100                         return this.childrenCache.get(element);
101         }
102 }
103
104 /**
105  * Remember the info for the element.
106  */
107 protected void putInfo(IJavaElement element, Object info) {
108         switch (element.getElementType()) {
109                 case IJavaElement.JAVA_MODEL:
110                         this.modelInfo = (JavaModelInfo) info;
111                         break;
112                 case IJavaElement.JAVA_PROJECT:
113                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
114                         this.projectAndRootCache.put(element, info);
115                         break;
116                 case IJavaElement.PACKAGE_FRAGMENT:
117                         this.pkgCache.put(element, info);
118                         break;
119                 case IJavaElement.COMPILATION_UNIT:
120                 case IJavaElement.CLASS_FILE:
121                         this.openableCache.put(element, info);
122                         break;
123                 default:
124                         this.childrenCache.put(element, info);
125         }
126 }
127 /**
128  * Removes the info of the element from the cache.
129  */
130 protected void removeInfo(IJavaElement element) {
131         switch (element.getElementType()) {
132                 case IJavaElement.JAVA_MODEL:
133                         this.modelInfo = null;
134                         break;
135                 case IJavaElement.JAVA_PROJECT:
136                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
137                         this.projectAndRootCache.remove(element);
138                         break;
139                 case IJavaElement.PACKAGE_FRAGMENT:
140                         this.pkgCache.remove(element);
141                         break;
142                 case IJavaElement.COMPILATION_UNIT:
143                 case IJavaElement.CLASS_FILE:
144                         this.openableCache.remove(element);
145                         break;
146                 default:
147                         this.childrenCache.remove(element);
148         }
149 }
150 }