correct way of processing cache
hi,
I need to check for a cache file that contains a MyCache object with
some precomputed values( based on data from image files in a folder).
The algorithm is as follows,
1.If a cache file exists, retrieve the MyCache object from cache file
and get a list of image file names from MyCache object,
and compare it with current List of filenames.
2.If both lists are same ,I can use the image data from MyCache
object.
3.if lists are different ,compute the image based data,create new
MyCache object with this image based data and current list of
filenames.
4.do step 3 if no cache file exists.
I tried to implement this as follows.However,I am not sure if this is
the 'object oriented way' to do this.Can somebody advise?
thanks and regards,
mark
<code>
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
public class CacheChecker {
private MyCache cachefile;
public void checkCache(String folder){
List<String> newFileNames = parseFolder(folder);
try {
MyCache cache = getExistingCache(folder);
compareListsAndProcessCache(folder,newFileNames,cache);
} catch (IOException e) {
//cache file not found,need to create new cache
calculateAndCreateNewCache();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void compareListsAndProcessCache(String folder, List<String>
newFileNames, MyCache cache) {
List<String>oldFileNames = cache.getImgNames();
//if lists are same get data from cache
if(newFileNames.equals(oldFileNames)){
this.cachefile = cache;
}else{
//lists not same
calculateAndCreateNewCache();
}
}
private void calculateAndCreateNewCache() {
//do calculations
//create MyCache from calculated data and filenames list
//write MyCache object to 'mycachefile'
}
private MyCache getExistingCache(String folder)throws IOException,
ClassNotFoundException{
FileInputStream fin = new FileInputStream(folder+File.separator
+"mycachefile");
ObjectInputStream oin = new ObjectInputStream(fin);
MyCache cache = (MyCache)oin.readObject();
oin.close();
fin.close();
return cache;
}
private List<String> parseFolder(String folder) {
// return current list of image filenames
return null;
}
}
class MyCache {
private List<String> imgNames;
private double[][] imagesData;
public MyCache(List<String> imgNames,double[][] imagesData){
this.imgNames = imgNames;
this.imagesData = imagesData;
}
public List<String> getImgNames() {
return imgNames;
}
public void setImgNames(List<String> imgNames) {
this.imgNames = imgNames;
}
public double[][] getImagesData() {
return imagesData;
}
}
</code>