<commit_before>package de.stephanlindauer.criticalmaps.model;

import de.stephanlindauer.criticalmaps.interfaces.IChatMessage;
import de.stephanlindauer.criticalmaps.model.chat.OutgoingChatMessage;
import de.stephanlindauer.criticalmaps.model.chat.ReceivedChatMessage;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import okhttp3.internal.Util;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class ChatModel {

    private final List<OutgoingChatMessage> outgoingMassages = new ArrayList<>();

    private List<ReceivedChatMessage> chatMessages = new ArrayList<>();

    @Inject
    public ChatModel() {
    }

    public void setFromJson(JSONObject jsonObject) throws JSONException,
            UnsupportedEncodingException {

        if (chatMessages == null) {
            chatMessages = new ArrayList<>(jsonObject.length());
        } else {
            chatMessages.clear();
        }

        Iterator<String> keys = jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            JSONObject value = jsonObject.getJSONObject(key);
            String message = URLDecoder.decode(value.getString("message"), Util.UTF_8.name());
            Date timestamp = new Date(Long.parseLong(value.getString("timestamp")) * 1000);

            //for i going backwards to delete without side-effects
            for (int i = outgoingMassages.size() - 1; i > -1; i--) {
                OutgoingChatMessage outgoingMessageToMaybeDelete = outgoingMassages.get(i);
                if (outgoingMessageToMaybeDelete.getIdentifier().equals(key)) {
                    outgoingMassages.remove(i);
                }
            }

            chatMessages.add(new ReceivedChatMessage(message, timestamp));
        }

        Collections.sort(chatMessages, new Comparator<ReceivedChatMessage>() {
            @Override
            public int compare(ReceivedChatMessage oneChatMessages, ReceivedChatMessage otherChatMessage) {
                return oneChatMessages.getTimestamp().compareTo(otherChatMessage.getTimestamp());
            }
        });
    }

    public void setNewOutgoingMessage(OutgoingChatMessage newOutgoingMessage) {
        outgoingMassages.add(newOutgoingMessage);
    }

    public JSONArray getOutgoingMessagesAsJson() {
        JSONArray jsonArray = new JSONArray();

        for (int i = 0, size = outgoingMassages.size(); i < size; i++) {
            OutgoingChatMessage outgoingChatMessage = outgoingMassages.get(i);
            try {
                JSONObject messageObject = new JSONObject();
                messageObject.put("text", outgoingChatMessage.getUrlEncodedMessage());
                messageObject.put("timestamp", outgoingChatMessage.getTimestamp().getTime());
                messageObject.put("identifier", outgoingChatMessage.getIdentifier());
                jsonArray.put(messageObject);
            } catch (JSONException ignored) {
            }
        }
        return jsonArray;
    }

    public ArrayList<IChatMessage> getSavedAndOutgoingMessages() {
        int mergedListsSize = chatMessages.size() + outgoingMassages.size();
        ArrayList<IChatMessage> mergeArrayList = new ArrayList<>(mergedListsSize);
        mergeArrayList.addAll(chatMessages);
        mergeArrayList.addAll(outgoingMassages);
        return mergeArrayList;
    }

    public boolean hasOutgoingMessages() {
        return !outgoingMassages.isEmpty();<commit_msg>ChatModel: slight changes<commit_after>package de.stephanlindauer.criticalmaps.model;

import de.stephanlindauer.criticalmaps.interfaces.IChatMessage;
import de.stephanlindauer.criticalmaps.model.chat.OutgoingChatMessage;
import de.stephanlindauer.criticalmaps.model.chat.ReceivedChatMessage;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import okhttp3.internal.Util;
import timber.log.Timber;

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

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class ChatModel {

    private final List<OutgoingChatMessage> outgoingMessages = new ArrayList<>();
    private List<ReceivedChatMessage> chatMessages = new ArrayList<>();

    @Inject
    public ChatModel() {
    }

    public void setFromJson(JSONObject jsonObject) throws JSONException,
            UnsupportedEncodingException {
        chatMessages = new ArrayList<>(jsonObject.length());

        Iterator<String> identifiers = jsonObject.keys();
        while (identifiers.hasNext()) {
            String identifier = identifiers.next();
            JSONObject value = jsonObject.getJSONObject(identifier);
            String message = URLDecoder.decode(value.getString("message"), Util.UTF_8.name());
            Date timestamp = new Date(Long.parseLong(value.getString("timestamp")) * 1000);

            Iterator<OutgoingChatMessage> outgoingChatMessageIterator = outgoingMessages.iterator();
            while (outgoingChatMessageIterator.hasNext()) {
                if (outgoingChatMessageIterator.next().getIdentifier().equals(identifier)) {
                    outgoingChatMessageIterator.remove();
                }
            }

            chatMessages.add(new ReceivedChatMessage(message, timestamp));
        }

        Collections.sort(chatMessages, new Comparator<ReceivedChatMessage>() {
            @Override
            public int compare(ReceivedChatMessage oneChatMessages,
                               ReceivedChatMessage otherChatMessage) {
                return oneChatMessages.getTimestamp().compareTo(otherChatMessage.getTimestamp());
            }
        });
    }

    public void setNewOutgoingMessage(OutgoingChatMessage newOutgoingMessage) {
        outgoingMessages.add(newOutgoingMessage);
    }

    public JSONArray getOutgoingMessagesAsJson() {
        JSONArray jsonArray = new JSONArray();

        for (OutgoingChatMessage outgoingChatMessage : outgoingMessages) {
            try {
                JSONObject messageObject = new JSONObject();
                messageObject.put("text", outgoingChatMessage.getUrlEncodedMessage());
                messageObject.put("timestamp", outgoingChatMessage.getTimestamp().getTime());
                messageObject.put("identifier", outgoingChatMessage.getIdentifier());
                jsonArray.put(messageObject);
            } catch (JSONException e) {
                Timber.d(e);
            }
        }
        return jsonArray;
    }

    public ArrayList<IChatMessage> getSavedAndOutgoingMessages() {
        int mergedListsSize = chatMessages.size() + outgoingMessages.size();
        ArrayList<IChatMessage> mergeArrayList = new ArrayList<>(mergedListsSize);
        mergeArrayList.addAll(chatMessages);
        mergeArrayList.addAll(outgoingMessages);
        return mergeArrayList;
    }

    public boolean hasOutgoingMessages() {
        return !outgoingMessages.isEmpty();
{'fraction_non_alphanumeric': 0.09510204081632653, 'fraction_numerical': 0.0020408163265306124, 'mean_word_length': 3.056843267108168, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '5528554', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1809, 'n_words': 351}