<commit_before>package com.uramonk.androidtemplateapp.data.repository

import android.util.Pair
import com.uramonk.androidtemplateapp.Constants
import com.uramonk.androidtemplateapp.data.api.WeatherApi
import com.uramonk.androidtemplateapp.data.entity.mapper.WeatherListEntityDataMapper
import com.uramonk.androidtemplateapp.domain.model.WeatherList
import com.uramonk.androidtemplateapp.domain.repository.WeatherRepository

import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function
import java.util.concurrent.TimeUnit

/**
 * Created by uramonk on 2016/06/29.
 */
class WeatherDataRepository(private val weatherApi: WeatherApi,
        private val weatherListEntityDataMapper: WeatherListEntityDataMapper) : WeatherRepository {

    companion object {
        val RETRY_NUM: Int = 2
        val RETRY_INTERVAL: Long = 2
    }

    override fun getWeatherList(): Observable<WeatherList> {
        return weatherApi.getWeather("TOKYO", Constants.OPEN_WEATHER_MAP_API_KEY).map {
            weatherListEntityDataMapper.transform(it)
        }.retryWhen(retryWhenUnAuthorized())
    }

    private fun retryWhenUnAuthorized(): Function<Observable<out Throwable>, Observable<Any>> {
        return Function { observable ->
            observable.zipWith(Observable.range(0, RETRY_NUM + 1),
                    BiFunction<Throwable, Int, Pair<Throwable, Int>> { throwable, integer ->
                        Pair(throwable, integer)
                    })
                    .flatMap<Any> { throwableIntegerPair ->
                        if (throwableIntegerPair.second < RETRY_NUM) {
                            Observable.timer(RETRY_INTERVAL, TimeUnit.SECONDS)
                        } else {
                            Observable.error(throwableIntegerPair.first)<commit_msg>Rename to retryWhenError<commit_after>package com.uramonk.androidtemplateapp.data.repository

import android.util.Pair
import com.uramonk.androidtemplateapp.Constants
import com.uramonk.androidtemplateapp.data.api.WeatherApi
import com.uramonk.androidtemplateapp.data.entity.mapper.WeatherListEntityDataMapper
import com.uramonk.androidtemplateapp.domain.model.WeatherList
import com.uramonk.androidtemplateapp.domain.repository.WeatherRepository

import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function
import java.util.concurrent.TimeUnit

/**
 * Created by uramonk on 2016/06/29.
 */
class WeatherDataRepository(private val weatherApi: WeatherApi,
        private val weatherListEntityDataMapper: WeatherListEntityDataMapper) : WeatherRepository {

    companion object {
        val RETRY_NUM: Int = 2
        val RETRY_INTERVAL: Long = 2
    }

    override fun getWeatherList(): Observable<WeatherList> {
        return weatherApi.getWeather("TOKYO", Constants.OPEN_WEATHER_MAP_API_KEY).map {
            weatherListEntityDataMapper.transform(it)
        }.retryWhen(retryWhenError())
    }

    private fun retryWhenError(): Function<Observable<out Throwable>, Observable<Any>> {
        return Function { observable ->
            observable.zipWith(Observable.range(0, RETRY_NUM + 1),
                    BiFunction<Throwable, Int, Pair<Throwable, Int>> { throwable, integer ->
                        Pair(throwable, integer)
                    })
                    .flatMap<Any> { throwableIntegerPair ->
                        if (throwableIntegerPair.second < RETRY_NUM) {
                            Observable.timer(RETRY_INTERVAL, TimeUnit.SECONDS)
                        } else {
                            Observable.error(throwableIntegerPair.first)
{'fraction_non_alphanumeric': 0.08249112264408631, 'fraction_numerical': 0.0065555859054903035, 'mean_word_length': 2.976112920738328, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '5111329', 'n_tokens_mistral': 1026, 'n_tokens_neox': 974, 'n_words': 171}