Search code examples
androidcachingobjectobjectcache

How do I convert a generically cached object into a specific one in android


I have an object caching class that can save an object in memory, the problem arises when I try to retreive it later. How do I output a generic Object and then put it into a defined object on the other end. Here is the class

public class ObjectCache implements Serializable{

private static final long serialVersionUID = 1L;
private Context context;
private String objectName;
private Integer keepAlive = 86400;

public ObjectCache(Context context,String objectName) {
    this.context = context;
    this.objectName = objectName;
}

public void setKeepAlive(Integer time) {
    this.keepAlive = time;
}

public boolean saveObject(Object obj) {

    final File cacheDir = context.getCacheDir();
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;

    if (!cacheDir.exists()) {
        cacheDir.mkdirs();
    }

    final File cacheFile = new File(cacheDir, objectName);

    try {

        fos = new FileOutputStream(cacheFile);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
            if (fos != null) {
                fos.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}

public boolean deleteObject() {

    final File cacheDir = context.getCacheDir();
    final File cacheFile = new File(cacheDir, objectName);
    return (cacheFile.delete());
}

public Object getObject() {

    final File cacheDir = context.getCacheDir();
    final File cacheFile = new File(cacheDir, objectName);

    Object simpleClass = null;
    FileInputStream fis = null;
    ObjectInputStream is = null;

    try {

        fis = new FileInputStream(cacheFile);
        is = new ObjectInputStream(fis);
        simpleClass = (Object) is.readObject();
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (is != null) {
                is.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return simpleClass;

}
}

And from activity I start the object class, save it, and retreive it like so

objectCache = new ObjectCache(this,"sessionCache2");
//save object returns true
boolean result = objectCache.saveObject(s);

//get object back
Object o = objectCache.getObject();

instead of Object o i need it to be a Session object, but then that will mean the return type of the getObject method needs to return that. Can't I convert the object some


Solution

  • If you are sure that the object that the ObjectCache will return is a Session object all you have to do is cast it:

    //get object back
    Session session = (Session) objectCache.getObject();
    

    This will throw a ClassCastException (unchecked) if the getObject() method does not return a Session object.