com.github.ignition.support.cache
Class CachedList<CO extends CachedModel>

java.lang.Object
  extended by com.github.ignition.support.cache.CachedModel
      extended by com.github.ignition.support.cache.CachedList<CO>
Type Parameters:
CO - Type of cached models to be stored in list
All Implemented Interfaces:
android.os.Parcelable

public class CachedList<CO extends CachedModel>
extends CachedModel

Superclass of all list objects to be stored in ModelCache. Operates just as standard cached object, and contains an array list of objects. Must be initialized with the class of the objects stored, as this is used in parcelling/unparcelling. In order to ensure thread-safe use of list (such as iteration), use the getList() method, creating a copy of the list in its current state.

Author:
michaelengland

Nested Class Summary
 
Nested classes/interfaces inherited from interface android.os.Parcelable
android.os.Parcelable.Creator<T>
 
Field Summary
protected  Class<? extends CachedModel> clazz
          Class type of object list
static android.os.Parcelable.Creator<CachedList<CachedModel>> CREATOR
          Creator object used for parcelling
protected  ArrayList<CO> list
          List of objects.
 
Fields inherited from interface android.os.Parcelable
CONTENTS_FILE_DESCRIPTOR, PARCELABLE_WRITE_RETURN_VALUE
 
Constructor Summary
CachedList()
          Simple parameter-less constructor.
CachedList(Class<? extends CachedModel> clazz)
          Constructor initializing class of objects stored.
CachedList(Class<? extends CachedModel> clazz, int initialLength)
          Constructor initializing class of objects stored as well as initial length of list.
CachedList(Class<? extends CachedModel> clazz, String id)
          Constructor initializing class of objects stored as well as id used in key generation.
CachedList(android.os.Parcel source)
          Constructor setting variables from parcel.
 
Method Summary
 void add(CO cachedObject)
          Synchronized method used to append an object to the list.
 String createKey(String id)
          Method called to determine a key in the cache using the object's id e.g.:
 boolean equals(Object o)
           
 CO get(int index)
          Synchronized method used to get an object from the live list.
 ArrayList<CO> getList()
          Synchronized method to get a copy of the list in its current state.
 void readFromParcel(android.os.Parcel source)
          Saves data to parcel.
 boolean reload(ModelCache modelCache)
          Attempts to reload any new data from cache.
 boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel)
          Method called to reload any data from a more recently stored object e.g.:
 void set(int index, CO cachedObject)
          Synchronized method used to set an object at a location in the list.
 int size()
          Synchronized method used to return size of list.
 void writeToParcel(android.os.Parcel dest, int flags)
           
 
Methods inherited from class com.github.ignition.support.cache.CachedModel
describeContents, find, getId, getKey, save, save, setId
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clazz

protected Class<? extends CachedModel> clazz
Class type of object list


list

protected ArrayList<CO extends CachedModel> list
List of objects.


CREATOR

public static final android.os.Parcelable.Creator<CachedList<CachedModel>> CREATOR
Creator object used for parcelling

Constructor Detail

CachedList

public CachedList()
Simple parameter-less constructor. Must also have parameter-less constructor in subclasses in order for parceling to work. Do not use this constructor when creating a list, use one setting class instead.


CachedList

public CachedList(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

CachedList

public CachedList(Class<? extends CachedModel> clazz)
Constructor initializing class of objects stored.

Parameters:
clazz - Required for parcelling and unparcelling of list

CachedList

public CachedList(Class<? extends CachedModel> clazz,
                  int initialLength)
Constructor initializing class of objects stored as well as initial length of list.

Parameters:
clazz - Required for parcelling and unparcelling of list
initialLength - Initial length of list

CachedList

public CachedList(Class<? extends CachedModel> clazz,
                  String id)
Constructor initializing class of objects stored as well as id used in key generation.

Parameters:
clazz - Required for parcelling and unparcelling of list
id - ID of new list (used when generating cache key).
Method Detail

getList

public ArrayList<CO> getList()
Synchronized method to get a copy of the list in its current state. This should be used when iterating over the list in order to avoid thread-unsafe operations.

Returns:
Copy of list in its current state

add

public void add(CO cachedObject)
Synchronized method used to append an object to the list.

Parameters:
cachedObject - Object to add to list

set

public void set(int index,
                CO cachedObject)
Synchronized method used to set an object at a location in the list.

Parameters:
index - Index of item to set
cachedObject - Object to set in list

get

public CO get(int index)
Synchronized method used to get an object from the live list.

Parameters:
index - Index of item in list
Returns:
Item in list

size

public int size()
Synchronized method used to return size of list.

Returns:
Size of list

equals

public boolean equals(Object o)
Overrides:
equals in class Object
See Also:
Object.equals(java.lang.Object)

createKey

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

Specified by:
createKey in class CachedModel
Parameters:
id - ID of object to be stored.
Returns:
Key that object with given ID should be stored in cache under.
See Also:
com.github.droidfu.cachefu.CachedModel#createKey(java.lang.String)

reload

public boolean reload(ModelCache modelCache)
Description copied from class: CachedModel
Attempts to reload any new data from cache.

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

reloadFromCachedModel

public boolean reloadFromCachedModel(ModelCache modelCache,
                                     CachedModel cachedModel)
Description copied from class: 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);
 }
 
 

Specified by:
reloadFromCachedModel in class CachedModel
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).
See Also:
com.github.droidfu.cachefu.CachedModel#reloadFromCachedModel(com.github.droidfu.cachefu.ModelCache, com.github.droidfu.cachefu.CachedModel)

readFromParcel

public void readFromParcel(android.os.Parcel source)
                    throws IOException
Description copied from class: CachedModel
Saves data to parcel.

Overrides:
readFromParcel in class CachedModel
Parameters:
source - Parcel to save to.
Throws:
IOException
See Also:
com.github.droidfu.cachefu.CachedModel#readFromParcel(android.os.Parcel)

writeToParcel

public void writeToParcel(android.os.Parcel dest,
                          int flags)
Specified by:
writeToParcel in interface android.os.Parcelable
Overrides:
writeToParcel in class CachedModel
See Also:
com.github.droidfu.cachefu.CachedModel#writeToParcel(android.os.Parcel, int)


Copyright © 2012. All Rights Reserved.