Class IndexCache
- java.lang.Object
-
- net.sf.eBus.util.IndexCache
-
public final class IndexCache extends Object
Persists a one or more 4-byte, integer index values to a specified file. This class allows an application to track an index value across executions. An example of this is tracking the incoming and outgoing sequence numbers used by a communication protocol.An index cache is created by calling
openand passing in the cache file name. If the index cache is currently open, then the existing instance is returned. Otherwise, if the file either does not exist or is of zero length, then a new index cache is created, containing no indices. If the file exists and is of non-zero length, the cache file is opened and its subordinate indices are read in. TheindexCount()method returns the number of subordinate indices in the cache.An
IndexCache.Indexinstance is retrieved by callinggetIndex. A new index is added to the cache by callingaddIndex. An index may have either a positive increment or a negative increment. If positive, then the initial index must be ≤ to the final index. If negative, then the initial index must by ≥ to the final index. The increment may not be zero. An existing index is removed from the cache via the methodremoveIndex.The method
IndexCache.Index.nextIndex()returns the incremented index. If the increment index exceeds the final index (either > or < depending on whether the increment is > or < zero), then the "reset on final index" flag is checked to determine what to do next. If this flag istrue, then the index is reset to the initial index value and that value is returned. Iffalse, then anIndexOutOfBoundsExceptionis thrown and the index can no longer be used.A long is used because that will support all other integer types. For smaller integer types, configure the instance accordingly.
Note: the cache file is not written only when the index cache is closed. This class puts adds a thread to the
Runtime.addShutdownHook(java.lang.Thread). This thread closes all open index cache files when the JVM shuts down.This class does not support sharing the index cache file between simultaneously executing applications. Such use will corrupt the index file and result in undetermined results. Neither
IndexCachenorIndexCache.Indexclasses are thread-safe. If there is a need to share anIndexCacheinstance among threads, then the application performs the appropriate synchronization. The static data memberopen(String)andisCacheOpen(java.lang.String)are thread-safe.If you want unique, reusable index values that do not need to be persisted between application executions, then use
IndexPool.- Author:
- Charles Rapp
- See Also:
IndexPool
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIndexCache.IndexTracks the individual index parameters and its location within the cache file.
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_AUTO_RESETAutomatic index reset is turned off by default.static longDEFAULT_FINAL_INDEXThe default final index isLong.MAX_VALUE.static longDEFAULT_INCREMENTThe default index increment is one.static longDEFAULT_INITIAL_INDEXThe default initial index is zero.static intMAXIMUM_INDEX_NAME_LENGTHAn index name may be at most 20 characters long.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description IndexCache.IndexaddIndex(String indexName, long initialIndex, long finalIndex, long increment, boolean resetFlag)Returns the new index added to the cache.StringcacheFileName()Returns the cache file name as originally supplied by user.voidclearIndices()Removes all indices from the cache leaving the cache empty.voidclose()Closes the cache file and removes this index cache from the map.booleancontainsIndex(String indexName)Returnstrueif this cache contains the named index andfalseotherwise.IndexCache.IndexgetIndex(String indexName)Returns the named index instance.intindexCount()Returns the number of indices in this cache.List<String>indexNames()Returns a list of current index names.static booleanisCacheOpen(String cacheName)Returnstrueif the named index cache is open andfalseif it is not.booleanisEmpty()Returnstrueif this cache has no indices andfalseif it has at least one index.DatelastModified()Returns the timestamp when the cache was last modified.static IndexCacheopen(String cacheFileName)Returns theIndexCacheinstance associated withcacheFileName.booleanremoveIndex(String indexName)Returnstrueif the named index was removed andfalseif the index is unknown.voidresetAllIndices()Resets all indices to their respective initial values.StringtoString()Returns a textual representation of this index cache.
-
-
-
Field Detail
-
DEFAULT_INITIAL_INDEX
public static final long DEFAULT_INITIAL_INDEX
The default initial index is zero.- See Also:
- Constant Field Values
-
DEFAULT_FINAL_INDEX
public static final long DEFAULT_FINAL_INDEX
The default final index isLong.MAX_VALUE.- See Also:
- Constant Field Values
-
DEFAULT_INCREMENT
public static final long DEFAULT_INCREMENT
The default index increment is one.- See Also:
- Constant Field Values
-
DEFAULT_AUTO_RESET
public static final boolean DEFAULT_AUTO_RESET
Automatic index reset is turned off by default.- See Also:
- Constant Field Values
-
MAXIMUM_INDEX_NAME_LENGTH
public static final int MAXIMUM_INDEX_NAME_LENGTH
An index name may be at most 20 characters long.- See Also:
- Constant Field Values
-
-
Method Detail
-
cacheFileName
public String cacheFileName()
Returns the cache file name as originally supplied by user.- Returns:
- the cache file name.
-
lastModified
public Date lastModified()
Returns the timestamp when the cache was last modified. This value is set to the cache file last modified date when opened or to the current date and time when created.- Returns:
- the timestamp when the cache was last modified.
-
isEmpty
public boolean isEmpty()
Returnstrueif this cache has no indices andfalseif it has at least one index.- Returns:
trueif this cache has no indices andfalseif it has at least one index.
-
indexCount
public int indexCount()
Returns the number of indices in this cache.- Returns:
- the number of indices in this cache.
-
indexNames
public List<String> indexNames()
Returns a list of current index names. The returned list is a copy of the actual list and so may be modified without impacting the actual list.- Returns:
- a list of current index names.
-
containsIndex
public boolean containsIndex(String indexName)
Returnstrueif this cache contains the named index andfalseotherwise.- Parameters:
indexName- the index name.- Returns:
trueif this cache contains the named index andfalseotherwise.- See Also:
getIndex(String),addIndex(String, long, long, long, boolean),removeIndex(String)
-
getIndex
public IndexCache.Index getIndex(String indexName)
Returns the named index instance. May returnnull.- Parameters:
indexName- the index name.- Returns:
- the specified named index instance.
- See Also:
containsIndex(String),addIndex(String, long, long, long, boolean),removeIndex(String)
-
isCacheOpen
public static boolean isCacheOpen(String cacheName)
Returnstrueif the named index cache is open andfalseif it is not.- Parameters:
cacheName- the index cache file name.- Returns:
trueif the named index cache is open andfalseif it is not.
-
addIndex
public IndexCache.Index addIndex(String indexName, long initialIndex, long finalIndex, long increment, boolean resetFlag)
Returns the new index added to the cache. Note thatincrementmay be either > or < zero and thatinitialIndexmay equalfinalIndex.- Parameters:
indexName- the index name. Must be unique withinthis IndexCache.initialIndex- the index initial value.finalIndex- the index maximum value. The index will not exceed this value.increment- increment the current value by this amount to get the next index value. The increment may be either > zero or < zero.resetFlag- iftrue, then reset the index to the initial value when the final value is exceeded.- Returns:
- the newly added index.
- Throws:
IllegalArgumentException- if:-
if
indexNameisnull, an empty string or longer thanMAXIMUM_INDEX_NAME_LENGTH, -
if
indexNameis not unique withinthis IndexCacheinstance, -
incrementis zero, -
initialIndexis >finalIndexandincrementis > zero or -
initialIndexis <finalIndexandincrementis < zero
-
if
-
removeIndex
public boolean removeIndex(String indexName)
Returnstrueif the named index was removed andfalseif the index is unknown.- Parameters:
indexName- remove the named index at this position.- Returns:
trueif the named index was removed andfalseif the index is unknown.
-
resetAllIndices
public void resetAllIndices()
Resets all indices to their respective initial values.
-
clearIndices
public void clearIndices()
Removes all indices from the cache leaving the cache empty.
-
close
public void close()
Closes the cache file and removes this index cache from the map. This index cache must be re-opened before it may be used again.- See Also:
open(java.lang.String)
-
toString
public String toString()
Returns a textual representation of this index cache.
-
open
public static IndexCache open(String cacheFileName) throws IOException
Returns theIndexCacheinstance associated withcacheFileName. If the instance is already open, that instance is returned. Otherwise the cache file's existence is tested. If the file does not exist or is of size zero, then returns a new, emptyIndexCacheinstance. Otherwise, the cache file is opened for reading and writing, the index information is read in and the instance is created from the data.- Parameters:
cacheFileName- read in the index data from this file.- Returns:
- the index cache instance associated with the file name.
- Throws:
IllegalArgumentException- ifcacheFileNameis eithernullor empty.IOException- ifcacheFileNameexists but contains corrupted data.
-
-