Refactory: remove unused classes, imports, fields and methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / BufferManager.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.Enumeration;
14
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;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22
23 /**
24  * The buffer manager manages the set of open buffers. It implements an LRU
25  * cache of buffers.
26  */
27 public class BufferManager implements IBufferFactory {
28
29         protected static BufferManager DEFAULT_BUFFER_MANAGER;
30
31         /**
32          * LRU cache of buffers. The key and value for an entry in the table is the
33          * identical buffer.
34          */
35         protected OverflowingLRUCache openBuffers = new BufferCache(60);
36
37         /**
38          * Creates a new buffer manager.
39          */
40         public BufferManager() {
41         }
42
43         /**
44          * Adds a buffer to the table of open buffers.
45          */
46         protected void addBuffer(IBuffer buffer) {
47                 openBuffers.put(buffer.getOwner(), buffer);
48         }
49
50         /**
51          * @see IBufferFactory#createBuffer(IOpenable)
52          */
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());
58         }
59
60         /**
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
63          * with it.
64          */
65         public IBuffer getBuffer(IOpenable owner) {
66                 return (IBuffer) openBuffers.get(owner);
67         }
68
69         /**
70          * Returns the default buffer manager.
71          */
72         public synchronized static BufferManager getDefaultBufferManager() {
73                 if (DEFAULT_BUFFER_MANAGER == null) {
74                         DEFAULT_BUFFER_MANAGER = new BufferManager();
75                 }
76                 return DEFAULT_BUFFER_MANAGER;
77         }
78
79         /**
80          * Returns the default buffer factory.
81          */
82         public IBufferFactory getDefaultBufferFactory() {
83                 return this;
84         }
85
86         /**
87          * Returns an enumeration of all open buffers.
88          * <p>
89          * The <code>Enumeration</code> answered is thread safe.
90          * 
91          * @see OverflowingLRUCache
92          * @return Enumeration of IBuffer
93          */
94         public Enumeration getOpenBuffers() {
95                 synchronized (openBuffers) {
96                         openBuffers.shrink();
97                         return openBuffers.elements();
98                 }
99         }
100
101         /**
102          * Removes a buffer from the table of open buffers.
103          */
104         protected void removeBuffer(IBuffer buffer) {
105                 openBuffers.remove(buffer.getOwner());
106         }
107 }