<commit_before>package com.garpr.android.settings;


import com.crashlytics.android.Crashlytics;
import com.garpr.android.misc.Console;
import com.garpr.android.misc.Constants;
import com.garpr.android.misc.Heartbeat;
import com.garpr.android.misc.Utils;
import com.garpr.android.models.Region;
import com.garpr.android.settings.Settings.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.LinkedList;


public final class RegionSetting extends Setting<Region> {


    private static final String TAG = "RegionSetting";

    private final JSONSetting mJSONSetting;
    private final LinkedList<Attachment> mListeners;




    RegionSetting(final String name, final String key) {
        super(name, key);
        mJSONSetting = new JSONSetting(name, key);
        mListeners = new LinkedList<>();
    }


    public void attachListener(final Heartbeat heartbeat, final RegionListener listener) {
        if (Utils.areAnyObjectsNull(heartbeat, listener)) {
            throw new RuntimeException();
        }

        synchronized (mListeners) {
            boolean attachListener = true;

            for (int i = 0; i < mListeners.size(); ) {
                final Attachment attachment = mListeners.get(i);

                if (attachment.isAlive()) {
                    if (attachment.hasListener(listener)) {
                        attachListener = false;
                    }

                    ++i;
                } else {
                    mListeners.remove(i);
                }
            }

            if (!heartbeat.isAlive()) {
                Console.w(TAG, "Attempted to attach a listener with a dead heartbeat");
                return;
            }

            if (attachListener) {
                mListeners.add(new Attachment(heartbeat, listener));
            }

            Console.d(TAG, "There are now " + mListeners.size() + " region listener(s)");
        }
    }


    @Override
    public void delete() {
        throw new UnsupportedOperationException();
    }


    public void detachListener(final RegionListener listener) {
        synchronized (mListeners) {
            for (int i = 0; i < mListeners.size(); ) {
                final Attachment attachment = mListeners.get(i);

                if (!attachment.isAlive() || attachment.hasListener(listener)) {
                    mListeners.remove(i);
                } else {
                    ++i;
                }
            }

            Console.d(TAG, "There are now " + mListeners.size() + " region listener(s)");
        }
    }


    @Override
    public Region get() {
        final JSONObject json = mJSONSetting.get();
        final Region region;<commit_msg>began moving to iterators<commit_after>package com.garpr.android.settings;


import com.crashlytics.android.Crashlytics;
import com.garpr.android.misc.Console;
import com.garpr.android.misc.Constants;
import com.garpr.android.misc.Heartbeat;
import com.garpr.android.misc.Utils;
import com.garpr.android.models.Region;
import com.garpr.android.settings.Settings.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedList;


public final class RegionSetting extends Setting<Region> {


    private static final String TAG = "RegionSetting";

    private final JSONSetting mJSONSetting;
    private final LinkedList<Attachment> mListeners;




    RegionSetting(final String name, final String key) {
        super(name, key);
        mJSONSetting = new JSONSetting(name, key);
        mListeners = new LinkedList<>();
    }


    public void attachListener(final Heartbeat heartbeat, final RegionListener listener) {
        if (Utils.areAnyObjectsNull(heartbeat, listener)) {
            throw new RuntimeException();
        }

        synchronized (mListeners) {
            boolean attachListener = true;
            final Iterator<Attachment> iterator = mListeners.iterator();

            while (iterator.hasNext()) {
                final Attachment attachment = iterator.next();

                if (attachment.isAlive()) {
                    if (attachment.hasListener(listener)) {
                        attachListener = false;
                    }
                } else {
                    iterator.remove();
                }
            }

            if (!heartbeat.isAlive()) {
                Console.w(TAG, "Attempted to attach a listener (" + listener.toString()
                        + ") with a dead heartbeat");
                return;
            }

            if (attachListener) {
                mListeners.add(new Attachment(heartbeat, listener));
                Console.d(TAG, "Attached " + listener.toString() + ", there are now "
                        + mListeners.size() + " region listener(s)");
            } else {
                Console.d(TAG, "Didn't need to attach " + listener.toString() +
                        " as it was already attached");
            }
        }
    }


    @Override
    public void delete() {
        throw new UnsupportedOperationException();
    }


    public void detachListener(final RegionListener listener) {
        synchronized (mListeners) {
            for (int i = 0; i < mListeners.size(); ) {
                final Attachment attachment = mListeners.get(i);

                if (!attachment.isAlive() || attachment.hasListener(listener)) {
                    mListeners.remove(i);
                } else {
                    ++i;
                }
            }

            Console.d(TAG, "There are now " + mListeners.size() + " region listener(s)");
        }
    }


    @Override
    public Region get() {
        final JSONObject json = mJSONSetting.get();
        final Region region;
{'fraction_non_alphanumeric': 0.09984533424987112, 'fraction_numerical': 0.0005155525004296271, 'mean_word_length': 1.9172932330827068, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 10, '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': '986503', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1468, 'n_words': 358}