edu.mit.jwi
Class CachingDictionary.ItemCache

java.lang.Object
  extended by edu.mit.jwi.CachingDictionary.ItemCache
All Implemented Interfaces:
IClosable, IHasLifecycle, ICachingDictionary.IItemCache
Enclosing class:
CachingDictionary

public static class CachingDictionary.ItemCache
extends java.lang.Object
implements ICachingDictionary.IItemCache

An LRU cache for objects in JWI.

Since:
JWI 2.2.0
Version:
2.4.0
Author:
Mark A. Finlayson

Nested Class Summary
 
Nested classes/interfaces inherited from interface edu.mit.jwi.data.IHasLifecycle
IHasLifecycle.LifecycleState, IHasLifecycle.ObjectClosedException, IHasLifecycle.ObjectOpenException
 
Field Summary
static int DEFAULT_INITIAL_CAPACITY
           
static float DEFAULT_LOAD_FACTOR
           
static int DEFAULT_MAXIMUM_CAPACITY
           
protected  java.util.Map<IItemID<?>,IItem<?>> itemCache
           
protected  java.util.Map<ISenseKey,IWord> keyCache
           
protected  java.util.concurrent.locks.Lock lifecycleLock
           
protected  java.util.Map<ISenseKey,ISenseEntry> senseCache
           
 
Constructor Summary
CachingDictionary.ItemCache()
          Default constructor that initializes the dictionary with caching enabled.
CachingDictionary.ItemCache(int initialCapacity, int maxCapacity, boolean enabled)
          Caller can specify both the initial size, maximum size, and the initial state of caching.
 
Method Summary
 void cacheItem(IItem<?> item)
          Caches the specified item, if this cache is enabled.
 void cacheSenseEntry(ISenseEntry entry)
          Caches the specified entry.
 void cacheWordByKey(IWord word)
          Caches the specified word, indexed by its sense key.
protected  void checkOpen()
          An internal method for assuring compliance with the dictionary interface that says that methods will throw ObjectClosedExceptions if the dictionary has not yet been opened.
 void clear()
          Removes all entries from the cache.
 void close()
          This closes the object by disposing of data backing objects or connections.
 int getInitialCapacity()
          Returns the initial capacity of this cache.
 int getMaximumCapacity()
          Returns the maximum capacity of this cache.
 boolean isEnabled()
          Returns true if this cache is enabled; false otherwise.
 boolean isOpen()
          Returns true if the dictionary is open, that is, ready to accept queries; returns false otherwise
protected
<K,V> java.util.Map<K,V>
makeCache(int initialCapacity)
          Creates the map that backs this cache.
 boolean open()
          This opens the object by performing any required initialization steps.
protected  void reduceCacheSize(java.util.Map<?,?> cache)
          Brings the map size into line with the specified maximum capacity of this cache.
<T extends IItem<D>,D extends IItemID<T>>
T
retrieveItem(D id)
          Retrieves the item identified by the specified id.
 ISenseEntry retrieveSenseEntry(ISenseKey key)
          Retrieves the sense entry identified by the specified sense key.
 IWord retrieveWord(ISenseKey key)
          Retrieves the word identified by the specified sense key.
 void setEnabled(boolean isEnabled)
          Enables (true) or disables (false) caching.
 void setInitialCapacity(int capacity)
          Sets the initial capacity of the cache
 void setMaximumCapacity(int capacity)
          Sets the maximum capacity of the cache
 int size()
          Returns the number of items in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_INITIAL_CAPACITY

public static final int DEFAULT_INITIAL_CAPACITY
See Also:
Constant Field Values

DEFAULT_MAXIMUM_CAPACITY

public static final int DEFAULT_MAXIMUM_CAPACITY
See Also:
Constant Field Values

DEFAULT_LOAD_FACTOR

public static final float DEFAULT_LOAD_FACTOR
See Also:
Constant Field Values

lifecycleLock

protected java.util.concurrent.locks.Lock lifecycleLock

itemCache

protected java.util.Map<IItemID<?>,IItem<?>> itemCache

keyCache

protected java.util.Map<ISenseKey,IWord> keyCache

senseCache

protected java.util.Map<ISenseKey,ISenseEntry> senseCache
Constructor Detail

CachingDictionary.ItemCache

public CachingDictionary.ItemCache()
Default constructor that initializes the dictionary with caching enabled.

Since:
JWI 2.2.0

CachingDictionary.ItemCache

public CachingDictionary.ItemCache(int initialCapacity,
                                   int maxCapacity,
                                   boolean enabled)
Caller can specify both the initial size, maximum size, and the initial state of caching.

Parameters:
initialCapacity - the initial capacity of the cache
maxCapacity - the maximum capacity of the cache
enabled - whether the cache starts out enabled
Since:
JWI 2.2.0
Method Detail

open

public boolean open()
             throws java.io.IOException
Description copied from interface: IHasLifecycle
This opens the object by performing any required initialization steps. If this method returns false, then subsequent calls to IHasLifecycle.isOpen() will return false.

Specified by:
open in interface IHasLifecycle
Returns:
true if there were no errors in initialization; false otherwise.
Throws:
java.io.IOException - if there was IO error while performing initializataion

makeCache

protected <K,V> java.util.Map<K,V> makeCache(int initialCapacity)
Creates the map that backs this cache.

