KickJava   Java API By Example, From Geeks To Geeks.

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


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
13 import org.eclipse.jdt.core.IJavaElement;
14 import org.eclipse.jdt.core.JavaModelException;
15 import org.eclipse.jdt.internal.core.util.LRUCache;
16
17 /**
18  * An LRU cache of <code>JavaElements</code>.
19  */

20 public class ElementCache extends OverflowingLRUCache {
21     
22     IJavaElement spaceLimitParent = null;
23     
24 /**
25  * Constructs a new element cache of the given size.
26  */

27 public ElementCache(int size) {
28     super(size);
29 }
30 /**
31  * Constructs a new element cache of the given size.
32  */

33 public ElementCache(int size, int overflow) {
34     super(size, overflow);
35 }
36 /**
37  * Returns true if the element is successfully closed and
38  * removed from the cache, otherwise false.
39  *
40  * <p>NOTE: this triggers an external removal of this element
41  * by closing the element.
42  */

43 protected boolean close(LRUCacheEntry entry) {
44     Openable element = (Openable) entry._fKey;
45     try {
46         if (!element.canBeRemovedFromCache()) {
47             return false;
48         } else {
49             // We must close an entire JarPackageFragmentRoot at once.
50
if (element instanceof JarPackageFragment) {
51                 JarPackageFragment packageFragment= (JarPackageFragment) element;
52                 JarPackageFragmentRoot root = (JarPackageFragmentRoot) packageFragment.getParent();
53                 root.close();
54             } else {
55                 element.close();
56             }
57             return true;
58         }
59     } catch (JavaModelException npe) {
60         return false;
61     }
62 }
63
64 /*
65  * Ensures that there is enough room for adding the given number of children.
66  * If the space limit must be increased, record the parent that needed this space limit.
67  */

68 protected void ensureSpaceLimit(int childrenSize, IJavaElement parent) {
69     // ensure the children can be put without closing other elements
70
int spaceNeeded = 1 + (int)((1 + fLoadFactor) * (childrenSize + fOverflow));
71     if (fSpaceLimit < spaceNeeded) {
72         // parent is being opened with more children than the space limit
73
shrink(); // remove overflow
74
setSpaceLimit(spaceNeeded);
75         this.spaceLimitParent = parent;
76     }
77 }
78
79 /*
80  * Returns a new instance of the receiver.
81  */

82 protected LRUCache newInstance(int size, int overflow) {
83     return new ElementCache(size, overflow);
84 }
85
86 /*
87  * If the given parent was the one that increased the space limit, reset
88  * the space limit to the given default value.
89  */

90 protected void resetSpaceLimit(int defaultLimit, IJavaElement parent) {
91     if (parent.equals(this.spaceLimitParent)) {
92         setSpaceLimit(defaultLimit);
93         this.spaceLimitParent = null;
94     }
95 }
96
97 }
98
Popular Tags