<commit_before>package com.garpr.android.preferences

import com.garpr.android.preferences.Preference.OnPreferenceChangeListener
import java.lang.ref.WeakReference

abstract class BasePreference<T>(
        override val key: String,
        override val defaultValue: T?
) : Preference<T> {

    private val listeners = mutableSetOf<WeakReference<OnPreferenceChangeListener<T>>>()


    override fun addListener(listener: OnPreferenceChangeListener<T>) {
        cleanListeners()

        synchronized (listeners) {
            listeners.add(WeakReference(listener))
        }
    }

    private fun cleanListeners(listenerToRemove: OnPreferenceChangeListener<T>? = null) {
        synchronized (listeners) {
            val iterator = listeners.iterator()

            while (iterator.hasNext()) {
                val listener = iterator.next().get()

                if (listener == null || listener == listenerToRemove) {
                    iterator.remove()
                }
            }
        }
    }

    protected fun notifyListeners() {
        cleanListeners()

        synchronized(listeners) {
            val iterator = listeners.iterator()

            while (iterator.hasNext()) {
                iterator.next().get()?.onPreferenceChange(this)
            }
        }
    }

    protected abstract fun performSet(newValue: T, notifyListeners: Boolean = true)

    override fun removeListener(listener: OnPreferenceChangeListener<T>) {
        cleanListeners(listener)
    }

    override fun set(preference: Preference<T>, notifyListeners: Boolean) {
        set(preference.get(), notifyListeners)
    }

    override fun set(newValue: T?, notifyListeners: Boolean) {
        if (newValue == null) {
            delete(notifyListeners)
        } else {
            performSet(newValue, notifyListeners)
        }
    }

}<commit_msg>make sure to use WeakReferenceWrapper in BasePreference<commit_after>package com.garpr.android.preferences

import com.garpr.android.preferences.Preference.OnPreferenceChangeListener
import com.garpr.android.wrappers.WeakReferenceWrapper

abstract class BasePreference<T>(
        override val key: String,
        override val defaultValue: T?
) : Preference<T> {

    private val listeners = mutableSetOf<WeakReferenceWrapper<OnPreferenceChangeListener<T>>>()


    override fun addListener(listener: OnPreferenceChangeListener<T>) {
        cleanListeners()

        synchronized (listeners) {
            listeners.add(WeakReferenceWrapper(listener))
        }
    }

    private fun cleanListeners(listenerToRemove: OnPreferenceChangeListener<T>? = null) {
        synchronized (listeners) {
            val iterator = listeners.iterator()

            while (iterator.hasNext()) {
                val listener = iterator.next().get()

                if (listener == null || listener == listenerToRemove) {
                    iterator.remove()
                }
            }
        }
    }

    protected fun notifyListeners() {
        cleanListeners()

        synchronized (listeners) {
            val iterator = listeners.iterator()

            while (iterator.hasNext()) {
                iterator.next().get()?.onPreferenceChange(this)
            }
        }
    }

    protected abstract fun performSet(newValue: T, notifyListeners: Boolean = true)

    override fun removeListener(listener: OnPreferenceChangeListener<T>) {
        cleanListeners(listener)
    }

    override fun set(preference: Preference<T>, notifyListeners: Boolean) {
        set(preference.get(), notifyListeners)
    }

    override fun set(newValue: T?, notifyListeners: Boolean) {
        if (newValue == null) {
            delete(notifyListeners)
        } else {
            performSet(newValue, notifyListeners)
        }
    }

}
{'fraction_non_alphanumeric': 0.09542690985989954, 'fraction_numerical': 0.0, 'mean_word_length': 2.443130118289354, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '4631354', 'n_tokens_mistral': 977, 'n_tokens_neox': 969, 'n_words': 199}