<commit_before>/*
 *     Copyright (C) 2017 <NAME>
 *
 *     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 2 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, write to the Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.pyamsoft.padlock.purge

import com.pyamsoft.pydroid.data.Cache
import io.reactivex.Observable
import io.reactivex.Single
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton

@Singleton internal class PurgeInteractorCache @Inject internal constructor(
    @param:Named("interactor_purge") private val impl: PurgeInteractor) : PurgeInteractor, Cache {

  private var cachedList: Observable<String>? = null
  private var lastAccessListTime: Long = 0L

  override fun clearCache() {
    cachedList = null
  }

  override fun populateList(forceRefresh: Boolean): Observable<String> {
    return Observable.defer {
      val cache = cachedList
      val list: Observable<String>
      val currentTime = System.currentTimeMillis()
      if (forceRefresh || cache == null || lastAccessListTime + FIVE_MINUTES_MILLIS < currentTime) {
        list = impl.populateList(true).cache()
        cachedList = list
        lastAccessListTime = currentTime
      } else {
        list = cache
      }
      return@defer list
    }.doOnError { clearCache() }
  }

  override fun deleteEntry(packageName: String): Single<String> {
    return impl.deleteEntry(packageName).doOnSuccess {
      val obj: Observable<String>? = cachedList
      if (obj != null) {
        cachedList = obj.filter { it == packageName }.doOnError { clearCache() }
      }<commit_msg>Fix purge deletion<commit_after>/*
 *     Copyright (C) 2017 <NAME>
 *
 *     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 2 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, write to the Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.pyamsoft.padlock.purge

import com.pyamsoft.pydroid.data.Cache
import io.reactivex.Observable
import io.reactivex.Single
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton

@Singleton internal class PurgeInteractorCache @Inject internal constructor(
    @param:Named("interactor_purge") private val impl: PurgeInteractor) : PurgeInteractor, Cache {

  private var cachedList: Observable<String>? = null
  private var lastAccessListTime: Long = 0L

  override fun clearCache() {
    cachedList = null
  }

  override fun populateList(forceRefresh: Boolean): Observable<String> {
    return Observable.defer {
      val cache = cachedList
      val list: Observable<String>
      val currentTime = System.currentTimeMillis()
      if (forceRefresh || cache == null || lastAccessListTime + FIVE_MINUTES_MILLIS < currentTime) {
        list = impl.populateList(true).cache()
        cachedList = list
        lastAccessListTime = currentTime
      } else {
        list = cache
      }
      return@defer list
    }.doOnError { clearCache() }
  }

  override fun deleteEntry(packageName: String): Single<String> {
    return impl.deleteEntry(packageName).doOnSuccess {
      val obj: Observable<String>? = cachedList
      if (obj != null) {
        cachedList = obj.filter { it != packageName }.doOnError { clearCache() }
      }
{'fraction_non_alphanumeric': 0.07851884898505465, 'fraction_numerical': 0.007584207004238233, 'mean_word_length': 3.332367149758454, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2895870', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1195, 'n_words': 440}