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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.Enumeration;
15 import net.sourceforge.phpdt.core.IBuffer;
16 import net.sourceforge.phpdt.core.IBufferFactory;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IOpenable;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
24 * The buffer manager manages the set of open buffers. It implements an LRU
27 public class BufferManager implements IBufferFactory {
29 protected static BufferManager DEFAULT_BUFFER_MANAGER;
32 * LRU cache of buffers. The key and value for an entry in the table is the
35 protected OverflowingLRUCache openBuffers = new BufferCache(60);
38 * Creates a new buffer manager.
40 public BufferManager() {
44 * Adds a buffer to the table of open buffers.
46 protected void addBuffer(IBuffer buffer) {
47 openBuffers.put(buffer.getOwner(), buffer);
51 * @see IBufferFactory#createBuffer(IOpenable)
53 public IBuffer createBuffer(IOpenable owner) {
54 IJavaElement element = (IJavaElement) owner;
55 IResource resource = element.getResource();
56 return new Buffer(resource instanceof IFile ? (IFile) resource : null,
57 owner, element.isReadOnly());
61 * Returns the open buffer associated with the given owner, or
62 * <code>null</code> if the owner does not have an open buffer associated
65 public IBuffer getBuffer(IOpenable owner) {
66 return (IBuffer) openBuffers.get(owner);
70 * Returns the default buffer manager.
72 public synchronized static BufferManager getDefaultBufferManager() {
73 if (DEFAULT_BUFFER_MANAGER == null) {
74 DEFAULT_BUFFER_MANAGER = new BufferManager();
76 return DEFAULT_BUFFER_MANAGER;
80 * Returns the default buffer factory.
82 public IBufferFactory getDefaultBufferFactory() {
87 * Returns an enumeration of all open buffers.
89 * The <code>Enumeration</code> answered is thread safe.
91 * @see OverflowingLRUCache
92 * @return Enumeration of IBuffer
94 public Enumeration getOpenBuffers() {
95 synchronized (openBuffers) {
97 return openBuffers.elements();
102 * Removes a buffer from the table of open buffers.
104 protected void removeBuffer(IBuffer buffer) {
105 openBuffers.remove(buffer.getOwner());