toothpick.registries.factory.AbstractFactoryRegistry Maven / Gradle / Ivy
package toothpick.registries.factory;
import java.util.ArrayList;
import java.util.List;
import toothpick.registries.FactoryRegistry;
import toothpick.Factory;
/**
* The base class of the {@link FactoryRegistry} classes generated by the toothpick annotation processor.
* Those {@link FactoryRegistry} can form a tree of {@link FactoryRegistry} and have the ability to run through the tree
* of their children {@link FactoryRegistry} to find a {@link Factory}.
*
* This class is meant to be used internally by toothpick.
*/
public abstract class AbstractFactoryRegistry implements FactoryRegistry {
private List childrenRegistries = new ArrayList<>();
public void addChildRegistry(FactoryRegistry childRegistry) {
this.childrenRegistries.add(childRegistry);
}
/**
* Explore the subtree of children registries via DFS to retrieve a {@link Factory} for a given class.
* This method will be called automatically by all factories generated by toothpick, if it is provided
* appropriate parameters when compiling each library.
*
* @param clazz the class to look the {@link Factory} for.
* @param the type of {@code clazz}.
* @return an instance of the {@link Factory} class asssociated with {@code clazz} or null if it can't find one.
*/
protected Factory getFactoryInChildrenRegistries(Class clazz) {
Factory factory;
for (FactoryRegistry registry : childrenRegistries) {
factory = registry.getFactory(clazz);
if (factory != null) {
return factory;
}
}
return null;
}
}