All Downloads are FREE. Search and download functionalities are using the official Maven repository.

online.sanen.unabo.nosql.Container Maven / Gradle / Ivy

The newest version!
package online.sanen.unabo.nosql;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

import com.mhdt.toolkit.Assert;

import online.sanen.unabo.api.exception.StructuralException;
import online.sanen.unabo.nosql.mongodb.BootstrapMongodb;

public class Container {

	static final Map exists = new ConcurrentHashMap<>();

	public Bootstrap load(Consumer configuration) {
		return load(null, configuration);
	}

	public Bootstrap load(Object id, Consumer configuration) {

		if (null!=id  && exists.containsKey(id)) {
			return exists.get(id);
		} else {
			ConfigurationNosql configurationNosql = new SimpleConfigurationNosql();
			configuration.accept(configurationNosql);


			Bootstrap bootstrap = new BootstrapMongodb(id,configurationNosql);
			Assert.notNull(bootstrap, "Bootstrap is null");

			
			if (id != null)
				exists.put(id, bootstrap);
			
			return bootstrap;
		}
	}

	public Bootstrap getFirst() {

		try {

			return exists.get(exists.keySet().stream().findFirst().get());
		} catch (NullPointerException e) {
			throw new StructuralException(
					"The default Bootstrap instance cannot be obtained from the Bootstrap factory,Reason may be that instance to not Bootstraps trust or no BootstrapId specified");
		}

	}

	public boolean isUniqueness() {
		return exists.size() == 1;
	}

	public boolean contains(Object key) {
		return exists.containsKey(key);
	}

	public Bootstrap get(Object key) {
		return exists.get(key);
	}

	public Bootstrap put(Object bootStrapId, Bootstrap bootstrap) {
		exists.put(bootStrapId, bootstrap);
		return bootstrap;
	}

	public void unload(String bootstrapId) {
		exists.remove(bootstrapId);
	}

	public static Set keys() {
		return exists.keySet();
	}

}