Android Open Source - droid-fu Cached Model






From Project

Back to project page droid-fu.

License

The source code is released under:

Apache License

If you think the Android project droid-fu listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.droidfu.cachefu;
//from   w ww . j a va 2s . c o m
import java.io.IOException;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Superclass of all objects to be stored in {@link ModelCache}.
 * 
 * To save an object to the cache use {@link #save(ModelCache)}, when updating an object with any
 * new data use {@link #reload(ModelCache)}, and when wanting to find an object from the cache, use
 * {@link #find(ModelCache, String, Class)}.
 * 
 * @author michaelengland
 * 
 */
public abstract class CachedModel implements Parcelable {

    private String id;
    private long transactionId = Long.MIN_VALUE;

    /**
     * Simple parameter-less constructor. <b>Must</b> also have parameter-less constructor in
     * subclasses in order for parceling to work.
     */
    public CachedModel() {
    }

    /**
     * Constructor setting variables from parcel. Same as using a blank constructor and calling
     * readFromParcel.
     * 
     * @param source
     *            Parcel to be read from.
     * @throws IOException
     */
    public CachedModel(Parcel source) throws IOException {
        readFromParcel(source);
    }

    /**
     * Constructor setting ID given.
     * 
     * @param id
     *            ID of new object (used when generating cache key).
     */
    public CachedModel(String id) {
        this.id = id;
    }

    /**
     * Returns ID of object used in key generation.
     * 
     * @return ID of new object (used when generating cache key).
     */
    public String getId() {
        return id;
    }

    /**
     * Set ID of object used in key generation.
     * 
     * @param id
     *            ID of new object (used when generating cache key).
     */
    public void setId(String id) {
        this.id = id;
    }

    void setTransactionId(long transactionId) {
        this.transactionId = transactionId;
    }

    /**
     * @return Key used to store object in cache. null if id is null.
     */
    public String getKey() {
        if (id == null) {
            return null;
        } else {
            return createKey(id);
        }
    }

    /**
     * Method for looking up object in cache.
     * 
     * @param modelCache
     *            Cache to be searched.
     * @param id
     *            ID of object to be searched for.
     * @param clazz
     *            Class of desired cached object
     * @return Object from cache based upon given parameters.
     */
    public static CachedModel find(ModelCache modelCache, String id,
            Class<? extends CachedModel> clazz) {
        // Create empty object of type
        CachedModel testObject;
        try {
            testObject = clazz.newInstance();
        } catch (Exception e) {
            return null;
        }

        // Set ID so key can be generated
        testObject.setId(id);

        // Attempt to reload object from cache
        if (testObject.reload(modelCache)) {
            return testObject;
        } else {
            return null;
        }
    }

    /**
     * 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.:
     * 
     * <pre>
     * {@code
     * public boolean save(ModelCache modelCache) {
     *     return super.save(modelCache) && super.save(modelCache, super.getKey());
     * }
     * }
     * </pre>
     * 
     * @param modelCache
     *            Cache to save to.
     * @return Whether or not saving to the cache was successful.
     */
    public boolean save(ModelCache modelCache) {
        return save(modelCache, getKey());
    }

    /**
     * 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.
     * 
     * @param modelCache
     *            Cache to save to.
     * @param saveKey
     *            Key to be saved under.
     * @return Whether or not saving to the cache was successful.
     */
    protected boolean save(ModelCache modelCache, String saveKey) {
        if ((modelCache != null) && (saveKey != null)) {
            modelCache.put(saveKey, this);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Attempts to reload any new data from cache.
     * 
     * @param modelCache
     *            Cache to be reloaded from.
     * @return Whether or not newer data was found in the cache.
     */
    public boolean reload(ModelCache modelCache) {
        String key = getKey();
        if ((modelCache != null) && (key != null)) {
            CachedModel cachedModel = modelCache.get(key);
            if ((cachedModel != null) && (cachedModel.transactionId > this.transactionId)) {
                reloadFromCachedModel(modelCache, cachedModel);
                this.transactionId = cachedModel.transactionId;
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Method called to determine a key in the cache using the object's id e.g.:
     * 
     * <pre>
     * {@code
     * public String createKey(String id) {
     *     "example_object_" + id;
     * }
     * }
     * </pre>
     * 
     * @param id
     *            ID of object to be stored.
     * @return Key that object with given ID should be stored in cache under.
     */
    public abstract String createKey(String id);

    /**
     * Method called to reload any data from a more recently stored object e.g.:
     * 
     * <pre>
     * {@code
     * public boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel) {
     *     ExampleObject cachedExampleObject = (ExampleObject) cachedModel;
     *     this.exampleVariable = cachedExampleObject.exampleVariable;
     *     return false;
     * }
     * }
     * </pre>
     * 
     * Can also be used to reload internal cached objects. e.g.:
     * 
     * <pre>
     * {@code
     * public boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel) {
     *     ExampleObject cachedExampleObject = (ExampleObject) cachedModel;
     *     this.exampleInternalCachedObject = cachedExampleObject.exampleInternalCachedObject;
     *     return this.exampleInternalCachedObject.reload(modelCache);
     * }
     * }
     * </pre>
     * 
     * @param modelCache
     *            Cache that is currently being reloaded from.
     * @param cachedModel
     *            Latest version of object in cache.
     * @return Whether or not an internal cached object was updated (useful for optimization
     *         purposes, especially on lists).
     */
    public abstract boolean reloadFromCachedModel(ModelCache modelCache, CachedModel cachedModel);

    /**
     * @see android.os.Parcelable#describeContents()
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeLong(transactionId);
    }

    /**
     * Saves data to parcel.
     * 
     * @param source
     *            Parcel to save to.
     * @throws IOException
     */
    public void readFromParcel(Parcel source) throws IOException {
        id = source.readString();
        transactionId = source.readLong();
    }

}




Java Source Code List

com.github.droidfu.DroidFuApplication.java
com.github.droidfu.DroidFu.java
com.github.droidfu.activities.BetterActivityHelper.java
com.github.droidfu.activities.BetterActivity.java
com.github.droidfu.activities.BetterDefaultActivity.java
com.github.droidfu.activities.BetterExpandableListActivity.java
com.github.droidfu.activities.BetterListActivity.java
com.github.droidfu.activities.BetterMapActivity.java
com.github.droidfu.activities.BetterPreferenceActivity.java
com.github.droidfu.adapters.ListAdapterWithProgress.java
com.github.droidfu.adapters.WebGalleryAdapter.java
com.github.droidfu.cachefu.AbstractCache.java
com.github.droidfu.cachefu.CacheHelper.java
com.github.droidfu.cachefu.CachedList.java
com.github.droidfu.cachefu.CachedModel.java
com.github.droidfu.cachefu.HttpResponseCache.java
com.github.droidfu.cachefu.ImageCache.java
com.github.droidfu.cachefu.ModelCache.java
com.github.droidfu.concurrent.BetterAsyncTaskCallable.java
com.github.droidfu.concurrent.BetterAsyncTask.java
com.github.droidfu.dialogs.DialogClickListener.java
com.github.droidfu.exception.ResourceMessageException.java
com.github.droidfu.http.BetterHttpRequestBase.java
com.github.droidfu.http.BetterHttpRequestRetryHandler.java
com.github.droidfu.http.BetterHttpRequest.java
com.github.droidfu.http.BetterHttpResponseImpl.java
com.github.droidfu.http.BetterHttpResponse.java
com.github.droidfu.http.BetterHttp.java
com.github.droidfu.http.CachedHttpRequest.java
com.github.droidfu.http.CachedHttpResponse.java
com.github.droidfu.http.ConnectionChangedBroadcastReceiver.java
com.github.droidfu.http.HttpDelete.java
com.github.droidfu.http.HttpGet.java
com.github.droidfu.http.HttpPost.java
com.github.droidfu.http.HttpPut.java
com.github.droidfu.http.ssl.EasySSLSocketFactory.java
com.github.droidfu.http.ssl.TrivialTrustManager.java
com.github.droidfu.imageloader.ImageLoaderHandler.java
com.github.droidfu.imageloader.ImageLoader.java
com.github.droidfu.listeners.MapGestureListener.java
com.github.droidfu.services.BetterService.java
com.github.droidfu.support.ArraySupport.java
com.github.droidfu.support.DiagnosticSupport.java
com.github.droidfu.support.DisplaySupport.java
com.github.droidfu.support.IntentSupport.java
com.github.droidfu.support.StringSupport.java
com.github.droidfu.testsupport.DroidFuAssertions.java
com.github.droidfu.widgets.WebImageView.java
com.google.common.base.FinalizableReferenceQueue.java
com.google.common.base.FinalizableReference.java
com.google.common.base.FinalizableSoftReference.java
com.google.common.base.FinalizableWeakReference.java
com.google.common.base.Function.java
com.google.common.base.Objects.java
com.google.common.base.internal.Finalizer.java
com.google.common.collect.AbstractMapEntry.java
com.google.common.collect.AsynchronousComputationException.java
com.google.common.collect.ComputationException.java
com.google.common.collect.CustomConcurrentHashMap.java
com.google.common.collect.ExpirationTimer.java
com.google.common.collect.MapMaker.java
com.google.common.collect.NullOutputException.java