<commit_before>package de.qabel.ackack;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by tox on 11/24/14.
 */
public class Actor implements Runnable {

    static private Map<Object, Actor> actors = Collections.synchronizedMap(new HashMap<Object, Actor>());

    private LinkedBlockingQueue<Event<?>> inQueue = new LinkedBlockingQueue<Event<?>>();

    public Registration<?> getDefaultReactor() {
        return defaultReactor;
    }

    public void setDefaultReactor(Registration<?> defaultReactor) {
        this.defaultReactor = defaultReactor;
    }

    private Registration<?> defaultReactor = null;

    static class Registration<T> {
        public Class<T> dataClass;
        public Reactor<T> reactor;
    }

    private Map<String, Registration<?>> reactors = new HashMap<String, Registration<?>>();

    public boolean isRunning() {
        return running;
    }

    public void stop() {
        this.running = false;
    }

    private boolean running;

    public <T> boolean send(Object receiver, String event, T data) {
        Event<T> e = new Event<T>();
        e.setData(data);
        e.setTime(System.currentTimeMillis());
        Actor actor = null;
        if(receiver instanceof Actor) {
            actor = ((Actor)receiver);
        }
        else {
            actor = actors.get(receiver);
        }

        if(actor == null) {
            return false;
        }

        actor.queue(e.clone());
        return true;
    }

    private void queue(Event<?> e) {
        try {
            inQueue.put(e);
        } catch (InterruptedException e1) {
            // TODO Log exception
            e1.printStackTrace();
        }
    }

    public void run() {
        Registration<?> registration;
        Event<?> e;

        try {
            while(isRunning()) {
                e = inQueue.take();
                registration = reactors.getOrDefault(e.getName(), defaultReactor);
                if(registration == null)
                    continue;
                if(registration.dataClass.isAssignableFrom(e.getData().getClass())) {
                    // TODO Fix Warning
                    registration.reactor.onEvent((Event)e);
                }
                // TODO Throw error if wrong datatype is given.
            }
        } catch (InterruptedException ex) {
            // TODO Log exception
            stop();
        }
    }

    public <T> void register(String event, Class<T> dataClass, Reactor<T> reactor) {
        Registration<T> registration = new Registration<T>();
        registration.dataClass = dataClass;
        registration.reactor = reactor;
        reactors.put(event, registration);
    }

    public <T> void registerDefault(Class<T> dataClass, Reactor<T> reactor) {
        Registration<T> registration = new Registration<T>();
        registration.dataClass = dataClass;
        registration.reactor = reactor;
        defaultReactor = registration;
    }
}
<commit_msg>moved variables, added checks and an annotation<commit_after>package de.qabel.ackack;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by tox on 11/24/14.
 */
public class Actor implements Runnable {

    static private Map<Object, Actor> actors = Collections.synchronizedMap(new HashMap<Object, Actor>());
    private LinkedBlockingQueue<Event<?>> inQueue = new LinkedBlockingQueue<Event<?>>();
    private Map<String, Registration<?>> reactors = new HashMap<String, Registration<?>>();
    private Registration<?> defaultReactor = null;
    private boolean running;

    public Registration<?> getDefaultReactor() {
        return defaultReactor;
    }

    public void setDefaultReactor(Registration<?> defaultReactor) {
        this.defaultReactor = defaultReactor;
    }

    static class Registration<T> {
        public Class<T> dataClass;
        public Reactor<T> reactor;
    }

    public boolean isRunning() {
        return running;
    }

    public void stop() {
        this.running = false;
    }

    public <T> boolean send(Object receiver, String event, T data) {
        Event<T> e = new Event<T>();
        e.setData(data);
        e.setTime(System.currentTimeMillis());
        Actor actor = null;
        if(receiver instanceof Actor) {
            actor = ((Actor)receiver);
        }
        else {
            actor = actors.get(receiver);
        }

        if(actor == null) {
            return false;
        }

        actor.queue(e.clone());
        return true;
    }

    private void queue(Event<?> e) {
        try {
            inQueue.put(e);
        } catch (InterruptedException e1) {
            // TODO Log exception
            e1.printStackTrace();
        }
    }

    @Override
    public void run() {
        Registration<?> registration;
        Event<?> e;

        try {
            while(isRunning()) {
                e = inQueue.take();
                registration = reactors.get(e.getName());
                if (registration == null && defaultReactor != null)
                    registration = defaultReactor;
                else
                    continue;
                if(registration.dataClass.isAssignableFrom(e.getData().getClass())) {
                    // TODO Fix Warning
                    registration.reactor.onEvent((Event)e);
                }
                // TODO Throw error if wrong datatype is given.
            }
        } catch (InterruptedException ex) {
            // TODO Log exception
            stop();
        }
    }

    public <T> void register(String event, Class<T> dataClass, Reactor<T> reactor) {
        Registration<T> registration = new Registration<T>();
        registration.dataClass = dataClass;
        registration.reactor = reactor;
        reactors.put(event, registration);
    }

    public <T> void registerDefault(Class<T> dataClass, Reactor<T> reactor) {
        Registration<T> registration = new Registration<T>();
        registration.dataClass = dataClass;
        registration.reactor = reactor;
        defaultReactor = registration;
    }
}

{'fraction_non_alphanumeric': 0.10905283505154639, 'fraction_numerical': 0.002577319587628866, 'mean_word_length': 2.0737623762376236, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, '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': '7646810', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1607, 'n_words': 404}