A massive organize imports and formatting of the sources using default Eclipse code...
[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
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import net.sourceforge.phpdt.core.IJavaElement;
17
18 /**
19  * The cache of java elements to their respective info.
20  */
21 public class JavaModelCache {
22         public static final int PKG_CACHE_SIZE = 500;
23
24         public static final int OPENABLE_CACHE_SIZE = 2000;
25
26         /**
27          * Active Java Model Info
28          */
29         protected JavaModelInfo modelInfo;
30
31         /**
32          * Cache of open projects and package fragment roots.
33          */
34         protected Map projectAndRootCache;
35
36         /**
37          * Cache of open package fragments
38          */
39         protected Map pkgCache;
40
41         /**
42          * Cache of open compilation unit and class files
43          */
44         protected OverflowingLRUCache openableCache;
45
46         /**
47          * Cache of open children of openable Java Model Java elements
48          */
49         protected Map childrenCache;
50
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
56                                                                                                                                         // 20
57                                                                                                                                         // chilren
58                                                                                                                                         // per
59                                                                                                                                         // openable
60         }
61
62         public double openableFillingRatio() {
63                 return this.openableCache.fillingRatio();
64         }
65
66         public int pkgSize() {
67                 return this.pkgCache.size();
68         }
69
70         /**
71          * Returns the info for the element.
72          */
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);
85                 default:
86                         return this.childrenCache.get(element);
87                 }
88         }
89
90         /**
91          * Returns the info for this element without disturbing the cache ordering.
92          */
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);
105                 default:
106                         return this.childrenCache.get(element);
107                 }
108         }
109
110         /**
111          * Remember the info for the element.
112          */
113         protected void putInfo(IJavaElement element, Object info) {
114                 switch (element.getElementType()) {
115                 case IJavaElement.JAVA_MODEL:
116                         this.modelInfo = (JavaModelInfo) info;
117                         break;
118                 case IJavaElement.JAVA_PROJECT:
119                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
120                         this.projectAndRootCache.put(element, info);
121                         break;
122                 case IJavaElement.PACKAGE_FRAGMENT:
123                         this.pkgCache.put(element, info);
124                         break;
125                 case IJavaElement.COMPILATION_UNIT:
126                 case IJavaElement.CLASS_FILE:
127                         this.openableCache.put(element, info);
128                         break;
129                 default:
130                         this.childrenCache.put(element, info);
131                 }
132         }
133
134         /**
135          * Removes the info of the element from the cache.
136          */
137         protected void removeInfo(IJavaElement element) {
138                 switch (element.getElementType()) {
139                 case IJavaElement.JAVA_MODEL:
140                         this.modelInfo = null;
141                         break;
142                 case IJavaElement.JAVA_PROJECT:
143                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
144                         this.projectAndRootCache.remove(element);
145                         break;
146                 case IJavaElement.PACKAGE_FRAGMENT:
147                         this.pkgCache.remove(element);
148                         break;
149                 case IJavaElement.COMPILATION_UNIT:
150                 case IJavaElement.CLASS_FILE:
151                         this.openableCache.remove(element);
152                         break;
153                 default:
154                         this.childrenCache.remove(element);
155                 }
156         }
157 }