mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-17 21:25:58 +02:00
Cosmetics.
This commit is contained in:
parent
3f8ba3ef71
commit
678dae7850
1 changed files with 111 additions and 119 deletions
|
@ -10,8 +10,6 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.util;
|
package org.eclipse.cdt.internal.core.util;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
|
@ -43,7 +41,6 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
* @see LRUCache
|
* @see LRUCache
|
||||||
*/
|
*/
|
||||||
protected static class LRUCacheEntry<K, T> {
|
protected static class LRUCacheEntry<K, T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hash table key
|
* Hash table key
|
||||||
*/
|
*/
|
||||||
|
@ -89,7 +86,6 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
return "LRUCacheEntry [" + _fKey + "-->" + _fValue + "]"; //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-2$
|
return "LRUCacheEntry [" + _fKey + "-->" + _fValue + "]"; //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,25 +124,26 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
* Default amount of space in the cache
|
* Default amount of space in the cache
|
||||||
*/
|
*/
|
||||||
protected static final int DEFAULT_SPACELIMIT = 100;
|
protected static final int DEFAULT_SPACELIMIT = 100;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new cache. Size of cache is defined by
|
* Creates a new cache. Size of cache is defined by
|
||||||
* <code>DEFAULT_SPACELIMIT</code>.
|
* <code>DEFAULT_SPACELIMIT</code>.
|
||||||
*/
|
*/
|
||||||
public LRUCache() {
|
public LRUCache() {
|
||||||
|
|
||||||
this(DEFAULT_SPACELIMIT);
|
this(DEFAULT_SPACELIMIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new cache.
|
* Creates a new cache.
|
||||||
* @param size Size of Cache
|
* @param size Size of Cache
|
||||||
*/
|
*/
|
||||||
public LRUCache(int size) {
|
public LRUCache(int size) {
|
||||||
|
|
||||||
fTimestampCounter = fCurrentSpace = 0;
|
fTimestampCounter = fCurrentSpace = 0;
|
||||||
fEntryQueue = fEntryQueueTail = null;
|
fEntryQueue = fEntryQueueTail = null;
|
||||||
fEntryTable = new Hashtable<K,LRUCacheEntry<K, T>>(size);
|
fEntryTable = new Hashtable<K,LRUCacheEntry<K, T>>(size);
|
||||||
fSpaceLimit = size;
|
fSpaceLimit = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new cache containing the same contents.
|
* Returns a new cache containing the same contents.
|
||||||
*
|
*
|
||||||
|
@ -154,7 +151,6 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Object clone() {
|
public Object clone() {
|
||||||
|
|
||||||
LRUCache<K, T> newCache = newInstance(fSpaceLimit);
|
LRUCache<K, T> newCache = newInstance(fSpaceLimit);
|
||||||
LRUCacheEntry<K, T> qEntry;
|
LRUCacheEntry<K, T> qEntry;
|
||||||
|
|
||||||
|
@ -166,11 +162,11 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
}
|
}
|
||||||
return newCache;
|
return newCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flushes all entries from the cache.
|
* Flushes all entries from the cache.
|
||||||
*/
|
*/
|
||||||
public void flush() {
|
public void flush() {
|
||||||
|
|
||||||
fCurrentSpace = 0;
|
fCurrentSpace = 0;
|
||||||
LRUCacheEntry<K, T> entry = fEntryQueueTail; // Remember last entry
|
LRUCacheEntry<K, T> entry = fEntryQueueTail; // Remember last entry
|
||||||
fEntryTable = new Hashtable<K,LRUCacheEntry<K, T>>(); // Clear it out
|
fEntryTable = new Hashtable<K,LRUCacheEntry<K, T>>(); // Clear it out
|
||||||
|
@ -180,6 +176,7 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
entry = entry._fPrevious;
|
entry = entry._fPrevious;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flushes the given entry from the cache. Does nothing if entry does not
|
* Flushes the given entry from the cache. Does nothing if entry does not
|
||||||
* exist in cache.
|
* exist in cache.
|
||||||
|
@ -187,25 +184,23 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
* @param key Key of object to flush
|
* @param key Key of object to flush
|
||||||
*/
|
*/
|
||||||
public void flush(Object key) {
|
public void flush(Object key) {
|
||||||
|
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
|
||||||
LRUCacheEntry<K,T> entry;
|
|
||||||
|
|
||||||
entry = fEntryTable.get(key);
|
|
||||||
|
|
||||||
/* If entry does not exist, return */
|
/* 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.
|
* Answers the value in the cache at the given key.
|
||||||
* If the value is not in the cache, returns null
|
* If the value is not in the cache, returns null
|
||||||
*
|
*
|
||||||
* @param key Hash table key of object to retrieve
|
* @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) {
|
public Object get(Object key) {
|
||||||
|
|
||||||
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
|
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -214,25 +209,28 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
this.updateTimestamp(entry);
|
this.updateTimestamp(entry);
|
||||||
return entry._fValue;
|
return entry._fValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the amount of space that is current used in the cache.
|
* Returns the amount of space that is current used in the cache.
|
||||||
*/
|
*/
|
||||||
public int getCurrentSpace() {
|
public int getCurrentSpace() {
|
||||||
return fCurrentSpace;
|
return fCurrentSpace;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the maximum amount of space available in the cache.
|
* Returns the maximum amount of space available in the cache.
|
||||||
*/
|
*/
|
||||||
public int getSpaceLimit() {
|
public int getSpaceLimit() {
|
||||||
return fSpaceLimit;
|
return fSpaceLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an Enumeration of the keys currently in the cache.
|
* Returns an Enumeration of the keys currently in the cache.
|
||||||
*/
|
*/
|
||||||
public Enumeration<K> keys() {
|
public Enumeration<K> keys() {
|
||||||
|
|
||||||
return fEntryTable.keys();
|
return fEntryTable.keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests if this cache is empty.
|
* Tests if this cache is empty.
|
||||||
*/
|
*/
|
||||||
|
@ -248,10 +246,7 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
* @param space Amount of space to free up
|
* @param space Amount of space to free up
|
||||||
*/
|
*/
|
||||||
protected boolean makeSpace(int space) {
|
protected boolean makeSpace(int space) {
|
||||||
|
int limit = this.getSpaceLimit();
|
||||||
int limit;
|
|
||||||
|
|
||||||
limit = this.getSpaceLimit();
|
|
||||||
|
|
||||||
/* if space is already available */
|
/* if space is already available */
|
||||||
if (fCurrentSpace + space <= limit) {
|
if (fCurrentSpace + space <= limit) {
|
||||||
|
@ -269,29 +264,28 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new LRUCache instance
|
* Returns a new LRUCache instance
|
||||||
*/
|
*/
|
||||||
protected LRUCache<K, T> newInstance(int size) {
|
protected LRUCache<K, T> newInstance(int size) {
|
||||||
return new LRUCache<K, T>(size);
|
return new LRUCache<K, T>(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an entry for the given key/value/space.
|
* Adds an entry for the given key/value/space.
|
||||||
*/
|
*/
|
||||||
protected void privateAdd(K key, T value, int space) {
|
protected void privateAdd(K key, T value, int space) {
|
||||||
|
LRUCacheEntry<K, T> entry = new LRUCacheEntry<K, T>(key, value, space);
|
||||||
LRUCacheEntry<K,T> entry;
|
|
||||||
|
|
||||||
entry = new LRUCacheEntry<K,T>(key, value, space);
|
|
||||||
this.privateAddEntry(entry, false);
|
this.privateAddEntry(entry, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the given entry from the receiver.
|
* Adds the given entry from the receiver.
|
||||||
* @param shuffle Indicates whether we are just shuffling the queue
|
* @param shuffle Indicates whether we are just shuffling the queue
|
||||||
* (i.e., the entry table is left alone).
|
* (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) {
|
if (!shuffle) {
|
||||||
fEntryTable.put(entry._fKey, entry);
|
fEntryTable.put(entry._fKey, entry);
|
||||||
fCurrentSpace += entry._fSpace;
|
fCurrentSpace += entry._fSpace;
|
||||||
|
@ -310,6 +304,7 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
|
|
||||||
fEntryQueue = entry;
|
fEntryQueue = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An entry has been removed from the cache, for example because it has
|
* An entry has been removed from the cache, for example because it has
|
||||||
* fallen off the bottom of the LRU queue.
|
* fallen off the bottom of the LRU queue.
|
||||||
|
@ -318,17 +313,15 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
protected void privateNotifyDeletionFromCache(LRUCacheEntry<K, T> entry) {
|
protected void privateNotifyDeletionFromCache(LRUCacheEntry<K, T> entry) {
|
||||||
// Default is NOP.
|
// Default is NOP.
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the entry from the entry queue.
|
* Removes the entry from the entry queue.
|
||||||
* @param shuffle indicates whether we are just shuffling the queue
|
* @param shuffle indicates whether we are just shuffling the queue
|
||||||
* (i.e., the entry table is left alone).
|
* (i.e., the entry table is left alone).
|
||||||
*/
|
*/
|
||||||
protected void privateRemoveEntry(LRUCacheEntry<K, T> entry, boolean shuffle) {
|
protected void privateRemoveEntry(LRUCacheEntry<K, T> entry, boolean shuffle) {
|
||||||
|
LRUCacheEntry<K, T> previous = entry._fPrevious;
|
||||||
LRUCacheEntry<K,T> previous, next;
|
LRUCacheEntry<K, T> next = entry._fNext;
|
||||||
|
|
||||||
previous = entry._fPrevious;
|
|
||||||
next = entry._fNext;
|
|
||||||
|
|
||||||
if (!shuffle) {
|
if (!shuffle) {
|
||||||
fEntryTable.remove(entry._fKey);
|
fEntryTable.remove(entry._fKey);
|
||||||
|
@ -350,6 +343,7 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
next._fPrevious = previous;
|
next._fPrevious = previous;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value in the cache at the given key. Returns the value.
|
* 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.
|
* @return added value.
|
||||||
*/
|
*/
|
||||||
public T put(K key, T value) {
|
public T put(K key, T value) {
|
||||||
|
int oldSpace, newTotal;
|
||||||
int newSpace, oldSpace, newTotal;
|
|
||||||
LRUCacheEntry<K,T> entry;
|
|
||||||
|
|
||||||
/* Check whether there's an entry in the cache */
|
/* Check whether there's an entry in the cache */
|
||||||
newSpace = spaceFor (key, value);
|
int newSpace = spaceFor(key, value);
|
||||||
entry = fEntryTable.get (key);
|
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
|
||||||
|
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
|
/*
|
||||||
/**
|
|
||||||
* Replace the entry in the cache if it would not overflow
|
* Replace the entry in the cache if it would not overflow
|
||||||
* the cache. Otherwise flush the entry and re-add it so as
|
* the cache. Otherwise flush the entry and re-add it so as
|
||||||
* to keep cache within budget
|
* to keep cache within budget
|
||||||
|
@ -389,6 +380,7 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes and returns the value in the cache for the given key.
|
* Removes and returns the value in the cache for the given key.
|
||||||
* If the key is not in the cache, returns null.
|
* If the key is not in the cache, returns null.
|
||||||
|
@ -397,7 +389,6 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
* @return Value removed from cache.
|
* @return Value removed from cache.
|
||||||
*/
|
*/
|
||||||
public T removeKey(K key) {
|
public T removeKey(K key) {
|
||||||
|
|
||||||
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
|
LRUCacheEntry<K, T> entry = fEntryTable.get(key);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -406,6 +397,7 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
this.privateRemoveEntry(entry, false);
|
this.privateRemoveEntry(entry, false);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the maximum amount of space that the cache can store
|
* Sets the maximum amount of space that the cache can store
|
||||||
*
|
*
|
||||||
|
@ -417,26 +409,27 @@ public class LRUCache<K,T> implements Cloneable {
|
||||||
}
|
}
|
||||||
fSpaceLimit = limit;
|
fSpaceLimit = limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the space taken by the given key and value.
|
* 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) {
|
if (value instanceof ILRUCacheable) {
|
||||||
return ((ILRUCacheable) value).getCacheFootprint();
|
return ((ILRUCacheable) value).getCacheFootprint();
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a String that represents the value of this object. This method
|
* Returns a String that represents the value of this object. This method
|
||||||
* is for debugging purposes only.
|
* is for debugging purposes only.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return
|
return "LRUCache " + (fCurrentSpace * 100.0 / fSpaceLimit) + "% full\n" + //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
"LRUCache " + (fCurrentSpace * 100.0 / fSpaceLimit) + "% full\n" + //$NON-NLS-1$ //$NON-NLS-2$
|
|
||||||
this.toStringContents();
|
this.toStringContents();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a String that represents the contents of this object. This method
|
* Returns a String that represents the contents of this object. This method
|
||||||
* is for debugging purposes only.
|
* is for debugging purposes only.
|
||||||
|
@ -467,12 +460,12 @@ protected String toStringContents() {
|
||||||
}
|
}
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the timestamp for the given entry, ensuring that the queue is
|
* Updates the timestamp for the given entry, ensuring that the queue is
|
||||||
* kept in correct order. The entry must exist
|
* 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++;
|
entry._fTimestamp = fTimestampCounter++;
|
||||||
if (fEntryQueue != entry) {
|
if (fEntryQueue != entry) {
|
||||||
this.privateRemoveEntry(entry, true);
|
this.privateRemoveEntry(entry, true);
|
||||||
|
@ -480,5 +473,4 @@ protected String toStringContents() {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue