1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Cosmetics.

This commit is contained in:
Sergey Prigogin 2009-11-16 06:27:28 +00:00
parent 3f8ba3ef71
commit 678dae7850

View file

@ -10,13 +10,11 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.util;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* The <code>LRUCache</code> is a hashtable that stores a finite number of elements.
* The <code>LRUCache</code> is a hash table that stores a finite number of elements.
* When an attempt is made to add values to a full cache, the least recently used values
* in the cache are discarded to make room for the new values as necessary.
*
@ -32,7 +30,7 @@ import java.util.Hashtable;
*
* This class is similar to the JDT LRUCache class.
*/
public class LRUCache<K,T> implements Cloneable {
public class LRUCache<K, T> implements Cloneable {
/**
* This type is used internally by the LRUCache to represent entries
@ -42,8 +40,7 @@ public class LRUCache<K,T> implements Cloneable {
*
* @see LRUCache
*/
protected static class LRUCacheEntry<K,T> {
protected static class LRUCacheEntry<K, T> {
/**
* Hash table key
*/
@ -67,18 +64,18 @@ public class LRUCache<K,T> implements Cloneable {
/**
* Previous entry in queue
*/
public LRUCacheEntry<K,T> _fPrevious;
public LRUCacheEntry<K, T> _fPrevious;
/**
* Next entry in queue
*/
public LRUCacheEntry<K,T> _fNext;
public LRUCacheEntry<K, T> _fNext;
/**
* Creates a new instance of the receiver with the provided values
* for key, value, and space.
*/
public LRUCacheEntry (K key, T value, int space) {
public LRUCacheEntry(K key, T value, int space) {
_fKey = key;
_fValue = value;
_fSpace = space;
@ -89,7 +86,6 @@ public class LRUCache<K,T> implements Cloneable {
*/
@Override
public String toString() {
return "LRUCacheEntry [" + _fKey + "-->" + _fValue + "]"; //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-2$
}
}
@ -112,41 +108,42 @@ public class LRUCache<K,T> implements Cloneable {
/**
* Hash table for fast random access to cache entries
*/
protected Hashtable<K,LRUCacheEntry<K,T>> fEntryTable;
protected Hashtable<K,LRUCacheEntry<K, T>> fEntryTable;
/**
* Start of queue (most recently used entry)
*/
protected LRUCacheEntry<K,T> fEntryQueue;
protected LRUCacheEntry<K, T> fEntryQueue;
/**
* End of queue (least recently used entry)
*/
protected LRUCacheEntry<K,T> fEntryQueueTail;
protected LRUCacheEntry<K, T> fEntryQueueTail;
/**
* Default amount of space in the cache
*/
protected static final int DEFAULT_SPACELIMIT = 100;
/**
* Creates a new cache. Size of cache is defined by
* <code>DEFAULT_SPACELIMIT</code>.
*/
public LRUCache() {
this(DEFAULT_SPACELIMIT);
}
/**
* Creates a new cache.
* @param size Size of Cache
*/
public LRUCache(int size) {
fTimestampCounter = fCurrentSpace = 0;
fEntryQueue = fEntryQueueTail = null;
fEntryTable = new Hashtable<K,LRUCacheEntry<K,T>>(size);
fEntryTable = new Hashtable<K,LRUCacheEntry<K, T>>(size);
fSpaceLimit = size;
}
/**
* Returns a new cache containing the same contents.
*
@ -154,85 +151,86 @@ public class LRUCache<K,T> implements Cloneable {
*/
@Override
public Object clone() {
LRUCache<K,T> newCache = newInstance(fSpaceLimit);
LRUCacheEntry<K,T> qEntry;
LRUCache<K, T> newCache = newInstance(fSpaceLimit);
LRUCacheEntry<K, T> qEntry;
/* Preserve order of entries by copying from oldest to newest */
qEntry = this.fEntryQueueTail;
while (qEntry != null) {
newCache.privateAdd (qEntry._fKey, qEntry._fValue, qEntry._fSpace);
newCache.privateAdd(qEntry._fKey, qEntry._fValue, qEntry._fSpace);
qEntry = qEntry._fPrevious;
}
return newCache;
}
/**
* Flushes all entries from the cache.
*/
public void flush() {
fCurrentSpace = 0;
LRUCacheEntry<K,T> entry = fEntryQueueTail; // Remember last entry
fEntryTable = new Hashtable<K,LRUCacheEntry<K,T>>(); // Clear it out
LRUCacheEntry<K, T> entry = fEntryQueueTail; // Remember last entry
fEntryTable = new Hashtable<K,LRUCacheEntry<K, T>>(); // Clear it out
fEntryQueue = fEntryQueueTail = null;
while (entry != null) { // send deletion notifications in LRU order
privateNotifyDeletionFromCache(entry);
entry = entry._fPrevious;
}
}
/**
* Flushes the given entry from the cache. Does nothing if entry does not
* exist in cache.
*
* @param key Key of object to flush
*/
public void flush (Object key) {
LRUCacheEntry<K,T> entry;
entry = fEntryTable.get(key);
public void flush(Object key) {
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
/* If entry does not exist, return */
if (entry == null) return;
if (entry == null) {
return;
}
this.privateRemoveEntry (entry, false);
this.privateRemoveEntry(entry, false);
}
/**
* Answers the value in the cache at the given key.
* If the value is not in the cache, returns null
*
* @param key Hash table key of object to retrieve
* @return Retreived object, or null if object does not exist
* @return Retrieved object, or null if object does not exist
*/
public Object get(Object key) {
LRUCacheEntry<K,T> entry = fEntryTable.get(key);
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
if (entry == null) {
return null;
}
this.updateTimestamp (entry);
this.updateTimestamp(entry);
return entry._fValue;
}
/**
* Returns the amount of space that is current used in the cache.
*/
public int getCurrentSpace() {
return fCurrentSpace;
}
/**
* Returns the maximum amount of space available in the cache.
*/
public int getSpaceLimit() {
return fSpaceLimit;
}
/**
* Returns an Enumeration of the keys currently in the cache.
*/
public Enumeration<K> keys() {
return fEntryTable.keys();
}
/**
* Tests if this cache is empty.
*/
@ -247,11 +245,8 @@ public class LRUCache<K,T> implements Cloneable {
*
* @param space Amount of space to free up
*/
protected boolean makeSpace (int space) {
int limit;
limit = this.getSpaceLimit();
protected boolean makeSpace(int space) {
int limit = this.getSpaceLimit();
/* if space is already available */
if (fCurrentSpace + space <= limit) {
@ -265,35 +260,34 @@ public class LRUCache<K,T> implements Cloneable {
/* Free up space by removing oldest entries */
while (fCurrentSpace + space > limit && fEntryQueueTail != null) {
this.privateRemoveEntry (fEntryQueueTail, false);
this.privateRemoveEntry(fEntryQueueTail, false);
}
return true;
}
/**
* Returns a new LRUCache instance
*/
protected LRUCache<K,T> newInstance(int size) {
return new LRUCache<K,T>(size);
protected LRUCache<K, T> newInstance(int size) {
return new LRUCache<K, T>(size);
}
/**
* Adds an entry for the given key/value/space.
*/
protected void privateAdd (K key, T value, int space) {
LRUCacheEntry<K,T> entry;
entry = new LRUCacheEntry<K,T>(key, value, space);
this.privateAddEntry (entry, false);
protected void privateAdd(K key, T value, int space) {
LRUCacheEntry<K, T> entry = new LRUCacheEntry<K, T>(key, value, space);
this.privateAddEntry(entry, false);
}
/**
* Adds the given entry from the receiver.
* @param shuffle Indicates whether we are just shuffling the queue
* (i.e., the entry table is left alone).
*/
protected void privateAddEntry (LRUCacheEntry<K,T> entry, boolean shuffle) {
protected void privateAddEntry(LRUCacheEntry<K, T> entry, boolean shuffle) {
if (!shuffle) {
fEntryTable.put (entry._fKey, entry);
fEntryTable.put(entry._fKey, entry);
fCurrentSpace += entry._fSpace;
}
@ -310,25 +304,24 @@ public class LRUCache<K,T> implements Cloneable {
fEntryQueue = entry;
}
/**
* An entry has been removed from the cache, for example because it has
* fallen off the bottom of the LRU queue.
* Subclasses could over-ride this to implement a persistent cache below the LRU cache.
*/
protected void privateNotifyDeletionFromCache(LRUCacheEntry<K,T> entry) {
protected void privateNotifyDeletionFromCache(LRUCacheEntry<K, T> entry) {
// Default is NOP.
}
/**
* Removes the entry from the entry queue.
* @param shuffle indicates whether we are just shuffling the queue
* (i.e., the entry table is left alone).
*/
protected void privateRemoveEntry (LRUCacheEntry<K,T> entry, boolean shuffle) {
LRUCacheEntry<K,T> previous, next;
previous = entry._fPrevious;
next = entry._fNext;
protected void privateRemoveEntry(LRUCacheEntry<K, T> entry, boolean shuffle) {
LRUCacheEntry<K, T> previous = entry._fPrevious;
LRUCacheEntry<K, T> next = entry._fNext;
if (!shuffle) {
fEntryTable.remove(entry._fKey);
@ -350,6 +343,7 @@ public class LRUCache<K,T> implements Cloneable {
next._fPrevious = previous;
}
}
/**
* Sets the value in the cache at the given key. Returns the value.
*
@ -358,17 +352,14 @@ public class LRUCache<K,T> implements Cloneable {
* @return added value.
*/
public T put(K key, T value) {
int newSpace, oldSpace, newTotal;
LRUCacheEntry<K,T> entry;
int oldSpace, newTotal;
/* Check whether there's an entry in the cache */
newSpace = spaceFor (key, value);
entry = fEntryTable.get (key);
int newSpace = spaceFor(key, value);
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
if (entry != null) {
/**
/*
* Replace the entry in the cache if it would not overflow
* the cache. Otherwise flush the entry and re-add it so as
* to keep cache within budget
@ -376,19 +367,20 @@ public class LRUCache<K,T> implements Cloneable {
oldSpace = entry._fSpace;
newTotal = getCurrentSpace() - oldSpace + newSpace;
if (newTotal <= getSpaceLimit()) {
updateTimestamp (entry);
updateTimestamp(entry);
entry._fValue = value;
entry._fSpace = newSpace;
this.fCurrentSpace = newTotal;
return value;
}
privateRemoveEntry (entry, false);
privateRemoveEntry(entry, false);
}
if (makeSpace(newSpace)) {
privateAdd (key, value, newSpace);
privateAdd(key, value, newSpace);
}
return value;
}
/**
* Removes and returns the value in the cache for the given key.
* If the key is not in the cache, returns null.
@ -396,16 +388,16 @@ public class LRUCache<K,T> implements Cloneable {
* @param key Key of object to remove from cache.
* @return Value removed from cache.
*/
public T removeKey (K key) {
LRUCacheEntry<K,T> entry = fEntryTable.get(key);
public T removeKey(K key) {
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
if (entry == null) {
return null;
}
T value = entry._fValue;
this.privateRemoveEntry (entry, false);
this.privateRemoveEntry(entry, false);
return value;
}
/**
* Sets the maximum amount of space that the cache can store
*
@ -417,68 +409,68 @@ public class LRUCache<K,T> implements Cloneable {
}
fSpaceLimit = limit;
}
/**
* Returns the space taken by the given key and value.
*/
protected int spaceFor (Object key, Object value) {
protected int spaceFor(Object key, Object value) {
if (value instanceof ILRUCacheable) {
return ((ILRUCacheable) value).getCacheFootprint();
}
return 1;
}
/**
* Returns a String that represents the value of this object. This method
* is for debugging purposes only.
*/
@Override
public String toString() {
return
"LRUCache " + (fCurrentSpace * 100.0 / fSpaceLimit) + "% full\n" + //$NON-NLS-1$ //$NON-NLS-2$
this.toStringContents();
}
/**
* Returns a String that represents the contents of this object. This method
* is for debugging purposes only.
*/
protected String toStringContents() {
StringBuffer result = new StringBuffer();
int length = fEntryTable.size();
Object[] unsortedKeys = new Object[length];
String[] unsortedToStrings = new String[length];
Enumeration<K> e = this.keys();
for (int i = 0; i < length; i++) {
Object key = e.nextElement();
unsortedKeys[i] = key;
unsortedToStrings[i] =
(key instanceof org.eclipse.cdt.internal.core.model.CElement) ?
((org.eclipse.cdt.internal.core.model.CElement)key).getElementName() :
key.toString();
/**
* Returns a String that represents the value of this object. This method
* is for debugging purposes only.
*/
@Override
public String toString() {
return "LRUCache " + (fCurrentSpace * 100.0 / fSpaceLimit) + "% full\n" + //$NON-NLS-1$ //$NON-NLS-2$
this.toStringContents();
}
ToStringSorter sorter = new ToStringSorter();
sorter.sort(unsortedKeys, unsortedToStrings);
for (int i = 0; i < length; i++) {
String toString = sorter.sortedStrings[i];
Object value = this.get(sorter.sortedObjects[i]);
result.append(toString);
result.append(" -> "); //$NON-NLS-1$
result.append(value);
result.append("\n"); //$NON-NLS-1$
/**
* Returns a String that represents the contents of this object. This method
* is for debugging purposes only.
*/
protected String toStringContents() {
StringBuffer result = new StringBuffer();
int length = fEntryTable.size();
Object[] unsortedKeys = new Object[length];
String[] unsortedToStrings = new String[length];
Enumeration<K> e = this.keys();
for (int i = 0; i < length; i++) {
Object key = e.nextElement();
unsortedKeys[i] = key;
unsortedToStrings[i] =
(key instanceof org.eclipse.cdt.internal.core.model.CElement) ?
((org.eclipse.cdt.internal.core.model.CElement)key).getElementName() :
key.toString();
}
ToStringSorter sorter = new ToStringSorter();
sorter.sort(unsortedKeys, unsortedToStrings);
for (int i = 0; i < length; i++) {
String toString = sorter.sortedStrings[i];
Object value = this.get(sorter.sortedObjects[i]);
result.append(toString);
result.append(" -> "); //$NON-NLS-1$
result.append(value);
result.append("\n"); //$NON-NLS-1$
}
return result.toString();
}
return result.toString();
}
/**
* Updates the timestamp for the given entry, ensuring that the queue is
* kept in correct order. The entry must exist
*/
protected void updateTimestamp (LRUCacheEntry<K,T> entry) {
protected void updateTimestamp(LRUCacheEntry<K, T> entry) {
entry._fTimestamp = fTimestampCounter++;
if (fEntryQueue != entry) {
this.privateRemoveEntry (entry, true);
this.privateAddEntry (entry, true);
this.privateRemoveEntry(entry, true);
this.privateAddEntry(entry, true);
}
return;
}
}