Java tutorial
/* * 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.api.models.CaptchaModel; import com.nttec.everychan.api.models.SendPostModel; import android.support.v4.util.LruCache; /** * ? ? ( ?).<br> * ? ? {@link SendPostModel} ( LRU-?) ? ? ? (?) ( ?) * @author miku-nyan * */ public class DraftsCache { private final Serializer serializer; private final LruCache<String, SendPostModel> lru; /** * ? * @param maxSize ? ? ?, ??? ? * @param serizlizer ? */ public DraftsCache(int maxSize, Serializer serializer) { this.serializer = serializer; this.lru = new LruCache<String, SendPostModel>(maxSize); } /** * ? LRU-? ?. ? ? */ public void clearLru() { lru.evictAll(); } /** * ? * @param hash ? ? * @param model */ public void put(String hash, SendPostModel model) { lru.put(hash, model); serializer.serializeDraft(hash, model); } /** * ? * @param hash ? ? * @return null, ? ?? ? */ public SendPostModel get(String hash) { SendPostModel fromLru = lru.get(hash); if (fromLru != null) return fromLru; return serializer.deserializeDraft(hash); } /** * ? * @param hash ? ? */ public void remove(String hash) { lru.remove(hash); serializer.removeDraft(hash); } private String lastCaptchaHash; private CaptchaModel lastCaptcha; /** * ? ? ? * @param hash ? ? * @param captcha */ public void setLastCaptcha(String hash, CaptchaModel captcha) { lastCaptchaHash = hash; lastCaptcha = captcha; } /** * ? ? */ public void clearLastCaptcha() { lastCaptchaHash = null; lastCaptcha = null; } /** * ? ? */ public CaptchaModel getLastCaptcha() { return lastCaptcha; } /** * ? ?, ? ??? ?? */ public String getLastCaptchaHash() { return lastCaptchaHash; } }