org.cdlib.xtf.util
Class FastIntCache

Object
  extended by FastIntCache

public class FastIntCache
extends Object

A fast but inflexible cache where the keys are integers, the size is fixed, and a crude LRU policy is enforced. Handles consecutive keys gracefully. Doesn't support resizing, deletion, or iteration (not that these operations would be hard, just that they haven't been needed so far.)

Author:
Martin Haye

Field Summary
private  IntHash newHash
           
private  IntHash oldHash
           
private  int size
           
static Tester tester
          Basic regression test
 
Constructor Summary
FastIntCache(int size)
          Construct a new cache.
 
Method Summary
 void clear()
          Clears all entries from the cache
 boolean contains(int key)
          Check whether the given key is present in the cache
 Object get(int key)
          Retrieve the value for the given key, or null if not found.
 void put(int key, Object val)
          Add a key/value pair to the cache.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

size

private int size

oldHash

private IntHash oldHash

newHash

private IntHash newHash

tester

public static final Tester tester
Basic regression test

Constructor Detail

FastIntCache

public FastIntCache(int size)
Construct a new cache. Basically, two hash tables are used, each with the given capacity. When one fills up, the other is thrown out. A Least-Recently-Used policy is effected by migrating entries from the old hash to the new hash when they are accessed through get().

Parameters:
size - How large to make each of the two internal hash tables.
Method Detail

clear

public void clear()
Clears all entries from the cache


contains

public boolean contains(int key)
Check whether the given key is present in the cache


get

public Object get(int key)
Retrieve the value for the given key, or null if not found.


put

public void put(int key,
                Object val)
Add a key/value pair to the cache. May result in pushing older items out of the cache.