Type Parameters:
K - the key type
V - the value type
Parameters:
initialCapacity - the initial capacity
Returns:
the new map
Since:
JWI 2.2.0

isOpen

public boolean isOpen()
Description copied from interface: IHasLifecycle
Returns true if the dictionary is open, that is, ready to accept queries; returns false otherwise

Specified by:
isOpen in interface IHasLifecycle
Returns:
true if the object is open; false otherwise

checkOpen

protected void checkOpen()
An internal method for assuring compliance with the dictionary interface that says that methods will throw ObjectClosedExceptions if the dictionary has not yet been opened.

Throws:
ObjectClosedException - if the dictionary is closed.
Since:
JWI 2.2.0

close

public void close()
Description copied from interface: IClosable
This closes the object by disposing of data backing objects or connections. If the object is already closed, or in the process of closing, this method does nothing (although, if the object is in the process of closing, it may block until closing is complete).

Specified by:
close in interface IClosable

clear

public void clear()
Description copied from interface: ICachingDictionary.IItemCache
Removes all entries from the cache.

Specified by:
clear in interface ICachingDictionary.IItemCache

isEnabled

public boolean isEnabled()
Description copied from interface: ICachingDictionary.IItemCache
Returns true if this cache is enabled; false otherwise. If a cache is enabled, it will cache an item passed to its cache methods.

Specified by:
isEnabled in interface ICachingDictionary.IItemCache
Returns:
true if this cache is enabled; false otherwise

setEnabled

public void setEnabled(boolean isEnabled)
Description copied from interface: ICachingDictionary.IItemCache
Enables (true) or disables (false) caching.

Specified by:
setEnabled in interface ICachingDictionary.IItemCache
Parameters:
isEnabled - if true, caching is enabled; if false, caching is disabled.

getInitialCapacity

public int getInitialCapacity()
Returns the initial capacity of this cache.

Returns:
the initial capacity of this cache.
Since:
JWI 2.2.0

setInitialCapacity

public void setInitialCapacity(int capacity)
Sets the initial capacity of the cache

Parameters:
capacity - the initial capacity
Since:
JWI 2.2.0

getMaximumCapacity

public int getMaximumCapacity()
Description copied from interface: ICachingDictionary.IItemCache
Returns the maximum capacity of this cache.

Specified by:
getMaximumCapacity in interface ICachingDictionary.IItemCache
Returns:
the maximum capacity of this cache.

setMaximumCapacity

public void setMaximumCapacity(int capacity)
Description copied from interface: ICachingDictionary.IItemCache
Sets the maximum capacity of the cache

Specified by:
setMaximumCapacity in interface ICachingDictionary.IItemCache
Parameters:
capacity - the maximum capacity

size

public int size()
Description copied from interface: ICachingDictionary.IItemCache
Returns the number of items in the cache.

Specified by:
size in interface ICachingDictionary.IItemCache
Returns:
the number of items in the cache.

cacheItem

public void cacheItem(IItem<?> item)
Description copied from interface: ICachingDictionary.IItemCache
Caches the specified item, if this cache is enabled. Otherwise does nothing.

Specified by:
cacheItem in interface ICachingDictionary.IItemCache
Parameters:
item - the item to be cached; may not be null

cacheWordByKey

public void cacheWordByKey(IWord word)
Description copied from interface: ICachingDictionary.IItemCache
Caches the specified word, indexed by its sense key.

Specified by:
cacheWordByKey in interface ICachingDictionary.IItemCache
Parameters:
word - the word to be cached; may not be null

cacheSenseEntry

public void cacheSenseEntry(ISenseEntry entry)
Description copied from interface: ICachingDictionary.IItemCache
Caches the specified entry.

Specified by:
cacheSenseEntry in interface ICachingDictionary.IItemCache
Parameters:
entry - the entry to be cached; may not be null

reduceCacheSize

protected void reduceCacheSize(java.util.Map<?,?> cache)
Brings the map size into line with the specified maximum capacity of this cache.

Parameters:
cache - the map to be trimmed
Since:
JWI 2.2.0

retrieveItem

public <T extends IItem<D>,D extends IItemID<T>> T retrieveItem(D id)
Description copied from interface: ICachingDictionary.IItemCache
Retrieves the item identified by the specified id.

Specified by:
retrieveItem in interface ICachingDictionary.IItemCache
Type Parameters:
T - the type of the item
D - the type of the item id
Parameters:
id - the id for the requested item
Returns:
the item for the specified id, or null if not present in the cache

retrieveWord

public IWord retrieveWord(ISenseKey key)
Description copied from interface: ICachingDictionary.IItemCache
Retrieves the word identified by the specified sense key.

Specified by:
retrieveWord in interface ICachingDictionary.IItemCache
Parameters:
key - the sense key for the requested word
Returns:
the word for the specified key, or null if not present in the cache

retrieveSenseEntry

public ISenseEntry retrieveSenseEntry(ISenseKey key)
Description copied from interface: ICachingDictionary.IItemCache
Retrieves the sense entry identified by the specified sense key.

Specified by:
retrieveSenseEntry in interface ICachingDictionary.IItemCache
Parameters:
key - the sense key for the requested sense entry
Returns:
the sense entry for the specified key, or null if not present in the cache


Copyright © 2007-2013 Massachusetts Institute of Technology. All Rights Reserved.