Class CachedFileContents<T>
- java.lang.Object
-
- com.android.tools.build.apkzlib.utils.CachedFileContents<T>
-
- Type Parameters:
T- the type of cached contents
public class CachedFileContents<T> extends java.lang.ObjectA cache for file contents. The cache allows closing a file and saving in memory its contents (or some related information). It can then be used to check if the contents are still valid at some later time. Typical usage flow is: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 Summary
Constructors Constructor Description CachedFileContents(java.io.File file)Creates a new contents.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclosed(T cache)Should be called when the file's contents are set and the file closed.TgetCache()Obtains the cached data set withclosed(Object)if the file has not been modified sinceclosed(Object)was invoked.java.io.FilegetFile()Obtains the file used for caching.booleanisValid()Are the cached contents still valid? If this method determines that the file has been modified since the last timeclosed(Object)was invoked.
-
-
-
Constructor Detail
-
CachedFileContents
public CachedFileContents(java.io.File file)
Creates a new contents. When the file is written,closed(Object)should be invoked to set the cache.- Parameters:
file- the file
-
-
Method Detail
-
closed
public void closed(@Nullable T cache)Should be called when the file's contents are set and the file closed. This will save the cache and register the file's timestamp to later detect if it has been modified.This method can be called as many times as the file has been written.
- Parameters:
cache- an optional cache to save
-
isValid
public boolean isValid()
Are the cached contents still valid? If this method determines that the file has been modified since the last timeclosed(Object)was invoked.- Returns:
- are the cached contents still valid? If this method returns
false, the cache is cleared
-
getCache
@Nullable public T getCache()
Obtains the cached data set withclosed(Object)if the file has not been modified sinceclosed(Object)was invoked.- Returns:
- the last cached data or
nullif the file has been modified sinceclosed(Object)has been invoked
-
getFile
public java.io.File getFile()
Obtains the file used for caching.- Returns:
- the file; this file always exists and contains the old (cached) contents of the file
-
-