<commit_before>package com.tinsuke.icekick

import android.app.Activity
import android.app.Fragment
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import java.io.Serializable
import java.util.*

private class IceKick {

    companion object {

        data class SavedProperties(
                val properties: MutableList<SavedProperty<*, *>> = mutableListOf(),
                val nullableProperties: MutableList<NullableSavedProperty<*, *>> = mutableListOf(),
                val lateinitProperties: MutableList<LateinitSavedProperty<*, *>> = mutableListOf()
        )

        val savedInstances = WeakHashMap<Any, SavedProperties>()

        private fun createSavedProperties(instance: Any): SavedProperties {
            var savedProperties = savedInstances[instance]
            if (savedProperties == null) {
                savedProperties = SavedProperties()
                savedInstances[instance] = savedProperties
            }
            return savedProperties
        }

        fun <S : Serializable> state(instance: Any): NullableSavedProperty<Any, S> {
            val savedProperties = createSavedProperties(instance)
            val property = NullableSavedProperty<Any, S>()
            savedProperties.nullableProperties.add(property)
            return property
        }

        fun <S : Serializable> state(instance: Any, value: S): SavedProperty<Any, S> {
            val savedProperties = createSavedProperties(instance)
            val property = SavedProperty<Any, S>(value)
            savedProperties.properties.add(property)
            return property
        }

        fun <S : Serializable> latestate(instance: Any): LateinitSavedProperty<Any, S> {
            val savedProperties = createSavedProperties(instance)
            val property = LateinitSavedProperty<Any, S>()
            savedProperties.lateinitProperties.add(property)
            return property
        }

        fun freezeInstanceState(instance: Any, outState: Bundle) {
            val savedProperties = IceKick.savedInstances[instance] ?: return
            for ((index, property) in savedProperties.properties.withIndex()) {
                outState.putSerializable("iceKick_$index", property.value)
            }
            for ((index, property) in savedProperties.nullableProperties.withIndex()) {
                outState.putSerializable("iceKick_nullable_$index", property.value)
            }
            for ((index, property) in savedProperties.lateinitProperties.withIndex()) {
                outState.putSerializable("iceKick_lateinit_$index", property.value)
            }
        }

        fun unfreezeInstanceState(instance: Any, savedInstanceState: Bundle?) {
            if (savedInstanceState == null) {
                return
            }

            val savedProperties = IceKick.savedInstances[instance] ?: return
            for ((index, property) in savedProperties.properties.withIndex()) {
                property.value = savedInstanceState.getSerializable("iceKick_$index") ?: continue
            }
            for ((index, property) in savedProperties.nullableProperties.withIndex()) {
                property.value = savedInstanceState.getSerializable("iceKick_nullable_$index")
            }
            for ((index, property) in savedProperties.lateinitProperties.withIndex()) {
                property.value = savedInstanceState.getSerializable("iceKick_lateinit_$index") ?: continue
            }
        }
    }

}

fun <S : Serializable> Activity.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> Activity.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> Activity.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)
fun Activity.freezeInstanceState(outState: Bundle) = IceKick.freezeInstanceState(this, outState)
fun Activity.unfreezeInstanceState(savedInstanceState: Bundle?) = IceKick.unfreezeInstanceState(this, savedInstanceState)

fun <S : Serializable> Fragment.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> Fragment.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> Fragment.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)
fun Fragment.freezeInstanceState(outState: Bundle) = IceKick.freezeInstanceState(this, outState)
fun Fragment.unfreezeInstanceState(savedInstanceState: Bundle?) = IceKick.unfreezeInstanceState(this, savedInstanceState)

fun <S : Serializable> android.support.v4.app.Fragment.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> android.support.v4.app.Fragment.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> android.support.v4.app.Fragment.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)
fun android.support.v4.app.Fragment.freezeInstanceState(outState: Bundle) = IceKick.freezeInstanceState(this, outState)
fun android.support.v4.app.Fragment.unfreezeInstanceState(savedInstanceState: Bundle?) = IceKick.unfreezeInstanceState(this, savedInstanceState)

fun <S : Serializable> View.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> View.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> View.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)

fun View.freezeInstanceState(parcelable: Parcelable?): Parcelable? {
    if (IceKick.savedInstances[this] == null) {
        return parcelable
    }

    val state = Bundle()
    IceKick.freezeInstanceState(this, state)
    return ViewState(state, parcelable)
}

fun View.unfreezeInstanceState(parcelable: Parcelable): Parcelable? {
    if (IceKick.savedInstances[this] == null || parcelable !is ViewState) {
        return parcelable
    }

    IceKick.unfreezeInstanceState(this, parcelable.state)
    return parcelable.parcelable
}

<commit_msg>IceKick made an object itself, instead of having a companion object + using stdlib methods where possible + using more Android-optimized iteration (forEachIndexed)<commit_after>package com.tinsuke.icekick

import android.app.Activity
import android.app.Fragment
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import java.io.Serializable
import java.util.*

