<commit_before>package sourcecoded.events;

import sourcecoded.events.annotation.EventListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;

/**
 * The EventBus for the SourceEvents API. This is used to register handlers and raise events
 *
 * @author SourceCoded
 */
public class EventBus {

    private ArrayList<Object> listeners = new ArrayList<Object>();

    private boolean exclusive = false;

    /**
     * Register either a Class or Instance of an Object as a handler for the eventsystem
     * <p/>
     * Instances will allow you to access non-static methods
     * Classes can only access Static methods
     */
    public void register(Object object) {
        listeners.add(object);
    }

    /**
     * Unregister an object from the handlers list
     */
    public void unregister(Object object) {
        listeners.remove(object);
    }

    /**
     * Unregister all handlers of this type
     */
    public void unregisterAllOfType(Class type) {
        for (Object curr : listeners.toArray()) {
            Class currClass;
            if (curr instanceof Class)
                currClass = (Class) curr;
            else currClass = curr.getClass();

            if (currClass == type)
                listeners.remove(curr);

        }
    }

    /**
     * Exclusivity setting.
     * <p/>
     * Set this to true if you want instances to ONLY access non-static methods. By default, this is set to false, meaning instances can access static methods
     */
    public void setExclusive(boolean val) {
        exclusive = val;
    }

    /**
     * Get the listener list in the form of a read-only list
     */
    public List<Object> getListeners() {
        return Collections.unmodifiableList(listeners);
    }

    /**
     * Raise an event to all the available listeners
     */
    public void raiseEvent(final AbstractEvent event) {
        new Thread() {
            public void run() {
                try {
                    raise(event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void raise(final AbstractEvent event) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        for (Object currentHandler : listeners) {
            Method[] methods;
            boolean isClass = false;


            if (currentHandler instanceof Class) {
                methods = ((Class) currentHandler).getMethods();
                isClass = true;
            } else {
                methods = currentHandler.getClass().getMethods();
            }

            ArrayList<Method> valid = new ArrayList<Method>();

            for (Method method : methods) {
                EventListener annotation = method.getAnnotation(EventListener.class);
                if (annotation == null)
                    continue;

                Class[] params = method.getParameterTypes();

                if (params.length != 1)
                    continue;

//                if (!event.getClass().getSimpleName().equals(params[0].getSimpleName()))
//                    continue;

                if (annotation.respectsInheritance() && !params[0].isAssignableFrom(event.getClass()))
                    continue;

                if (!annotation.respectsInheritance() && params[0] != event.getClass())
                    continue;


                if (!isClass && exclusive && Modifier.isStatic(method.getModifiers())) continue;

                if (isClass && !Modifier.isStatic(method.getModifiers())) continue;

                valid.add(method);
            }

            Comparator<Method> comp = new Comparator<Method>() {
                @Override
                public int compare(Method o1, Method o2) {
                    EventListener a1 = o1.getAnnotation(EventListener.class);
                    EventListener a2 = o2.getAnnotation(EventListener.class);
<commit_msg>Added a non-concurrent event raiser<commit_after>package sourcecoded.events;

import sourcecoded.events.annotation.EventListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;

/**
 * The EventBus for the SourceEvents API. This is used to register handlers and raise events
 *
 * @author SourceCoded
 */
public class EventBus {

    private ArrayList<Object> listeners = new ArrayList<Object>();

    private boolean exclusive = false;

    /**
     * Register either a Class or Instance of an Object as a handler for the eventsystem
     * <p/>
     * Instances will allow you to access non-static methods
     * Classes can only access Static methods
     */
    public void register(Object object) {
        listeners.add(object);
    }

    /**
     * Unregister an object from the handlers list
     */
    public void unregister(Object object) {
        listeners.remove(object);
    }

    /**
     * Unregister all handlers of this type
     */
    public void unregisterAllOfType(Class type) {
        for (Object curr : listeners.toArray()) {
            Class currClass;
            if (curr instanceof Class)
                currClass = (Class) curr;
            else currClass = curr.getClass();

            if (currClass == type)
                listeners.remove(curr);

        }
    }

    /**
     * Exclusivity setting.
     * <p/>
     * Set this to true if you want instances to ONLY access non-static methods. By default, this is set to false, meaning instances can access static methods
     */
    public void setExclusive(boolean val) {
        exclusive = val;
    }

    /**
     * Get the listener list in the form of a read-only list
     */
    public List<Object> getListeners() {
        return Collections.unmodifiableList(listeners);
    }

    /**
     * Raise an event to all the available listeners in a new thread
     */
    public void raiseEventConcurrent(final AbstractEvent event) {
        new Thread() {
            public void run() {
                try {
                    raise(event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * Raise an event to all available listeners
     */
    public void raiseEvent(final AbstractEvent event) {
        try {
            raise(event);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void raise(final AbstractEvent event) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        for (Object currentHandler : (ArrayList) listeners.clone()) {
            Method[] methods;
            boolean isClass = false;


            if (currentHandler instanceof Class) {
                methods = ((Class) currentHandler).getMethods();
                isClass = true;
            } else {
                methods = currentHandler.getClass().getMethods();
            }

            ArrayList<Method> valid = new ArrayList<Method>();

            for (Method method : methods) {
                EventListener annotation = method.getAnnotation(EventListener.class);
                if (annotation == null)
                    continue;

                Class[] params = method.getParameterTypes();

                if (params.length != 1)
                    continue;

                if (annotation.respectsInheritance() && !params[0].isAssignableFrom(event.getClass()))
                    continue;

                if (!annotation.respectsInheritance() && params[0] != event.getClass())
                    continue;


                if (!isClass && exclusive && Modifier.isStatic(method.getModifiers())) continue;

                if (isClass && !Modifier.isStatic(method.getModifiers())) continue;

                valid.add(method);
            }

            Comparator<Method> comp = new Comparator<Method>() {
                @Override
                public int compare(Method o1, Method o2) {
                    EventListener a1 = o1.getAnnotation(EventListener.class);
                    EventListener a2 = o2.getAnnotation(EventListener.class);

{'fraction_non_alphanumeric': 0.09253659126648119, 'fraction_numerical': 0.0022982944236119513, 'mean_word_length': 1.8878798463150541, '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': '3382234', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1951, 'n_words': 594}