<commit_before>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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 org.wso2.carbon.uuf.internal;

import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import java.util.function.Consumer;

public class EventPublisher<T> {

    private final ServiceTracker serviceTracker;

    public EventPublisher(BundleContext bundleContext, Class<T> listenerType) {
        this.serviceTracker = new ServiceTracker<T, T>(bundleContext, listenerType, null);
        this.serviceTracker.open();
    }

    public void publish(Consumer<T> consumer) {
        for (T service : (T[]) this.serviceTracker.getServices()) {
            consumer.accept(service);
        }
    }

    @Override
    protected void finalize() throws Throwable {
        this.serviceTracker.close();
        super.finalize();
    }
}<commit_msg>added null check for publisher services<commit_after>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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 org.wso2.carbon.uuf.internal;

import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import java.util.function.Consumer;

public class EventPublisher<T> {

    private final ServiceTracker serviceTracker;

    public EventPublisher(BundleContext bundleContext, Class<T> listenerType) {
        this.serviceTracker = new ServiceTracker<T, T>(bundleContext, listenerType, null);
        this.serviceTracker.open();
    }

    public void publish(Consumer<T> consumer) {
        T[] services = (T[]) this.serviceTracker.getServices();
        if (services != null) {
            for (T service : services) {
                consumer.accept(service);
            }
        }
    }

    @Override
    protected void finalize() throws Throwable {
        this.serviceTracker.close();
        super.finalize();
    }
}
{'fraction_non_alphanumeric': 0.09555629802873371, 'fraction_numerical': 0.007350484463748747, 'mean_word_length': 3.264957264957265, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5015710', 'n_tokens_mistral': 833, 'n_tokens_neox': 808, 'n_words': 277}