1 11 package org.eclipse.jdt.internal.core; 12 13 import java.text.NumberFormat ; 14 import java.util.Enumeration ; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.jdt.core.IBuffer; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IOpenable; 21 22 26 public class BufferManager { 27 28 protected static BufferManager DEFAULT_BUFFER_MANAGER; 29 protected static boolean VERBOSE; 30 31 35 private BufferCache openBuffers = new BufferCache(60); 36 37 40 protected org.eclipse.jdt.core.IBufferFactory defaultBufferFactory = new org.eclipse.jdt.core.IBufferFactory() { 41 44 public IBuffer createBuffer(IOpenable owner) { 45 return BufferManager.createBuffer(owner); 46 } 47 }; 48 49 52 protected void addBuffer(IBuffer buffer) { 53 if (VERBOSE) { 54 String owner = ((Openable)buffer.getOwner()).toStringWithAncestors(); 55 System.out.println("Adding buffer for " + owner); } 57 synchronized (this.openBuffers) { 58 this.openBuffers.put(buffer.getOwner(), buffer); 59 } 60 this.openBuffers.closeBuffers(); 62 if (VERBOSE) { 63 System.out.println("-> Buffer cache filling ratio = " + NumberFormat.getInstance().format(this.openBuffers.fillingRatio()) + "%"); } 65 } 66 public static IBuffer createBuffer(IOpenable owner) { 67 IJavaElement element = (IJavaElement)owner; 68 IResource resource = element.getResource(); 69 return 70 new Buffer( 71 resource instanceof IFile ? (IFile)resource : null, 72 owner, 73 element.isReadOnly()); 74 } 75 public static IBuffer createNullBuffer(IOpenable owner) { 76 IJavaElement element = (IJavaElement)owner; 77 IResource resource = element.getResource(); 78 return 79 new NullBuffer( 80 resource instanceof IFile ? (IFile)resource : null, 81 owner, 82 element.isReadOnly()); 83 } 84 89 public IBuffer getBuffer(IOpenable owner) { 90 synchronized (this.openBuffers) { 91 return (IBuffer)this.openBuffers.get(owner); 92 } 93 } 94 97 public synchronized static BufferManager getDefaultBufferManager() { 98 if (DEFAULT_BUFFER_MANAGER == null) { 99 DEFAULT_BUFFER_MANAGER = new BufferManager(); 100 } 101 return DEFAULT_BUFFER_MANAGER; 102 } 103 107 public org.eclipse.jdt.core.IBufferFactory getDefaultBufferFactory() { 108 return this.defaultBufferFactory; 109 } 110 118 public Enumeration getOpenBuffers() { 119 Enumeration result; 120 synchronized (this.openBuffers) { 121 this.openBuffers.shrink(); 122 result = this.openBuffers.elements(); 123 } 124 this.openBuffers.closeBuffers(); 126 return result; 127 } 128 129 132 protected void removeBuffer(IBuffer buffer) { 133 if (VERBOSE) { 134 String owner = ((Openable)buffer.getOwner()).toStringWithAncestors(); 135 System.out.println("Removing buffer for " + owner); } 137 synchronized (this.openBuffers) { 138 this.openBuffers.remove(buffer.getOwner()); 139 } 140 this.openBuffers.closeBuffers(); 142 if (VERBOSE) { 143 System.out.println("-> Buffer cache filling ratio = " + NumberFormat.getInstance().format(this.openBuffers.fillingRatio()) + "%"); } 145 } 146 } 147 | Popular Tags |