<commit_before>
package com;

import java.util.*;
import java.util.logging.*;
import java.io.*;

import json.*;
import json.streamwriter.*;
import messages.*;
import utilities.*;

public class Dispatcher implements SenderI {
    protected final Set<Endpoint> broadcastEP = new HashSet<>();

    protected static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

    protected final EnumMap<MessageType.MessageGroup, Set<Endpoint>>
        groupEP = new EnumMap<>(MessageType.MessageGroup.class);

    public void addEndpoint(Endpoint ep) {
        Dispatcher.logger.log(
            Level.INFO,
            "try to add endpoint <{0}> appName <{1}> ver<{2}>",
            new Object[]{
                ep.getSocket(),
                ep.getAppName(),
                ep.getVersion().toString()
            }
        );

        Set<Endpoint> set;
        for(MessageType.MessageGroup msgGroup : ep.getMsgGroups()) {
            if(this.groupEP.containsKey(msgGroup)) {
                set = this.groupEP.get(msgGroup);
            } else {
                set = new HashSet();
            }
            set.add(ep);
            this.groupEP.put(msgGroup, set);
        }

        this.broadcastEP.add(ep);
    }

    public void removeEndpoint(Endpoint ep) {
        if(ep.isAlive()) {
            try {
                ep.interrupt();
                ep.join(250);
            } catch(InterruptedException e) {
                Dispatcher.logger.log(
                    Level.WARNING,
                    "InterruptedException occured! <{0}>",
                    new Object[]{e.toString()}
                );
            }
        }
        try {
            ep.closeEndpoint();
        } catch(Exception e) {
            Dispatcher.logger.log(
                Level.WARNING,
                "Exception occured! <{0}> Closing socket failed!",
                new Object[]{e.toString()}
            );
        }

        Iterator<Endpoint> iter = this.broadcastEP.iterator();

        boolean removed = false;

        while(iter.hasNext()) {
            if(iter.next() == ep) {
                iter.remove();
                removed = true;
                break;
            }
        }

        if(!removed) {
            Dispatcher.logger.log(
                Level.WARNING,
                "could not remove endpoint <{0}> from set!",
                new Object[]{ep.getSocket()}
            );
        }

        for(MessageType.MessageGroup msgGroup : ep.getMsgGroups()) {
            if(this.groupEP.containsKey(msgGroup)) {
                Set<Endpoint> set = this.groupEP.get(msgGroup);
                iter = set.iterator();

                while(iter.hasNext()) {
                    if(iter.next() == ep) {
                        iter.remove();
                    }
                }
            }
        }

        this.dispatch(new Message(MessageType.CLIENT_CLOSED, ep));

        Dispatcher.logger.log(
            Level.INFO,
            "endpoint <{0}> succesfully removed!",
            new Object[]{ep.getSocket()}
        );
    }

    public int getEndPointsCount() {
        return this.broadcastEP.size();
    }

    public void resetDispatcher() {
        Iterator<Endpoint> iter = this.broadcastEP.iterator();

        while(iter.hasNext()) {
            this.removeEndpoint(iter.next());
        }
    }

    public Set<Endpoint> getEndpoints() {
        return this.broadcastEP;
    }

    public Endpoint getEndpointByAppId(long appID) {
        for(Endpoint item : this.broadcastEP) {
            if(item.getAppId() == appID) {
                return item;<commit_msg>avoiding ConcurrentModificationException while shutting down<commit_after>
package com;

import java.util.*;
import java.util.logging.*;
import java.io.*;

import json.*;
import json.streamwriter.*;
import messages.*;
import utilities.*;

public class Dispatcher implements SenderI {
    protected final Set<Endpoint> broadcastEP = new HashSet<>();

    protected static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

    protected final EnumMap<MessageType.MessageGroup, Set<Endpoint>>
        groupEP = new EnumMap<>(MessageType.MessageGroup.class);

    public void addEndpoint(Endpoint ep) {
        Dispatcher.logger.log(
            Level.INFO,
            "try to add endpoint <{0}> appName <{1}> ver<{2}>",
            new Object[]{
                ep.getSocket(),
                ep.getAppName(),
                ep.getVersion().toString()
            }
        );

        Set<Endpoint> set;
        for(MessageType.MessageGroup msgGroup : ep.getMsgGroups()) {
            if(this.groupEP.containsKey(msgGroup)) {
                set = this.groupEP.get(msgGroup);
            } else {
                set = new HashSet();
            }
            set.add(ep);
            this.groupEP.put(msgGroup, set);
        }

        this.broadcastEP.add(ep);
    }

    public void removeEndpoint(Endpoint ep) {
        this.shutDownEndpoint(ep);

        Iterator<Endpoint> iter = this.broadcastEP.iterator();

        boolean removed = false;

        while(iter.hasNext()) {
            if(iter.next() == ep) {
                iter.remove();
                removed = true;
                break;
            }
        }

        if(!removed) {
            Dispatcher.logger.log(
                Level.WARNING,
                "could not remove endpoint <{0}> from set!",
                new Object[]{ep.getSocket()}
            );
        }

        for(MessageType.MessageGroup msgGroup : ep.getMsgGroups()) {
            if(this.groupEP.containsKey(msgGroup)) {
                Set<Endpoint> set = this.groupEP.get(msgGroup);
                iter = set.iterator();

                while(iter.hasNext()) {
                    if(iter.next() == ep) {
                        iter.remove();
                    }
                }
            }
        }

        this.dispatch(new Message(MessageType.CLIENT_CLOSED, ep));

        Dispatcher.logger.log(
            Level.INFO,
            "endpoint <{0}> succesfully removed!",
            new Object[]{ep.getSocket()}
        );
    }

    protected void shutDownEndpoint(Endpoint ep) {
        if(ep.isAlive()) {
            try {
                ep.interrupt();
                ep.join(250);
            } catch(InterruptedException e) {
                Dispatcher.logger.log(
                    Level.WARNING,
                    "InterruptedException occured! <{0}>",
                    new Object[]{e.toString()}
                );
            }
        }
        try {
            ep.closeEndpoint();
        } catch(Exception e) {
            Dispatcher.logger.log(
                Level.WARNING,
                "Exception occured! <{0}> Closing socket failed!",
                new Object[]{e.toString()}
            );
        }
    }

    public int getEndPointsCount() {
        return this.broadcastEP.size();
    }

    public void resetDispatcher() {
        Iterator<Endpoint> iter = this.broadcastEP.iterator();

        while(iter.hasNext()) {
            this.shutDownEndpoint(iter.next());
        }
    }

    public Set<Endpoint> getEndpoints() {
        return this.broadcastEP;
    }

    public Endpoint getEndpointByAppId(long appID) {
        for(Endpoint item : this.broadcastEP) {
            if(item.getAppId() == appID) {
                return item;
{'fraction_non_alphanumeric': 0.11755146262188516, 'fraction_numerical': 0.0027085590465872156, 'mean_word_length': 1.5221994535519126, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '3707966', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1912, 'n_words': 378}