com.nttec.everychan.cache.PagesCache.java Source code

Java tutorial

Introduction

Here is the source code for com.nttec.everychan.cache.PagesCache.java

Source

/*
 * Everychan Android (Meta Imageboard Client)
 * Copyright (C) 2014-2016  miku-nyan <https://github.com/miku-nyan>
 *     
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nttec.everychan.cache;

import com.nttec.everychan.common.Logger;
import com.nttec.everychan.ui.presentation.PresentationModel;
import android.support.v4.util.LruCache;

/**
 * ? ?, ? ? ? ? ?  ( )      PresentationModel (  ?)
 * @author miku-nyan
 *
 */
public class PagesCache {
    private static final String TAG = "PagesCache";

    private final Serializer serializer;
    private final LruCache<String, PresentationModel> lru;

    /**
     * ?
     * @param maxSize ?  ?  ? (  ?. {@link PresentationModel#getSerializablePageSize})
     * @param serizlizer ?
     */
    public PagesCache(int maxSize, Serializer serializer) {
        this.serializer = serializer;
        this.lru = new LruCache<String, PresentationModel>(maxSize) {
            @Override
            protected int sizeOf(String key, PresentationModel value) {
                return value.size;
            }
        };
    }

    /**
     * ? LRU-?  ?.
     *   ?     ?   ( ?  ?  ?)
     */
    public void clearLru() {
        lru.evictAll();
    }

    /**
     * ??     ?  ?
     * @param hash ? ?
     * @return ?   null, ? ? ??  ?  ?
     */
    public PresentationModel getPresentationModel(String hash) {
        return lru.get(hash);
    }

    /**
     *     LRU-?  ?,   ?? ?  {@link SerializablePage}   ?.
     * @param hash ? ?
     * @param model ? 
     */
    public void putPresentationModel(String hash, PresentationModel model) {
        putPresentationModel(hash, model, true);
    }

    /**
     *     LRU-?  ?
     * @param hash ? ?
     * @param model ? 
     * @param putToFileCache   ?? ?  {@link SerializablePage}   ?
     */
    public void putPresentationModel(String hash, PresentationModel model, boolean putToFileCache) {
        if (hash == null || model == null) {
            if (hash == null)
                Logger.e(TAG, "received null hash");
            if (model == null)
                Logger.e(TAG, "received null object for hash: " + hash);
            return;
        }
        lru.put(hash, model);
        if (putToFileCache) {
            putSerializablePage(hash, model.source);
        }
    }

    /**
     * ??  ?  ?  ?  ?   ?. 
     * @param hash ? ?
     * @return ??   null, ? ? ??   LRU-?  ?,    ?
     */
    public SerializablePage getSerializablePage(String hash) {
        PresentationModel fromLRU = getPresentationModel(hash);
        if (fromLRU != null)
            return fromLRU.source;
        return serializer.deserializePage(hash);
    }

    /**
     *  ?  ?   ?.
     * @param hash ? ?
     * @param page ?? 
     */
    public void putSerializablePage(String hash, SerializablePage page) {
        serializer.serializePage(hash, page);
    }

    /**
     * ??  ?  ?? ?   ?.
     * @param hash ?
     * @return ??   null, ? ? ??   ?
     */
    public SerializableBoardsList getSerializableBoardsList(String hash) {
        return serializer.deserializeBoardsList(hash);
    }

    /**
     *  ?  ?? ?   ?.
     * @param hash ?
     * @param boardsList ?? 
     */
    public void putSerializableBoardsList(String hash, SerializableBoardsList boardsList) {
        serializer.serializeBoardsList(hash, boardsList);
    }

}