001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core;
011:
012: import java.text.NumberFormat;
013: import java.util.Enumeration;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.jdt.core.IBuffer;
018: import org.eclipse.jdt.core.IJavaElement;
019: import org.eclipse.jdt.core.IOpenable;
020:
021: /**
022: * The buffer manager manages the set of open buffers.
023: * It implements an LRU cache of buffers.
024: */
025: public class BufferManager {
026:
027: protected static BufferManager DEFAULT_BUFFER_MANAGER;
028: protected static boolean VERBOSE;
029:
030: /**
031: * LRU cache of buffers. The key and value for an entry
032: * in the table is the identical buffer.
033: */
034: private BufferCache openBuffers = new BufferCache(60);
035:
036: /**
037: * @deprecated
038: */
039: protected org.eclipse.jdt.core.IBufferFactory defaultBufferFactory = new org.eclipse.jdt.core.IBufferFactory() {
040: /**
041: * @deprecated
042: */
043: public IBuffer createBuffer(IOpenable owner) {
044: return BufferManager.createBuffer(owner);
045: }
046: };
047:
048: /**
049: * Adds a buffer to the table of open buffers.
050: */
051: protected void addBuffer(IBuffer buffer) {
052: if (VERBOSE) {
053: String owner = ((Openable) buffer.getOwner())
054: .toStringWithAncestors();
055: System.out.println("Adding buffer for " + owner); //$NON-NLS-1$
056: }
057: synchronized (this .openBuffers) {
058: this .openBuffers.put(buffer.getOwner(), buffer);
059: }
060: // close buffers that were removed from the cache if space was needed
061: this .openBuffers.closeBuffers();
062: if (VERBOSE) {
063: System.out
064: .println("-> Buffer cache filling ratio = " + NumberFormat.getInstance().format(this .openBuffers.fillingRatio()) + "%"); //$NON-NLS-1$//$NON-NLS-2$
065: }
066: }
067:
068: public static IBuffer createBuffer(IOpenable owner) {
069: IJavaElement element = (IJavaElement) owner;
070: IResource resource = element.getResource();
071: return new Buffer(resource instanceof IFile ? (IFile) resource
072: : null, owner, element.isReadOnly());
073: }
074:
075: public static IBuffer createNullBuffer(IOpenable owner) {
076: IJavaElement element = (IJavaElement) owner;
077: IResource resource = element.getResource();
078: return new NullBuffer(
079: resource instanceof IFile ? (IFile) resource : null,
080: owner, element.isReadOnly());
081: }
082:
083: /**
084: * Returns the open buffer associated with the given owner,
085: * or <code>null</code> if the owner does not have an open
086: * buffer associated with it.
087: */
088: public IBuffer getBuffer(IOpenable owner) {
089: synchronized (this .openBuffers) {
090: return (IBuffer) this .openBuffers.get(owner);
091: }
092: }
093:
094: /**
095: * Returns the default buffer manager.
096: */
097: public synchronized static BufferManager getDefaultBufferManager() {
098: if (DEFAULT_BUFFER_MANAGER == null) {
099: DEFAULT_BUFFER_MANAGER = new BufferManager();
100: }
101: return DEFAULT_BUFFER_MANAGER;
102: }
103:
104: /**
105: * Returns the default buffer factory.
106: * @deprecated
107: */
108: public org.eclipse.jdt.core.IBufferFactory getDefaultBufferFactory() {
109: return this .defaultBufferFactory;
110: }
111:
112: /**
113: * Returns an enumeration of all open buffers.
114: * <p>
115: * The <code>Enumeration</code> answered is thread safe.
116: *
117: * @see OverflowingLRUCache
118: * @return Enumeration of IBuffer
119: */
120: public Enumeration getOpenBuffers() {
121: Enumeration result;
122: synchronized (this .openBuffers) {
123: this .openBuffers.shrink();
124: result = this .openBuffers.elements();
125: }
126: // close buffers that were removed from the cache if space was needed
127: this .openBuffers.closeBuffers();
128: return result;
129: }
130:
131: /**
132: * Removes a buffer from the table of open buffers.
133: */
134: protected void removeBuffer(IBuffer buffer) {
135: if (VERBOSE) {
136: String owner = ((Openable) buffer.getOwner())
137: .toStringWithAncestors();
138: System.out.println("Removing buffer for " + owner); //$NON-NLS-1$
139: }
140: synchronized (this .openBuffers) {
141: this .openBuffers.remove(buffer.getOwner());
142: }
143: // close buffers that were removed from the cache (should be only one)
144: this .openBuffers.closeBuffers();
145: if (VERBOSE) {
146: System.out
147: .println("-> Buffer cache filling ratio = " + NumberFormat.getInstance().format(this .openBuffers.fillingRatio()) + "%"); //$NON-NLS-1$//$NON-NLS-2$
148: }
149: }
150: }
|