
com.octo.android.robospice.persistence.file.InFileObjectPersister Maven / Gradle / Ivy
The newest version!
package com.octo.android.robospice.persistence.file;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import roboguice.util.temp.Ln;
import android.app.Application;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.ObjectPersister;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.exception.CacheLoadingException;
import com.octo.android.robospice.persistence.exception.KeySanitationExcepion;
import com.octo.android.robospice.persistence.keysanitation.KeySanitizer;
/**
* An {@link ObjectPersister} that saves/loads data in a file.
* @author sni
* @param
* the class of the data to load/save.
*/
public abstract class InFileObjectPersister extends ObjectPersister {
// ----------------------------------
// CONSTANTS
// ----------------------------------
/* package private */
static final String CACHE_PREFIX_END = "_";
/* package private */
static final String DEFAULT_ROOT_CACHE_DIR = "robospice-cache";
// ----------------------------------
// ATTRIBUTES
// ----------------------------------
private KeySanitizer keySanitizer;
private File cacheFolder;
private String factoryCachePrefix = "";
// ----------------------------------
// CONSTRUCTOR
// ----------------------------------
public InFileObjectPersister(Application application, Class clazz) throws CacheCreationException {
super(application, clazz);
setCacheFolder(null);
}
public InFileObjectPersister(Application application, Class clazz, File cacheFolder) throws CacheCreationException {
super(application, clazz);
setCacheFolder(cacheFolder);
}
// ----------------------------------
// PUBLIC API
// ----------------------------------
/**
* Set the cacheFolder to use.
* @param cacheFolder
* the new cache folder to use. Can be null, will then default to
* {@link #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.
*/
public void setCacheFolder(File cacheFolder) throws CacheCreationException {
if (cacheFolder == null) {
cacheFolder = new File(getApplication().getCacheDir(), DEFAULT_ROOT_CACHE_DIR);
}
synchronized (cacheFolder.getAbsolutePath().intern()) {
if (!cacheFolder.exists() && !cacheFolder.mkdirs()) {
throw new CacheCreationException("The cache folder " + cacheFolder.getAbsolutePath() + " could not be created.");
}
}
this.cacheFolder = cacheFolder;
}
public final File getCacheFolder() {
return cacheFolder;
}
@Override
public long getCreationDateInCache(Object cacheKey) throws CacheLoadingException {
File cacheFile = getCacheFile(cacheKey);
if (cacheFile.exists()) {
return cacheFile.lastModified();
} else {
throw new CacheLoadingException(
"Data could not be found in cache for cacheKey=" + cacheKey);
}
}
@Override
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy