Lightweight Bartering Grid

lbg.protocol.data
Class SlottedLRUCache<K extends java.lang.Comparable<? extends K>>

java.lang.Object
  extended by lbg.protocol.data.Cache<K>
      extended by lbg.protocol.data.SlottedLRUCache<K>
Type Parameters:
K - The type of the entries
Direct Known Subclasses:
ByteBoundedSlottedLRUCache

public class SlottedLRUCache<K extends java.lang.Comparable<? extends K>>
extends Cache<K>

This class implements a cache of a size bounded in number of entries.

Author:
Xavier Dalem

Field Summary
 
Fields inherited from class lbg.protocol.data.Cache
entries, veto
 
Constructor Summary
SlottedLRUCache(int capacity)
           
 
Method Summary
 Metadata<K> add(K entry, Metadata<K> meta)
          Adds an entry to the cache.
 boolean canAdd(Metadata<K>[] entries, boolean mindVeto)
          Checks if the cache can hold all entries.
 boolean isFull()
           
 Metadata<K> remove(K entry)
          Removes an entry from the cache.
 K selectForRemoval()
          Selects an entry that could be removed, but doesn't remove it yet.
 java.lang.String toString()
          Returns a String representation of this cache
 java.lang.String toStringDetailled()
           
 
Methods inherited from class lbg.protocol.data.Cache
getCacheHit, getMetadata, getVeto, has, hasVetoOn, isEmpty, setVeto, size, vetoSize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SlottedLRUCache

public SlottedLRUCache(int capacity)
Method Detail

add

public Metadata<K> add(K entry,
                       Metadata<K> meta)
                                                          throws VetoException,
                                                                 java.lang.IllegalStateException
Adds an entry to the cache.

If the cache is full, an entry will be evicted if possible. If the entry was already present, it is updated as having been used recently (i.e. put on top of the LRU queue).

Setting meta data for an existing entry doesn't replace the existing metadata. Either Cache.getMetadata(Comparable) and modify it or remove(Comparable) then add it back.

Specified by:
add in class Cache<K extends java.lang.Comparable<? extends K>>
Parameters:
entry - The entry to add
meta - Metadata concerning this entry
Returns:
The entry that was popped, if any
Throws:
VetoException - if one entry should be popped but all are veto'ed
java.lang.IllegalStateException

canAdd

public boolean canAdd(Metadata<K>[] entries,
                      boolean mindVeto)
Description copied from class: Cache
Checks if the cache can hold all entries. The check is based on the current veto, as this method returns true if all entries not in the veto can be stored in the remaining space.

Specified by:
canAdd in class Cache<K extends java.lang.Comparable<? extends K>>
Parameters:
entries - The entries that would be added
mindVeto - true if the current veto should be taken into account. If false, it will be considered that the veto could be removed before actually inserting the data. In the latter case, adding without clearing the veto could fail even if the method returns true
Returns:
true if all entries can potentially be added.

isFull

public boolean isFull()
Specified by:
isFull in class Cache<K extends java.lang.Comparable<? extends K>>

remove

public Metadata<K> remove(K entry)
                                                             throws VetoException
Description copied from class: Cache
Removes an entry from the cache.

Overrides:
remove in class Cache<K extends java.lang.Comparable<? extends K>>
Parameters:
entry - The entry to remove
Returns:
true if the entry was in the cache
Throws:
VetoException - if the entry is veto'ed

selectForRemoval

public K selectForRemoval()
                                                             throws VetoException
Description copied from class: Cache
Selects an entry that could be removed, but doesn't remove it yet.

Specified by:
selectForRemoval in class Cache<K extends java.lang.Comparable<? extends K>>
Returns:
The entry that could be removed, or null if none can be removed but this is normal (e.g. because the cache has no limit or is empty)
Throws:
VetoException - If the veto prevents removal of any entry

toString

public java.lang.String toString()
Returns a String representation of this cache

Overrides:
toString in class java.lang.Object

toStringDetailled

public java.lang.String toStringDetailled()

Lightweight Bartering Grid

Copyright (c) 2005-2008, Cyril Briquet, parts Xavier Dalem.