<commit_before>package com.skcraft.plume.common.util.service;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.skcraft.plume.common.event.lifecycle.PostInitializationEvent;
import com.skcraft.plume.common.util.module.AutoRegister;
import com.skcraft.plume.common.util.module.Modules;
import com.skcraft.plume.common.util.FatalError;

import java.util.Collection;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

@AutoRegister
@Singleton
public class ServiceFactory {

    private final ServiceLocator locator;
    private final Multimap<Class<?>, Class<?>> required = ArrayListMultimap.create();
    private boolean loaded = false;

    @Inject
    public ServiceFactory(ServiceLocator locator) {
        checkNotNull(locator, "locator");
        this.locator = locator;
    }

    @SuppressWarnings("unchecked")
    public <T> Service<T> create(Class<?> service, Class<?> requester) {
        synchronized (this) {
            if (!loaded) {
                required.put(service, requester);
            } else {
                throw new NoProviderExistsException(createMissingProviderMessage(service, Lists.newArrayList(requester)));
            }
        }
        return new Service<>(locator, (Class<T>) service);
    }

    @Subscribe
    public void onPostInitialization(PostInitializationEvent event) {
        synchronized (this) {
            if (!loaded) {
                loaded = true;
                for (Map.Entry<Class<?>, Collection<Class<?>>> entry : required.asMap().entrySet()) {
                    event.getFatalErrors().add(new FatalError(createMissingProviderMessage(entry.getKey(), entry.getValue())));
                }
                required.clear();
            }
        }
    }

    private String createMissingProviderMessage(Class<?> service, Collection<Class<?>> requesters) {
        StringBuilder builder = new StringBuilder();
        builder.append("The service '").append(service.getName()).append("' is required by:");
        for (Class<?> requester : requesters) {
            builder.append("\n\t").append(Modules.getModuleName(requester));
        }
        return builder.toString();
    }

}<commit_msg>Fix service verification.<commit_after>package com.skcraft.plume.common.util.service;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.skcraft.plume.common.event.lifecycle.PostInitializationEvent;
import com.skcraft.plume.common.util.module.AutoRegister;
import com.skcraft.plume.common.util.module.Modules;
import com.skcraft.plume.common.util.FatalError;

import java.util.Collection;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

@AutoRegister
@Singleton
public class ServiceFactory {

    private final ServiceLocator locator;
    private final Multimap<Class<?>, Class<?>> required = ArrayListMultimap.create();
    private boolean loaded = false;

    @Inject
    public ServiceFactory(ServiceLocator locator) {
        checkNotNull(locator, "locator");
        this.locator = locator;
    }

    @SuppressWarnings("unchecked")
    public <T> Service<T> create(Class<?> service, Class<?> requester) {
        synchronized (this) {
            if (!locator.get(service).isPresent()) {
                if (!loaded) {
                    required.put(service, requester);
                } else {
                    throw new NoProviderExistsException(createMissingProviderMessage(service, Lists.newArrayList(requester)));
                }
            }
        }
        return new Service<>(locator, (Class<T>) service);
    }

    @Subscribe
    public void onPostInitialization(PostInitializationEvent event) {
        synchronized (this) {
            if (!loaded) {
                loaded = true;
                for (Map.Entry<Class<?>, Collection<Class<?>>> entry : required.asMap().entrySet()) {
                    if (!locator.get(entry.getKey()).isPresent()) {
                        event.getFatalErrors().add(new FatalError(createMissingProviderMessage(entry.getKey(), entry.getValue())));
                    }
                }
                required.clear();
            }
        }
    }

    private String createMissingProviderMessage(Class<?> service, Collection<Class<?>> requesters) {
        StringBuilder builder = new StringBuilder();
        builder.append("The service '").append(service.getName()).append("' is required by:");
        for (Class<?> requester : requesters) {
            builder.append("\n\t").append(Modules.getModuleName(requester));
        }
        return builder.toString();
    }

}
{'fraction_non_alphanumeric': 0.12123629112662014, 'fraction_numerical': 0.0007976071784646061, 'mean_word_length': 2.9341176470588235, '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': '6616207', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1358, 'n_words': 252}