<commit_before>/*
 * Copyright 2015-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.intellij.ideabuck.ws.buckevents;

import com.facebook.buck.event.external.events.BuckEventExternalInterface;
import com.facebook.buck.intellij.ideabuck.ws.buckevents.consumers.BuckEventsConsumerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.intellij.openapi.application.ApplicationManager;

import java.io.IOException;

public class BuckEventsQueue implements BuckEventsQueueInterface {

    private final BuckEventsConsumerFactory mFactory;
    private ObjectMapper mObjectMapper;
    private BuckEventsAdapter mBuckEventsAdapter;

    public BuckEventsQueue(ObjectMapper objectMapper, BuckEventsConsumerFactory factory) {
        mFactory = factory;
        mObjectMapper = objectMapper;
        mBuckEventsAdapter = new BuckEventsAdapter();
    }

    @Override
    public void add(String rawMessage, BuckEventExternalInterface event) {
        handleEvent(rawMessage, event);
    }

    private void handleEvent(final String rawMessage, final BuckEventExternalInterface event) {
        ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
            @Override
            public void run() {
                synchronized (BuckEventsQueue.this.mFactory) {
                    try {
                        mBuckEventsAdapter.get(
                            event.getEventName()).handleEvent(
                            rawMessage,
                            event,
                            mFactory,
                            mObjectMapper);
                    } catch (IOException e) {
                        // TODO(cosmin1123) Handle exception
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}<commit_msg>Fix NPE when an unrecognized event was encountered.<commit_after>/*
 * Copyright 2015-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.intellij.ideabuck.ws.buckevents;

import com.facebook.buck.event.external.events.BuckEventExternalInterface;
import com.facebook.buck.intellij.ideabuck.ws.buckevents.consumers.BuckEventsConsumerFactory;
import com.facebook.buck.intellij.ideabuck.ws.buckevents.handlers.BuckEventHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;

import java.io.IOException;

public class BuckEventsQueue implements BuckEventsQueueInterface {

    private static final Logger LOG = Logger.getInstance(BuckEventsQueue.class);

    private final BuckEventsConsumerFactory mFactory;
    private ObjectMapper mObjectMapper;
    private BuckEventsAdapter mBuckEventsAdapter;

    public BuckEventsQueue(ObjectMapper objectMapper, BuckEventsConsumerFactory factory) {
        mFactory = factory;
        mObjectMapper = objectMapper;
        mBuckEventsAdapter = new BuckEventsAdapter();
    }

    @Override
    public void add(String rawMessage, BuckEventExternalInterface event) {
        handleEvent(rawMessage, event);
    }

    private void handleEvent(final String rawMessage, final BuckEventExternalInterface event) {
        ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
            @Override
            public void run() {
                synchronized (BuckEventsQueue.this.mFactory) {
                    try {
                        String eventName = event.getEventName();
                        BuckEventHandler buckEventHandler = mBuckEventsAdapter.get(eventName);
                        if (buckEventHandler == null) {
                            LOG.warn("Unhandled event '" + eventName + "': " + rawMessage);
                        } else {
                            buckEventHandler.handleEvent(
                                rawMessage,
                                event,
                                mFactory,
                                mObjectMapper);
                        }
                    } catch (IOException e) {
                        // TODO(cosmin1123) Handle exception
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
{'fraction_non_alphanumeric': 0.0704014939309057, 'fraction_numerical': 0.004481792717086834, 'mean_word_length': 2.310259579728059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5331459', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1238, 'n_words': 352}