T - the type of cached contentspublic class CachedFileContents<T>
extends java.lang.Object
Object fileRepresentation = // ...
File toWrite = // ...
// Write file contents and update in memory representation
CachedFileContents<Object> contents = new CachedFileContents<Object>(toWrite);
contents.closed(fileRepresentation);
// Later, when data is needed:
if (contents.isValid()) {
fileRepresentation = contents.getCache();
} else {
// Re-read the file and recreate the file representation
}
| Constructor and Description |
|---|
CachedFileContents(java.io.File file)
Creates a new contents.
|
| Modifier and Type | Method and Description |
|---|---|
void |
closed(T cache)
Should be called when the file's contents are set and the file closed.
|
T |
getCache()
Obtains the cached data set with
closed(Object) if the file has not been modified
since closed(Object) was invoked. |
java.io.File |
getFile()
Obtains the file used for caching.
|
boolean |
isValid()
Are the cached contents still valid? If this method determines that the file has been
modified since the last time
closed(Object) was invoked. |
public CachedFileContents(@Nonnull
java.io.File file)
closed(Object) should be invoked
to set the cache.file - the filepublic void closed(@Nullable
T cache)
This method can be called as many times as the file has been written.
cache - an optional cache to savepublic boolean isValid()
closed(Object) was invoked.false, the
cache is cleared@Nullable public T getCache()
closed(Object) if the file has not been modified
since closed(Object) was invoked.null if the file has been modified since
closed(Object) has been invoked@Nonnull public java.io.File getFile()