<commit_before>package io.skullabs.undertow.hazelcast;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import io.skullabs.undertow.hazelcast.HazelcastConfiguration.ClusterClientConfig;
import trip.spi.*;

@Service
public class HazelcastInstanceProducer {

	@Provided
	HazelcastConfiguration hazelcastConfig;
	HazelcastInstance instance;

	@Producer
	public HazelcastInstance produceHazelcastInstance() {
		if ( instance == null )
			instance = createHazelcastInstance();
		return instance;
	}

	HazelcastInstance createHazelcastInstance() {
		if ( hazelcastConfig.mode().equals( HazelcastConfiguration.MODE_CLIENT ) ) {
			final ClientConfig clientConfig = createClientConfiguration();
			return HazelcastClient.newHazelcastClient( clientConfig );
		}
		final Config cfg = new Config();
		return Hazelcast.newHazelcastInstance( cfg );
	}

	ClientConfig createClientConfiguration() {
		final ClientConfig clientConfig = new ClientConfig();
		final ClusterClientConfig clusterClient = hazelcastConfig.clusterClient();
		if ( !isBlank( clusterClient.username() ) )
			clientConfig.getGroupConfig()
					.setName( clusterClient.username() )
					.setPassword( clusterClient.password() );
		for ( String address : clusterClient.addresses() )
			clientConfig.getNetworkConfig().addAddress( address );
		return clientConfig;
	}

	boolean isBlank( String string ) {
		return string == null || string.isEmpty();
	}
}<commit_msg>Added some javadocs.<commit_after>package io.skullabs.undertow.hazelcast;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import io.skullabs.undertow.hazelcast.HazelcastConfiguration.ClusterClientConfig;
import lombok.extern.java.Log;
import trip.spi.*;

@Log
@Service
public class HazelcastInstanceProducer {

	@Provided
	ServiceProvider provider;

	@Provided
	HazelcastConfiguration hazelcastConfig;
	HazelcastInstance instance;

	/**
	 * Produce a Hazelcast Instance. It will ensure that has only one instance
	 * of Hazelcast. You are always able to "inject"
	 * {@code HazelcastInstanceProducer} and call
	 * {@code createHazelcastInstance()} method manually to produce more
	 * Hazelcast instances.
	 * 
	 * @return a HazelcastInstance
	 * @see HazelcastInstanceProducer#createHazelcastInstance()
	 */
	@Producer
	public HazelcastInstance produceHazelcastInstance() {
		if ( instance == null )
			instance = createHazelcastInstance();
		return instance;
	}

	/**
	 * Creates a Hazelcast Instance. If the hazelcast mode, defined in
	 * {@code application.conf}, is set to "client", it will creates an instance
	 * that connects to a cluster node, otherwise, it will create an instance
	 * that behaves like a cluster node.<br>
	 * <br>
	 * 
	 * <b>Note:</b> you are always able to provide your own {@link Config} or
	 * {@link ClientConfig} implementation through tRip producer.
	 * 
	 * @return a HazelcastInstance
	 */
	public HazelcastInstance createHazelcastInstance() {
		if ( hazelcastConfig.mode().equals( HazelcastConfiguration.MODE_CLIENT ) ) {
			final ClientConfig clientConfig = loadClientConfig();
			return HazelcastClient.newHazelcastClient( clientConfig );
		}
		final Config cfg = loadConfig();
		return Hazelcast.newHazelcastInstance( cfg );
	}

	ClientConfig loadClientConfig() {
		try {
			ClientConfig clientConfig = provider.load( ClientConfig.class );
			if ( clientConfig == null )
				clientConfig = createClientConfiguration();
			return clientConfig;
		} catch ( ServiceProviderException cause ) {
			log.warning( "Could not read Hazelcast Client Configuration: " + cause.getMessage() + ". Creating one manually." );
			return createClientConfiguration();
		}
	}

	ClientConfig createClientConfiguration() {
		final ClientConfig clientConfig = new ClientConfig();
		final ClusterClientConfig clusterClient = hazelcastConfig.clusterClient();
		if ( !isBlank( clusterClient.username() ) )
			clientConfig.getGroupConfig()
					.setName( clusterClient.username() )
					.setPassword( clusterClient.password() );
		for ( String address : clusterClient.addresses() )
			clientConfig.getNetworkConfig().addAddress( address );
		return clientConfig;
	}

	boolean isBlank( String string ) {
		return string == null || string.isEmpty();
	}

	Config loadConfig() {
		try {
			Config config = provider.load( Config.class );
			if ( config == null )
				config = createConfig();
			return config;
		} catch ( ServiceProviderException cause ) {
			log.warning( "Could not read Hazelcast Configuration: " + cause.getMessage() + ". Creating one manually." );
			return createConfig();
		}
	}

	private Config createConfig() {
		return new Config();
	}
}
{'fraction_non_alphanumeric': 0.09546351084812624, 'fraction_numerical': 0.0, 'mean_word_length': 4.086258776328987, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6748057', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1447, 'n_words': 354}