KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > BufferCache


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.jdt.core.IBuffer;
16 import org.eclipse.jdt.internal.core.util.LRUCache;
17
18 /**
19  * An LRU cache of <code>IBuffers</code>.
20  */

21 public class BufferCache extends OverflowingLRUCache {
22     
23     private ThreadLocal JavaDoc buffersToClose = new ThreadLocal JavaDoc();
24 /**
25  * Constructs a new buffer cache of the given size.
26  */

27 public BufferCache(int size) {
28     super(size);
29 }
30 /**
31  * Constructs a new buffer cache of the given size.
32  */

33 public BufferCache(int size, int overflow) {
34     super(size, overflow);
35 }
36 /**
37  * Returns true if the buffer is successfully closed and
38  * removed from the cache, otherwise false.
39  *
40  * <p>NOTE: this triggers an external removal of this buffer
41  * by closing the buffer.
42  */

43 protected boolean close(LRUCacheEntry entry) {
44     IBuffer buffer= (IBuffer) entry._fValue;
45     
46     // prevent buffer that have unsaved changes or working copy buffer to be removed
47
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=39311
48
if (!((Openable)buffer.getOwner()).canBufferBeRemovedFromCache(buffer)) {
49         return false;
50     } else {
51         ArrayList JavaDoc buffers = (ArrayList JavaDoc) this.buffersToClose.get();
52         if (buffers == null) {
53             buffers = new ArrayList JavaDoc();
54             this.buffersToClose.set(buffers);
55         }
56         buffers.add(buffer);
57         return true;
58     }
59 }
60
61 void closeBuffers() {
62     ArrayList JavaDoc buffers = (ArrayList JavaDoc) this.buffersToClose.get();
63     if (buffers == null)
64         return;
65     this.buffersToClose.set(null);
66     for (int i = 0, length = buffers.size(); i < length; i++) {
67         ((IBuffer) buffers.get(i)).close();
68     }
69 }
70     /**
71      * Returns a new instance of the reciever.
72      */

73     protected LRUCache newInstance(int size, int overflow) {
74         return new BufferCache(size, overflow);
75     }
76 }
77
Popular Tags