object IceKick {
    private const val PROPERTY_PREFIX = "iceKick_"
    private const val NULLABLE_PREFIX = "iceKick_nullable_"
    private const val LATEINIT_PREFIX = "iceKick_lateinit_"

    internal data class SavedProperties(
            val properties: MutableList<SavedProperty<*, *>> = mutableListOf(),
            val nullableProperties: MutableList<NullableSavedProperty<*, *>> = mutableListOf(),
            val lateinitProperties: MutableList<LateinitSavedProperty<*, *>> = mutableListOf()
    )

    internal val savedInstances = WeakHashMap<Any, SavedProperties>()

    private fun createOrGetSavedProperties(instance: Any) = savedInstances.getOrPut(instance) { SavedProperties() }

    fun <S : Serializable> state(instance: Any) = NullableSavedProperty<Any, S>().apply {
        createOrGetSavedProperties(instance).nullableProperties.add(this)
    }

    fun <S : Serializable> state(instance: Any, value: S) = SavedProperty<Any, S>(value).apply {
        createOrGetSavedProperties(instance).properties.add(this)
    }

    fun <S : Serializable> latestate(instance: Any) = LateinitSavedProperty<Any, S>().apply {
        createOrGetSavedProperties(instance).lateinitProperties.add(this)
    }

    fun freezeInstanceState(instance: Any, outState: Bundle) {
        val savedProperties = IceKick.savedInstances[instance] ?: return
        savedProperties.properties.forEachIndexed { index, property ->
            outState.putSerializable("$PROPERTY_PREFIX$index", property.value)
        }
        savedProperties.nullableProperties.forEachIndexed { index, property ->
            outState.putSerializable("$NULLABLE_PREFIX$index", property.value)
        }
        savedProperties.lateinitProperties.forEachIndexed { index, property ->
            outState.putSerializable("$LATEINIT_PREFIX$index", property.value)
        }
    }

    fun unfreezeInstanceState(instance: Any, savedInstanceState: Bundle?) {
        if (savedInstanceState == null) {
            return
        }

        val savedProperties = IceKick.savedInstances[instance] ?: return
        savedProperties.properties.forEachIndexed { index, property ->
            property.value = savedInstanceState.getSerializable("$PROPERTY_PREFIX$index") ?: return@forEachIndexed
        }
        savedProperties.nullableProperties.forEachIndexed { index, property ->
            property.value = savedInstanceState.getSerializable("$NULLABLE_PREFIX$index")
        }
        savedProperties.lateinitProperties.forEachIndexed { index, property ->
            property.value = savedInstanceState.getSerializable("$LATEINIT_PREFIX$index") ?: return@forEachIndexed
        }
    }
}

fun <S : Serializable> Activity.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> Activity.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> Activity.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)
fun Activity.freezeInstanceState(outState: Bundle) = IceKick.freezeInstanceState(this, outState)
fun Activity.unfreezeInstanceState(savedInstanceState: Bundle?) = IceKick.unfreezeInstanceState(this, savedInstanceState)

fun <S : Serializable> Fragment.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> Fragment.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> Fragment.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)
fun Fragment.freezeInstanceState(outState: Bundle) = IceKick.freezeInstanceState(this, outState)
fun Fragment.unfreezeInstanceState(savedInstanceState: Bundle?) = IceKick.unfreezeInstanceState(this, savedInstanceState)

fun <S : Serializable> android.support.v4.app.Fragment.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> android.support.v4.app.Fragment.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> android.support.v4.app.Fragment.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)
fun android.support.v4.app.Fragment.freezeInstanceState(outState: Bundle) = IceKick.freezeInstanceState(this, outState)
fun android.support.v4.app.Fragment.unfreezeInstanceState(savedInstanceState: Bundle?) = IceKick.unfreezeInstanceState(this, savedInstanceState)

fun <S : Serializable> View.state(): NullableSavedProperty<Any, S> = IceKick.state(this)
fun <S : Serializable> View.state(value: S): SavedProperty<Any, S> = IceKick.state(this, value)
fun <S : Serializable> View.latestate(): LateinitSavedProperty<Any, S> = IceKick.latestate(this)

fun View.freezeInstanceState(parcelable: Parcelable?): Parcelable? {
    if (IceKick.savedInstances[this] == null) {
        return parcelable
    }

    val state = Bundle()
    IceKick.freezeInstanceState(this, state)
    return ViewState(state, parcelable)
}

fun View.unfreezeInstanceState(parcelable: Parcelable): Parcelable? {
    if (IceKick.savedInstances[this] == null || parcelable !is ViewState) {
        return parcelable
    }

    IceKick.unfreezeInstanceState(this, parcelable.state)
    return parcelable.parcelable
}


{'fraction_non_alphanumeric': 0.10604481500781657, 'fraction_numerical': 0.0008685079034219212, 'mean_word_length': 4.043802014892685, 'pattern_counts': {'":': 0, '<': 80, '<?xml version=': 0, '>': 86, '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': '3951242', 'n_tokens_mistral': 3356, 'n_tokens_neox': 3218, 'n_words': 646}