<commit_before>package kikaha.urouting.api;

import java.util.HashMap;
import java.util.Map;

import kikaha.urouting.Reflection;
import lombok.val;
import trip.spi.Provided;
import trip.spi.ServiceProvider;
import trip.spi.ServiceProviderException;
import trip.spi.Singleton;

@Singleton
@SuppressWarnings( { "rawtypes", "unchecked" } )
public class ContextProducerFactory {

	@Provided
	ServiceProvider provider;
	Map<Class, ContextProducer> producers;

	public <T> ContextProducer<T> producerFor( Class<T> clazz ) throws RoutingException {
		return getProducers().get( clazz );
	}

	public Map<Class, ContextProducer> getProducers() throws RoutingException {
		if ( this.producers == null ) {
			producers = new HashMap<Class, ContextProducer>();
			loadProducers();
		}
		return this.producers;
	}

	private void loadProducers() throws RoutingException {
		try {
			val producers = provider.loadAll( ContextProducer.class );
			for ( ContextProducer producer : producers )
				register( producer );
		} catch ( ServiceProviderException cause ) {
			throw new RoutingException( cause );
		}
	}

	public <T> void register( ContextProducer<T> producer ) {
		val forClazz = Reflection.getFirstGenericTypeFrom( producer, ContextProducer.class );
		producers.put( forClazz, producer );
	}
}<commit_msg>ContextProducerFactory are thread safe now.<commit_after>package kikaha.urouting.api;

import java.util.HashMap;
import java.util.Map;

import kikaha.urouting.Reflection;
import lombok.val;
import trip.spi.Provided;
import trip.spi.ServiceProvider;
import trip.spi.ServiceProviderException;
import trip.spi.Singleton;

@Singleton
@SuppressWarnings( { "rawtypes", "unchecked" } )
public class ContextProducerFactory {

	@Provided
	ServiceProvider provider;
	Map<Class, ContextProducer> producers;

	public <T> ContextProducer<T> producerFor( Class<T> clazz ) throws RoutingException {
		return getProducers().get( clazz );
	}

	public Map<Class, ContextProducer> getProducers() throws RoutingException {
		if ( this.producers == null ) {
			synchronized ( this.producers ) {
				if ( this.producers == null ) {
					producers = new HashMap<Class, ContextProducer>();
					loadProducers();
				}
			}
		}
		return this.producers;
	}

	private void loadProducers() throws RoutingException {
		try {
			val producers = provider.loadAll( ContextProducer.class );
			for ( ContextProducer producer : producers )
				register( producer );
		} catch ( ServiceProviderException cause ) {
			throw new RoutingException( cause );
		}
	}

	public <T> void register( ContextProducer<T> producer ) {
		val forClazz = Reflection.getFirstGenericTypeFrom( producer, ContextProducer.class );
		producers.put( forClazz, producer );
	}
}
{'fraction_non_alphanumeric': 0.10419734904270987, 'fraction_numerical': 0.0, 'mean_word_length': 4.830472103004292, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1891646', 'n_tokens_mistral': 902, 'n_tokens_neox': 820, 'n_words': 199}