<commit_before>/*
 * Copyright 2018 The twicalico authors
 *
 * 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.github.moko256.twicalico.cacheMap;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.LruCache;

import com.github.moko256.mastodon.MTStatus;
import com.github.moko256.twicalico.GlobalApplication;
import com.github.moko256.twicalico.database.CachedStatusesSQLiteOpenHelper;
import com.github.moko256.twicalico.entity.AccessToken;
import com.github.moko256.twicalico.entity.Emoji;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import rx.Observable;
import twitter4j.GeoLocation;
import twitter4j.HashtagEntity;
import twitter4j.MediaEntity;
import twitter4j.Place;
import twitter4j.RateLimitStatus;
import twitter4j.Scopes;
import twitter4j.Status;
import twitter4j.SymbolEntity;
import twitter4j.URLEntity;
import twitter4j.User;
import twitter4j.UserMentionEntity;

/**
 * Created by moko256 on 2016/12/22.
 *
 * @author moko256
 */

public class StatusCacheMap {

    private LruCache<Long, Status> cache =new LruCache<>(GlobalApplication.statusCacheListLimit / 4);
    private CachedStatusesSQLiteOpenHelper diskCache;

    public void prepare(Context context, AccessToken accessToken){
        if (diskCache != null){
            diskCache.close();
        }
        if (cache.size() > 0){
            cache.evictAll();
        }
        diskCache = new CachedStatusesSQLiteOpenHelper(context, accessToken.getUserId());
    }

    public int size() {
        return cache.size();
    }

    public void add(@Nullable final Status status) {
        if (status != null) {
            GlobalApplication.userCache.add(status.getUser());
            if (status.isRetweet()) {
                add(status.getRetweetedStatus());
            }
            Status cacheStatus = new CachedStatus(status);
            cache.put(status.getId(), cacheStatus);
            diskCache.addCachedStatus(cacheStatus);
        }
    }

    @Nullable
    public Status get(Long id){
        Status memoryCache = cache.get(id);
        if (memoryCache == null){
            Status storageCache = diskCache.getCachedStatus(id);
            if (storageCache != null) {
                cache.put(storageCache.getId(), storageCache);
            }
            return  storageCache;
        } else {
            return memoryCache;
        }
    }

    public void addAll(Collection<? extends Status> c) {
        if (c.size() > 0) {
            Observable<Status> statusesObservable = Observable.unsafeCreate(subscriber -> {
                for (Status status : c) {
                    if (status != null) {
                        subscriber.onNext(status);
                        if (status.isRetweet()) {
                            subscriber.onNext(status.getRetweetedStatus());
                        }
                    }
                }
                subscriber.onCompleted();
            });

            GlobalApplication.userCache.addAll(
                    statusesObservable.map(Status::getUser).distinct().toList().toSingle().toBlocking().value()
            );

            Observable<Status> cachedStatusObservable = statusesObservable.map(CachedStatus::new);

            cachedStatusObservable.forEach(status -> cache.put(status.getId(), status));

            diskCache.addCachedStatuses(cachedStatusObservable.toList().toSingle().toBlocking().value());
        }
    }

    public void delete(List<Long> ids){
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            if (id != null) {
                list.add(id);
            }
        }
        diskCache.deleteCachedStatuses(list);
    }

    public static class CachedStatus implements Status{

        /* Based on twitter4j.StatusJSONImpl */

        private final Date createdAt;
        private final long id;

        private final long userId;

        private final long retweetedStatusId;
<commit_msg>fix: add serialVersionUID for DeployGate<commit_after>/*
 * Copyright 2018 The twicalico authors
 *
 * 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.github.moko256.twicalico.cacheMap;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.LruCache;

import com.github.moko256.mastodon.MTStatus;
import com.github.moko256.twicalico.GlobalApplication;
import com.github.moko256.twicalico.database.CachedStatusesSQLiteOpenHelper;
import com.github.moko256.twicalico.entity.AccessToken;
import com.github.moko256.twicalico.entity.Emoji;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import rx.Observable;
import twitter4j.GeoLocation;
import twitter4j.HashtagEntity;
import twitter4j.MediaEntity;
import twitter4j.Place;
import twitter4j.RateLimitStatus;
import twitter4j.Scopes;
import twitter4j.Status;
import twitter4j.SymbolEntity;
import twitter4j.URLEntity;
import twitter4j.User;
import twitter4j.UserMentionEntity;

/**
 * Created by moko256 on 2016/12/22.
 *
 * @author moko256
 */

public class StatusCacheMap {

    private LruCache<Long, Status> cache =new LruCache<>(GlobalApplication.statusCacheListLimit / 4);
    private CachedStatusesSQLiteOpenHelper diskCache;

    public void prepare(Context context, AccessToken accessToken){
        if (diskCache != null){
            diskCache.close();
        }
        if (cache.size() > 0){
            cache.evictAll();
        }
        diskCache = new CachedStatusesSQLiteOpenHelper(context, accessToken.getUserId());
    }

    public int size() {
        return cache.size();
    }

    public void add(@Nullable final Status status) {
        if (status != null) {
            GlobalApplication.userCache.add(status.getUser());
            if (status.isRetweet()) {
                add(status.getRetweetedStatus());
            }
            Status cacheStatus = new CachedStatus(status);
            cache.put(status.getId(), cacheStatus);
            diskCache.addCachedStatus(cacheStatus);
        }
    }

    @Nullable
    public Status get(Long id){
        Status memoryCache = cache.get(id);
        if (memoryCache == null){
            Status storageCache = diskCache.getCachedStatus(id);
            if (storageCache != null) {
                cache.put(storageCache.getId(), storageCache);
            }
            return  storageCache;
        } else {
            return memoryCache;
        }
    }

    public void addAll(Collection<? extends Status> c) {
        if (c.size() > 0) {
            Observable<Status> statusesObservable = Observable.unsafeCreate(subscriber -> {
                for (Status status : c) {
                    if (status != null) {
                        subscriber.onNext(status);
                        if (status.isRetweet()) {
                            subscriber.onNext(status.getRetweetedStatus());
                        }
                    }
                }
                subscriber.onCompleted();
            });

            GlobalApplication.userCache.addAll(
                    statusesObservable.map(Status::getUser).distinct().toList().toSingle().toBlocking().value()
            );

            Observable<Status> cachedStatusObservable = statusesObservable.map(CachedStatus::new);

            cachedStatusObservable.forEach(status -> cache.put(status.getId(), status));

            diskCache.addCachedStatuses(cachedStatusObservable.toList().toSingle().toBlocking().value());
        }
    }

    public void delete(List<Long> ids){
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            if (id != null) {
                list.add(id);
            }
        }
        diskCache.deleteCachedStatuses(list);
    }

    public static class CachedStatus implements Status{

        private static final long serialVersionUID = 2895731644139422046L;

        /* Based on twitter4j.StatusJSONImpl */

        private final Date createdAt;
        private final long id;

        private final long userId;

        private final long retweetedStatusId;

{'fraction_non_alphanumeric': 0.0970382337102854, 'fraction_numerical': 0.01410877759827679, 'mean_word_length': 2.497551789077213, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '831935', 'n_tokens_mistral': 2617, 'n_tokens_neox': 2482, 'n_words': 606}