KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 java.text.NumberFormat JavaDoc;
14 import java.util.Enumeration JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.jdt.core.IBuffer;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IOpenable;
21
22 /**
23  * The buffer manager manages the set of open buffers.
24  * It implements an LRU cache of buffers.
25  */

26 public class BufferManager {
27
28     protected static BufferManager DEFAULT_BUFFER_MANAGER;
29     protected static boolean VERBOSE;
30
31     /**
32      * LRU cache of buffers. The key and value for an entry
33      * in the table is the identical buffer.
34      */

35     private BufferCache openBuffers = new BufferCache(60);
36     
37     /**
38      * @deprecated
39      */

40     protected org.eclipse.jdt.core.IBufferFactory defaultBufferFactory = new org.eclipse.jdt.core.IBufferFactory() {
41         /**
42          * @deprecated
43          */

44         public IBuffer createBuffer(IOpenable owner) {
45             return BufferManager.createBuffer(owner);
46         }
47     };
48
49 /**
50  * Adds a buffer to the table of open buffers.
51  */

52 protected void addBuffer(IBuffer buffer) {
53     if (VERBOSE) {
54         String JavaDoc owner = ((Openable)buffer.getOwner()).toStringWithAncestors();
55         System.out.println("Adding buffer for " + owner); //$NON-NLS-1$
56
}
57     synchronized (this.openBuffers) {
58         this.openBuffers.put(buffer.getOwner(), buffer);
59     }
60     // close buffers that were removed from the cache if space was needed
61
this.openBuffers.closeBuffers();
62     if (VERBOSE) {
63         System.out.println("-> Buffer cache filling ratio = " + NumberFormat.getInstance().format(this.openBuffers.fillingRatio()) + "%"); //$NON-NLS-1$//$NON-NLS-2$
64
}
65 }
66 public static IBuffer createBuffer(IOpenable owner) {
67     IJavaElement element = (IJavaElement)owner;
68     IResource resource = element.getResource();
69     return
70         new Buffer(
71             resource instanceof IFile ? (IFile)resource : null,
72             owner,
73             element.isReadOnly());
74 }
75 public static IBuffer createNullBuffer(IOpenable owner) {
76     IJavaElement element = (IJavaElement)owner;
77     IResource resource = element.getResource();
78     return
79         new NullBuffer(
80             resource instanceof IFile ? (IFile)resource : null,
81             owner,
82             element.isReadOnly());
83 }
84 /**
85  * Returns the open buffer associated with the given owner,
86  * or <code>null</code> if the owner does not have an open
87  * buffer associated with it.
88  */

89 public IBuffer getBuffer(IOpenable owner) {
90     synchronized (this.openBuffers) {
91         return (IBuffer)this.openBuffers.get(owner);
92     }
93 }
94 /**
95  * Returns the default buffer manager.
96  */

97 public synchronized static BufferManager getDefaultBufferManager() {
98     if (DEFAULT_BUFFER_MANAGER == null) {
99         DEFAULT_BUFFER_MANAGER = new BufferManager();
100     }
101     return DEFAULT_BUFFER_MANAGER;
102 }
103 /**
104  * Returns the default buffer factory.
105  * @deprecated
106  */

107 public org.eclipse.jdt.core.IBufferFactory getDefaultBufferFactory() {
108     return this.defaultBufferFactory;
109 }
110 /**
111  * Returns an enumeration of all open buffers.
112  * <p>
113  * The <code>Enumeration</code> answered is thread safe.
114  *
115  * @see OverflowingLRUCache
116  * @return Enumeration of IBuffer
117  */

118 public Enumeration JavaDoc getOpenBuffers() {
119     Enumeration JavaDoc result;
120     synchronized (this.openBuffers) {
121         this.openBuffers.shrink();
122         result = this.openBuffers.elements();
123     }
124     // close buffers that were removed from the cache if space was needed
125
this.openBuffers.closeBuffers();
126     return result;
127 }
128
129 /**
130  * Removes a buffer from the table of open buffers.
131  */

132 protected void removeBuffer(IBuffer buffer) {
133     if (VERBOSE) {
134         String JavaDoc owner = ((Openable)buffer.getOwner()).toStringWithAncestors();
135         System.out.println("Removing buffer for " + owner); //$NON-NLS-1$
136
}
137     synchronized (this.openBuffers) {
138         this.openBuffers.remove(buffer.getOwner());
139     }
140     // close buffers that were removed from the cache (should be only one)
141
this.openBuffers.closeBuffers();
142     if (VERBOSE) {
143         System.out.println("-> Buffer cache filling ratio = " + NumberFormat.getInstance().format(this.openBuffers.fillingRatio()) + "%"); //$NON-NLS-1$//$NON-NLS-2$
144
}
145 }
146 }
147
Popular Tags