generated from Steanky/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Steank edited this page Jan 16, 2023
·
8 revisions
!!This page is a WIP!!
At the core of this library is the concept of an element class. An element class is a regular Java class that is annotated with the @Model
annotation:
@Model("model_class")
public class ModelClass {
@FactoryMethod
public ModelClass() {
}
}
In order to construct instances of these classes, it is first necessary to:
- Make sure the element class is registered with a
ContextManager
, so Element knows about it. - Load an
ElementContext
object from some configuration data using theContextManager
. - Create the element object instance from the
ElementContext
after providing any necessary dependencies.
Here's an example of the required setup for a ContextManager
, along with explanatory comments. Most applications will only need to do this setup once.
Don't worry if you don't understand all of the terminology; it will be explained in detail later.
//KeyParsers are used to convert regular strings into namespaced identifiers (Keys) for element classes and more
//here, "example" is the default namespace, which will be used if no namespace is explicitly provided
KeyParser keyParser = new BasicKeyParser("example");
//KeyExtractor, given some configuration data, are used to determine what element class should be loaded
//BasicKeyExtractor looks for a field named "type" and extracts a key from it, using its given KeyParser
KeyExtractor typeExtractor = new BasicKeyExtractor("type", keyParser);
//given a Class object, an ElementTypeIdentifier produces a type key
//the basic implementation just looks at the value of the @Model annotation, if present
//this is like a KeyExtractor, but for element classes instead of configuration data
ElementTypeIdentifier elementTypeIdentifier = new BasicElementTypeIdentifier(keyParser);
//when necessary, is used to pre-process data classes for information that will be used to automatically construct
//dependencies that are themselves elements
//the basic implementation looks for accessor methods annotated with @DataPath
DataInspector dataInspector = new BasicDataInspector(keyParser);
//used to instantiate arrays, or subclasses of Collection, when there are dependencies present which are also element objects
ContainerCreator collectionCreator = new BasicContainerCreator();
//FactoryResolver produces ElementFactory instances, given a Class object for an element type
//ElementFactory is used internally to construct instances of element objects
FactoryResolver factoryResolver = new BasicFactoryResolver(keyParser, dataInspector, collectionCreator,
MappingProcessorSource.builder().ignoringLengths().build());
//extracts ConfigProcessor instances from element Class objects, which are used internally to process configuration data into data objects
ProcessorResolver processorResolver = BasicProcessorResolver.INSTANCE;
//a combination of the features of FactoryResolver and ProcessorResolver
ElementInspector elementInspector = new BasicElementInspector(factoryResolver, processorResolver);
//holds named ConfigProcessor instances
Registry<ConfigProcessor<?>> configRegistry = new HashRegistry<>();
//holds named booleans, which store cache information
Registry<Boolean> cacheRegistry = new HashRegistry<>();
//holds named ElementFactory instances
Registry<ElementFactory<?, ?>> factoryRegistry = new HashRegistry<>();
//responsible for actually constructing ElementContext objects
ElementContext.Source source = new BasicElementContext.Source(configRegistry, factoryRegistry,cacheRegistry, typeExtractor);
//the main entrypoint of Element, to which element classes can be registered and context objects created
ContextManager contextManager = new BasicContextManager(elementInspector, elementTypeIdentifier, source);