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

io.datakernel.di.module.Module Maven / Gradle / Ivy

package io.datakernel.di.module;

import io.datakernel.di.core.*;
import io.datakernel.di.impl.Preprocessor;
import io.datakernel.di.util.Trie;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import static io.datakernel.di.core.BindingGenerator.combinedGenerator;
import static io.datakernel.di.core.BindingTransformer.combinedTransformer;
import static io.datakernel.di.core.Multibinder.combinedMultibinder;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toSet;

/**
 * A module is an object, that provides certain sets of bindings, transformers, generators or multibinders
 * arranged by keys in certain data structures.
 *
 * @see AbstractModule
 */
public interface Module {
	Trie, Set>>> getBindings();

	Map>> getBindingTransformers();

	Map, Set>> getBindingGenerators();

	Map, Multibinder> getMultibinders();

	default Module combineWith(Module another) {
		return Modules.combine(this, another);
	}

	default Module overrideWith(Module another) {
		return Modules.override(this, another);
	}

	default Module transformWith(UnaryOperator fn) {
		return fn.apply(this);
	}

	default Module export(Key key, Key... keys) {
		return export(Stream.concat(Stream.of(key), Arrays.stream(keys)).collect(toSet()));
	}

	default Module export(Set> keys) {
		return Modules.export(this, keys);
	}

	default  Module rebindExport(Class from, Key to) {
		return rebindExport(Key.of(from), to);
	}

	default  Module rebindExport(Class from, Class to) {
		return rebindExport(Key.of(from), Key.of(to));
	}

	default  Module rebindExport(Key from, Class to) {
		return rebindExport(from, Key.of(to));
	}

	default  Module rebindExport(Key from, Key to) {
		return rebindExports(singletonMap(from, to));
	}

	default  Module rebindImport(Key from, Key to) {
		return rebindImportKeys(singletonMap(from, to));
	}

	default  Module rebindImport(Class from, Binding binding) {
		return rebindImport(Key.of(from), binding);
	}

	default  Module rebindImport(Key from, Binding binding) {
		return rebindImports(singletonMap(from, binding));
	}

	default Module rebindExports(@NotNull Map, Key> map) {
		return Modules.rebindExports(this, map);
	}

	default Module rebindImports(@NotNull Map, Binding> map) {
		return Modules.rebindImports(this, map);
	}

	default Module rebindImports(BiFunction, Binding, Binding> rebinder) {
		return Modules.rebindImports(this, rebinder);
	}

	default Module rebindImportKeys(@NotNull Map, Key> mapping) {
		return Modules.rebindImportKeys(this, mapping);
	}

	default  Module rebindImportDependencies(Key key, Key dependency, Key to) {
		return rebindImports((k, binding) -> k.equals(key) ? binding.rebindDependency(dependency, to) : binding);
	}

	default  Module rebindImportDependencies(Key key, @NotNull Map, Key> dependencyMapping) {
		return rebindImports((k, binding) -> key.equals(k) ? binding.rebindDependencies(dependencyMapping) : binding);
	}

	/**
	 * A shortcut that reduces bindings multimap trie from this module using multibinders, transformers and generators from this module
	 */
	default Trie, Binding>> getReducedBindings() {
		return Preprocessor.reduce(getBindings(),
				combinedMultibinder(getMultibinders()),
				combinedTransformer(getBindingTransformers()),
				combinedGenerator(getBindingGenerators()));
	}

	/**
	 * Returns an empty {@link Module module}.
	 */
	static Module empty() {
		return Modules.EMPTY;
	}

	static ModuleBuilder create() {
		return new ModuleBuilderImpl<>();
	}

	/**
	 * Creates a {@link Module module} out of given binding graph trie
	 */
	static Module of(Trie, Set>>> bindings) {
		return new SimpleModule(bindings, emptyMap(), emptyMap(), emptyMap());
	}

	/**
	 * Creates a {@link Module module} out of given binding graph trie, transformers, generators and multibinders
	 */
	static Module of(Trie, Set>>> bindings,
			Map>> transformers,
			Map, Set>> generators,
			Map, Multibinder> multibinders) {
		return new SimpleModule(bindings, transformers, generators, multibinders);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy