com.arthurpitman.common.data.LocalProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.arthurpitman.common.data.LocalProvider.java

Source

/*
 * Copyright (C) 2012 - 2014 Arthur Pitman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arthurpitman.common.data;

import java.util.Arrays;

import android.support.v4.util.LruCache;

import com.arthurpitman.common.CoreException;

/**
 * Base class for local providers of IdObjects.
 * @param <T>
 */
public abstract class LocalProvider<T extends IdObject> {
    private LruCache<Long, T> cache;

    /**
     * Creates a LocalProvider with the specified cache size.
     * @param cacheSize
     */
    public LocalProvider(int cacheSize) {
        cache = new LruCache<Long, T>(cacheSize);
    }

    /**
     * Gets an object by ID.
     * @param id
     * @return
     * @throws CoreException
     */
    public T get(long id) throws CoreException {
        T o = cache.get(id);
        if ((o == null) || o.isStale()) {
            o = getLocal(id);
        }
        return o;
    }

    /**
     * Gets a set of objects specified by an {@link IdSet}.
     * @param ids
     * @return
     * @throws CoreException
     */
    public ResultSet<T> get(IdSet ids) throws CoreException {
        long[] sortedIds = ids.toArray();
        Arrays.sort(sortedIds);
        ResultSet<T> result = new ResultSet<T>(sortedIds.length);
        for (long id : sortedIds) {
            T o = cache.get(id);
            if ((o == null) || o.isStale()) {
                o = getLocal(id);
                if (o != null) {
                    cache.put(id, o);
                    result.append(o);
                }
            } else {
                result.append(o);
            }
        }
        return result;
    }

    /**
     * Refreshes a single object.
     * @param id
     * @param defer
     * @throws CoreException
     */
    public void refresh(long id, boolean defer) throws CoreException {
        if (defer) {
            T o = cache.get(id);
            if (o != null) {
                o.setStale(true);
            }
        } else {
            if (cache.get(id) != null) {
                T o = getLocal(id);
                cache.put(o.getId(), o);
            }
        }
    }

    /**
     * Refreshes a set of objects.
     * @param ids
     * @param defer
     * @throws CoreException
     */
    public void refresh(IdSet ids, boolean defer) throws CoreException {
        int size = ids.size();
        for (int i = 0; i < size; i++) {
            long id = ids.get(i);
            refresh(id, defer);
        }
    }

    /**
     * Retrieves an object from local storage.
     * <p/>
     * Override this in derived classes.
     * @param id The id of the object
     * @return The object or null if unavailable
     * @throws CoreException
     */
    protected abstract T getLocal(long id) throws CoreException;
}