public final class IndexCache extends Object
An index cache is created by calling
open and 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. The indexCount() method returns
the number of subordinate indices in the cache.
An IndexCache.Index instance is retrieved by calling
getIndex. A new index is added to the
cache by calling
addIndex.
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 method
removeIndex.
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 is true, then
the index is reset to the initial index value and that value
is returned. If false, then an
IndexOutOfBoundsException is 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 IndexCache nor
IndexCache.Index classes are thread-safe. If there is
a need to share an IndexCache instance among threads,
then the application performs the appropriate synchronization.
The static data member open(String) and
isCacheOpen(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.
IndexPool| Modifier and Type | Class and Description |
|---|---|
static class |
IndexCache.Index
Tracks the individual index parameters and its location
within the cache file.
|
| Modifier and Type | Field and Description |
|---|---|
static boolean |
DEFAULT_AUTO_RESET
Automatic index reset is turned off by default.
|
static long |
DEFAULT_FINAL_INDEX
The default final index is
Long.MAX_VALUE. |
static long |
DEFAULT_INCREMENT
The default index increment is one.
|
static long |
DEFAULT_INITIAL_INDEX
The default initial index is zero.
|
static int |
MAXIMUM_INDEX_NAME_LENGTH
An index name may be at most 20 characters long.
|
| Modifier and Type | Method and Description |
|---|---|
IndexCache.Index |
addIndex(String indexName,
long initialIndex,
long finalIndex,
long increment,
boolean resetFlag)
Returns the new index added to the cache.
|
String |
cacheFileName()
Returns the cache file name as originally supplied by
user.
|
void |
clearIndices()
Removes all indices from the cache leaving the cache
empty.
|
void |
close()
Closes the cache file and removes this index cache from
the map.
|
boolean |
containsIndex(String indexName)
Returns
true if this cache contains the named
index and false otherwise. |
IndexCache.Index |
getIndex(String indexName)
Returns the named index instance.
|
int |
indexCount()
Returns the number of indices in this cache.
|
List<String> |
indexNames()
Returns a list of current index names.
|
static boolean |
isCacheOpen(String cacheName)
Returns
true if the named index cache is open and
false if it is not. |
boolean |
isEmpty()
Returns
true if this cache has no indices and
false if it has at least one index. |
Date |
lastModified()
Returns the timestamp when the cache was last modified.
|
static IndexCache |
open(String cacheFileName)
Returns the
IndexCache instance associated with
cacheFileName. |
boolean |
removeIndex(String indexName)
Returns
true if the named index was removed and
false if the index is unknown. |
void |
resetAllIndices()
Resets all indices to their respective initial values.
|
String |
toString()
Returns a textual representation of this index cache.
|
public static final long DEFAULT_INITIAL_INDEX
public static final long DEFAULT_FINAL_INDEX
Long.MAX_VALUE.public static final long DEFAULT_INCREMENT
public static final boolean DEFAULT_AUTO_RESET
public static final int MAXIMUM_INDEX_NAME_LENGTH
public String cacheFileName()
public Date lastModified()
public boolean isEmpty()
true if this cache has no indices and
false if it has at least one index.true if this cache has no indices and
false if it has at least one index.public int indexCount()
public List<String> indexNames()
public boolean containsIndex(String indexName)
true if this cache contains the named
index and false otherwise.indexName - the index name.true if this cache contains the named
index and false otherwise.getIndex(String),
addIndex(String, long, long, long, boolean),
removeIndex(String)public IndexCache.Index getIndex(String indexName)
null.indexName - the index name.containsIndex(String),
addIndex(String, long, long, long, boolean),
removeIndex(String)public static boolean isCacheOpen(String cacheName)
true if the named index cache is open and
false if it is not.cacheName - the index cache file name.true if the named index cache is open and
false if it is not.public IndexCache.Index addIndex(String indexName, long initialIndex, long finalIndex, long increment, boolean resetFlag) throws IllegalArgumentException
increment may be either > or < zero and
that initialIndex may equal finalIndex.indexName - the index name. Must be unique within
this 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 - if true, then reset the index
to the initial value when the final value is exceeded.IllegalArgumentException - if:
indexName is null, an empty string
or longer than MAXIMUM_INDEX_NAME_LENGTH,
indexName is not unique within
this IndexCache instance,
increment is zero,
initialIndex is > finalIndex and
increment is > zero or
initialIndex is < finalIndex and
increment is < zero
public boolean removeIndex(String indexName)
true if the named index was removed and
false if the index is unknown.indexName - remove the named index at this position.true if the named index was removed and
false if the index is unknown.public void resetAllIndices()
public void clearIndices()
public void close()
open(java.lang.String)public String toString()
public static IndexCache open(String cacheFileName) throws IllegalArgumentException, IOException
IndexCache instance associated with
cacheFileName. 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, empty IndexCache
instance. Otherwise, the cache file is opened for reading
and writing, the index information is read in and the
instance is created from the data.cacheFileName - read in the index data from this
file.IllegalArgumentException - if cacheFileName is either null or empty.IOException - if cacheFileName exists but contains corrupted
data.Copyright © 2019. All rights reserved.