com.github.ignition.support.cache
Class CachedModel

java.lang.Object
  extended by com.github.ignition.support.cache.CachedModel
All Implemented Interfaces:
android.os.Parcelable
Direct Known Subclasses:
CachedList

public abstract class CachedModel
extends Object
implements android.os.Parcelable

Superclass of all objects to be stored in ModelCache. To save an object to the cache use save(ModelCache), when updating an object with any new data use reload(ModelCache), and when wanting to find an object from the cache, use find(ModelCache, String, Class).

Author:
michaelengland

Nested Class Summary
 
Nested classes/interfaces inherited from interface android.os.Parcelable
android.os.Parcelable.Creator<T>
 
Field Summary
 
Fields inherited from interface android.os.Parcelable
CONTENTS_FILE_DESCRIPTOR, PARCELABLE_WRITE_RETURN_VALUE
 
Constructor Summary
CachedModel()
          Simple parameter-less constructor.
CachedModel(android.os.Parcel source)
          Constructor setting variables from parcel.
CachedModel(String id)
          Constructor setting ID given.
 
Method Summary
abstract  String createKey(String id)
          Method called to determine a key in the cache using the object's id e.g.:
 int describeContents()
           
static CachedModel find(ModelCache modelCache, String id, Class<? extends CachedModel> clazz)
          Method for looking up object in cache.
 String getId()
          Returns ID of object used in key generation.
 String getKey()
           
 void readFromParcel(android.os.Parcel source)
          Saves data to parcel.
 boolean reload(ModelCache modelCache)
          Attempts to reload any new data from cache.
abstract  boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel)
          Method called to reload any data from a more recently stored object e.g.:
 boolean save(ModelCache modelCache)
          Attempts to store the object in the cache using this object's key.
protected  boolean save(ModelCache modelCache, String saveKey)
          Attempts to save the object in the cache using a given key.
 void setId(String id)
          Set ID of object used in key generation.
 void writeToParcel(android.os.Parcel dest, int flags)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachedModel

public CachedModel()
Simple parameter-less constructor. Must also have parameter-less constructor in subclasses in order for parceling to work.


CachedModel

public CachedModel(android.os.Parcel source)
            throws IOException
Constructor setting variables from parcel. Same as using a blank constructor and calling readFromParcel.

Parameters:
source - Parcel to be read from.
Throws:
IOException

CachedModel

public CachedModel(String id)
Constructor setting ID given.

Parameters:
id - ID of new object (used when generating cache key).
Method Detail

getId

public String getId()
Returns ID of object used in key generation.

Returns:
ID of new object (used when generating cache key).

setId

public void setId(String id)
Set ID of object used in key generation.

Parameters:
id - ID of new object (used when generating cache key).

getKey

public String getKey()
Returns:
Key used to store object in cache. null if id is null.

find

public static CachedModel find(ModelCache modelCache,
                               String id,
                               Class<? extends CachedModel> clazz)
Method for looking up object in cache.

Parameters:
modelCache - Cache to be searched.
id - ID of object to be searched for.
clazz - Class of desired cached object
Returns:
Object from cache based upon given parameters.

save

public boolean save(ModelCache modelCache)
Attempts to store the object in the cache using this object's key. Generally this is the required used. Overwritten when saving subclasses over the top of separate superclass cache stores. e.g.:
 public boolean save(ModelCache modelCache) {
     return super.save(modelCache) && super.save(modelCache, super.getKey());
 }
 
 

Parameters:
modelCache - Cache to save to.
Returns:
Whether or not saving to the cache was successful.

save

protected boolean save(ModelCache modelCache,
                       String saveKey)
Attempts to save the object in the cache using a given key. Generally only used for saving subclasses over the top of separate superclass cache stores.

Parameters:
modelCache - Cache to save to.
saveKey - Key to be saved under.
Returns:
Whether or not saving to the cache was successful.

reload

public boolean reload(ModelCache modelCache)
Attempts to reload any new data from cache.

Parameters:
modelCache - Cache to be reloaded from.
Returns:
Whether or not newer data was found in the cache.

createKey

public abstract String createKey(String id)
Method called to determine a key in the cache using the object's id e.g.:
 public String createKey(String id) {
     "example_object_" + id;
 }
 
 

Parameters:
id - ID of object to be stored.
Returns:
Key that object with given ID should be stored in cache under.

reloadFromCachedModel

public abstract boolean reloadFromCachedModel(ModelCache modelCache,
                                              CachedModel cachedModel)
Method called to reload any data from a more recently stored object e.g.:
 public boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel) {
     ExampleObject cachedExampleObject = (ExampleObject) cachedModel;
     this.exampleVariable = cachedExampleObject.exampleVariable;
     return false;
 }
 
 
Can also be used to reload internal cached objects. e.g.:
 public boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel) {
     ExampleObject cachedExampleObject = (ExampleObject) cachedModel;
     this.exampleInternalCachedObject = cachedExampleObject.exampleInternalCachedObject;
     return this.exampleInternalCachedObject.reload(modelCache);
 }
 
 

Parameters:
modelCache - Cache that is currently being reloaded from.
cachedModel - Latest version of object in cache.
Returns:
Whether or not an internal cached object was updated (useful for optimization purposes, especially on lists).

describeContents

public int describeContents()
Specified by:
describeContents in interface android.os.Parcelable
See Also:
Parcelable.describeContents()

writeToParcel

public void writeToParcel(android.os.Parcel dest,
                          int flags)
Specified by:
writeToParcel in interface android.os.Parcelable
See Also:
Parcelable.writeToParcel(android.os.Parcel, int)

readFromParcel

public void readFromParcel(android.os.Parcel source)
                    throws IOException
Saves data to parcel.

Parameters:
source - Parcel to save to.
Throws:
IOException


Copyright © 2012. All Rights Reserved.