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.IOpenable;
21 * The buffer manager manages the set of open buffers.
22 * It implements an LRU cache of buffers.
24 public class BufferManager implements IBufferFactory {
26 protected static BufferManager DEFAULT_BUFFER_MANAGER;
29 * LRU cache of buffers. The key and value for an entry
30 * in the table is the identical buffer.
32 protected OverflowingLRUCache openBuffers = new BufferCache(60);
35 * Creates a new buffer manager.
37 public BufferManager() {
40 * Adds a buffer to the table of open buffers.
42 protected void addBuffer(IBuffer buffer) {
43 openBuffers.put(buffer.getOwner(), buffer);
46 * @see IBufferFactory#createBuffer(IOpenable)
48 //public IBuffer createBuffer(IOpenable owner) {
49 // IJavaElement element = (IJavaElement)owner;
50 // IResource resource = element.getResource();
53 // resource instanceof IFile ? (IFile)resource : null,
55 // element.isReadOnly());
59 * Returns the open buffer associated with the given owner,
60 * or <code>null</code> if the owner does not have an open
61 * buffer associated with it.
63 public IBuffer getBuffer(IOpenable owner) {
64 return (IBuffer)openBuffers.get(owner);
67 * Returns the default buffer manager.
69 public synchronized static BufferManager getDefaultBufferManager() {
70 if (DEFAULT_BUFFER_MANAGER == null) {
71 DEFAULT_BUFFER_MANAGER = new BufferManager();
73 return DEFAULT_BUFFER_MANAGER;
76 * Returns the default buffer factory.
78 public IBufferFactory getDefaultBufferFactory() {
82 * Returns an enumeration of all open buffers.
84 * The <code>Enumeration</code> answered is thread safe.
86 * @see OverflowingLRUCache
87 * @return Enumeration of IBuffer
89 public Enumeration getOpenBuffers() {
90 synchronized (openBuffers) {
92 return openBuffers.elements();
98 * Removes a buffer from the table of open buffers.
100 protected void removeBuffer(IBuffer buffer) {
101 openBuffers.remove(buffer.getOwner());