com.octo.android.robospice.persistence.file
Class InFileObjectPersister<T>

java.lang.Object
  extended by com.octo.android.robospice.persistence.ObjectPersister<T>
      extended by com.octo.android.robospice.persistence.file.InFileObjectPersister<T>
Type Parameters:
T - the class of the data to load/save.
All Implemented Interfaces:
CacheCleaner, Persister
Direct Known Subclasses:
InFileBitmapObjectPersister, InFileInputStreamObjectPersister, InFileStringObjectPersister, JsonObjectPersister, RetrofitObjectPersister, SpringAndroidObjectPersister

public abstract class InFileObjectPersister<T>
extends ObjectPersister<T>

An ObjectPersister that saves/loads data in a file.

Author:
sni

Constructor Summary
InFileObjectPersister(android.app.Application application, Class<T> clazz)
           
InFileObjectPersister(android.app.Application application, Class<T> clazz, File cacheFolder)
           
 
Method Summary
protected  String fromKey(String cacheKey)
          Get a cache key that may be de-sanitized if a KeySanitizer is used.
 List<Object> getAllCacheKeys()
           
 File getCacheFile(Object cacheKey)
           
 File getCacheFolder()
           
protected  String getCachePrefix()
           
 long getCreationDateInCache(Object cacheKey)
          Return the creation date of creation of cache. entry for a given cacheKey.
 KeySanitizer getKeySanitizer()
           
protected  boolean isCachedAndNotExpired(File cacheFile, long maxTimeInCacheBeforeExpiry)
           
protected  boolean isCachedAndNotExpired(Object cacheKey, long maxTimeInCacheBeforeExpiry)
           
 boolean isDataInCache(Object cacheKey, long maxTimeInCacheBeforeExpiry)
           
 boolean isUsingKeySanitizer()
           
 List<T> loadAllDataFromCache()
           
 T loadDataFromCache(Object cacheKey, long maxTimeInCache)
          Load data from cache if not expired.
protected abstract  T readCacheDataFromFile(File file)
           
 void removeAllDataFromCache()
           
 boolean removeDataFromCache(Object cacheKey)
           
 void setCacheFolder(File cacheFolder)
          Set the cacheFolder to use.
 void setKeySanitizer(KeySanitizer keySanitizer)
           
protected  String toKey(String cacheKey)
          Get a key that may be sanitized if a KeySanitizer is used.
 
Methods inherited from class com.octo.android.robospice.persistence.ObjectPersister
canHandleClass, getApplication, getHandledClass, isAsyncSaveEnabled, saveDataToCacheAndReturnData, setAsyncSaveEnabled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InFileObjectPersister

public InFileObjectPersister(android.app.Application application,
                             Class<T> clazz)
                      throws CacheCreationException
Throws:
CacheCreationException

InFileObjectPersister

public InFileObjectPersister(android.app.Application application,
                             Class<T> clazz,
                             File cacheFolder)
                      throws CacheCreationException
Throws:
CacheCreationException
Method Detail

setCacheFolder

public void setCacheFolder(File cacheFolder)
                    throws CacheCreationException
Set the cacheFolder to use.

Parameters:
cacheFolder - the new cache folder to use. Can be null, will then default to DEFAULT_ROOT_CACHE_DIR sub folder in the application cache dir.
Throws:
CacheCreationException - if the cache folder doesn't exist or can't be created.

getCacheFolder

public final File getCacheFolder()

getCreationDateInCache

public long getCreationDateInCache(Object cacheKey)
                            throws CacheLoadingException
Description copied from class: ObjectPersister
Return the creation date of creation of cache. entry for a given cacheKey.

Specified by:
getCreationDateInCache in class ObjectPersister<T>
Parameters:
cacheKey - the cachekey identifying the object to look for.
Returns:
a long corresponding to the creation date of creation of cache.
Throws:
CacheLoadingException - if there is no such element in cache.

getAllCacheKeys

public List<Object> getAllCacheKeys()
Specified by:
getAllCacheKeys in class ObjectPersister<T>

loadAllDataFromCache

public List<T> loadAllDataFromCache()
                             throws CacheLoadingException
Specified by:
loadAllDataFromCache in class ObjectPersister<T>
Throws:
CacheLoadingException

removeDataFromCache

public boolean removeDataFromCache(Object cacheKey)
Specified by:
removeDataFromCache in class ObjectPersister<T>

removeAllDataFromCache

public void removeAllDataFromCache()
Specified by:
removeAllDataFromCache in interface CacheCleaner
Specified by:
removeAllDataFromCache in class ObjectPersister<T>

loadDataFromCache

public T loadDataFromCache(Object cacheKey,
                           long maxTimeInCache)
                    throws CacheLoadingException
Description copied from class: ObjectPersister
Load data from cache if not expired.

Specified by:
loadDataFromCache in class ObjectPersister<T>
Parameters:
cacheKey - the cacheKey of the data to load.
maxTimeInCache - the maximum time the data can have been stored in cached before being considered expired. 0 means infinite.
Returns:
the data if it could be loaded.
Throws:
CacheLoadingException - if the data in cache is expired.

isDataInCache

public boolean isDataInCache(Object cacheKey,
                             long maxTimeInCacheBeforeExpiry)
Specified by:
isDataInCache in class ObjectPersister<T>

isUsingKeySanitizer

public boolean isUsingKeySanitizer()
Returns:
Whether or not this InFileObjectPersister uses a KeySanitizer.

setKeySanitizer

public void setKeySanitizer(KeySanitizer keySanitizer)
Parameters:
keySanitizer - the new key sanitizer to be used by this InFileObjectPersister. May be null, in that case no key sanitation will be used default). If key sanitation fails on a given cache key (by throwing a KeySanitationExcepion, original (unsanitized) cache keys will be used directly.

getKeySanitizer

public KeySanitizer getKeySanitizer()
Returns:
the key sanitizer used by this InFileObjectPersister. May be null, in that case no key sanitation will be used default).

getCacheFile

public final File getCacheFile(Object cacheKey)

toKey

protected final String toKey(String cacheKey)
Get a key that may be sanitized if a KeySanitizer is used.

Parameters:
cacheKey - a non-sanitized cacheKey.
Returns:
a key that will be sanitized if a KeySanitizer is used.

fromKey

protected final String fromKey(String cacheKey)
Get a cache key that may be de-sanitized if a KeySanitizer is used.

Parameters:
cacheKey - a possibly sanitized cacheKey.
Returns:
a key that will be de-sanitized if a KeySanitizer is used.

readCacheDataFromFile

protected abstract T readCacheDataFromFile(File file)
                                    throws CacheLoadingException
Throws:
CacheLoadingException

getCachePrefix

protected final String getCachePrefix()

isCachedAndNotExpired

protected boolean isCachedAndNotExpired(Object cacheKey,
                                        long maxTimeInCacheBeforeExpiry)

isCachedAndNotExpired

protected boolean isCachedAndNotExpired(File cacheFile,
                                        long maxTimeInCacheBeforeExpiry)


Copyright © 2012-2014. All Rights Reserved.