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;
25 * The buffer manager manages the set of open buffers.
26 * It implements an LRU cache of buffers.
28 public class BufferManager implements IBufferFactory {
30 protected static BufferManager DEFAULT_BUFFER_MANAGER;
33 * LRU cache of buffers. The key and value for an entry
34 * in the table is the identical buffer.
36 protected OverflowingLRUCache openBuffers = new BufferCache(60);
39 * Creates a new buffer manager.
41 public BufferManager() {
44 * Adds a buffer to the table of open buffers.
46 protected void addBuffer(IBuffer buffer) {
47 openBuffers.put(buffer.getOwner(), buffer);
50 * @see IBufferFactory#createBuffer(IOpenable)
52 public IBuffer createBuffer(IOpenable owner) {
53 IJavaElement element = (IJavaElement)owner;
54 IResource resource = element.getResource();
57 resource instanceof IFile ? (IFile)resource : null,
59 element.isReadOnly());
63 * Returns the open buffer associated with the given owner,
64 * or <code>null</code> if the owner does not have an open
65 * buffer associated with it.
67 public IBuffer getBuffer(IOpenable owner) {
68 return (IBuffer)openBuffers.get(owner);
71 * Returns the default buffer manager.
73 public synchronized static BufferManager getDefaultBufferManager() {
74 if (DEFAULT_BUFFER_MANAGER == null) {
75 DEFAULT_BUFFER_MANAGER = new BufferManager();
77 return DEFAULT_BUFFER_MANAGER;
80 * Returns the default buffer factory.
82 public IBufferFactory getDefaultBufferFactory() {
86 * Returns an enumeration of all open buffers.
88 * The <code>Enumeration</code> answered is thread safe.
90 * @see OverflowingLRUCache
91 * @return Enumeration of IBuffer
93 public Enumeration getOpenBuffers() {
94 synchronized (openBuffers) {
96 return openBuffers.elements();
102 * Removes a buffer from the table of open buffers.
104 protected void removeBuffer(IBuffer buffer) {
105 openBuffers.remove(buffer.getOwner());