-
Notifications
You must be signed in to change notification settings - Fork 8
/
NodeFactory.java
119 lines (108 loc) · 4.19 KB
/
NodeFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (C) 2018 2024 gregory higgins.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package com.fluxtion.compiler.builder.factory;
import com.fluxtion.compiler.generation.GenerationContext;
import com.fluxtion.runtime.annotations.builder.Config;
import com.fluxtion.runtime.annotations.builder.Inject;
import com.fluxtion.runtime.audit.Auditor;
import java.util.Map;
import java.util.ServiceLoader;
/**
* A factory for creating instances. The SEP will call this method to create
* nodes.<p>
*
* <h2>Instance re-use</h2>
* To ensure that node instances are re-used there are two approaches:
* <ul>
* <li>The factory caches the node, and returns the same instance for the same
* configuration.
* <li>The factory creates new nodes and the node overrides hashcode and equals.
* The {@link NodeRegistry} will consult a map for an equivalent node. If an
* equivalent node is found then the newly created node will be discarded and
* the existing instance used.
* </ul>
* <p>
* <h2>Registering factories</h2>
* Fluxtion employs the {@link ServiceLoader} pattern to register user
* implemented NodeFactories. Please read the java documentation describing the
* meta-data a factory implementor must provide to register a factory using the
* {@link ServiceLoader} pattern.
*
* @param <T>
* @author Greg Higgins
*/
public interface NodeFactory<T> {
/**
* The key in the map passed to the NodeFactory instance that holds the {@link java.lang.reflect.Field} type
* information for the target field.
*/
String FIELD_KEY = "NodeFactory.InjectField.TypeInfo";
/**
* The key in the map passed to the NodeFactory instance that holds the name of the instance to be injected when
* using information for the target field.
*/
String INSTANCE_KEY = "NodeFactory.InjectField.InstanceName";
/**
* NodeFactory writer must implement this method to generate instances of
* nodes. The Fluxtion compiler will call this method when an {@link Inject}
* instance is created. {@link Config} variables are used to populate the
* config map.
*
* @param config map configuration
* @param registry The node registry of the current generation contextß
* @return The newly created node instance
*/
T createNode(Map<String, Object> config, NodeRegistry registry);
/**
* Callback invoked by Fluxtion generator after the generated SEP has been
* registered in the{@link GenerationContext}
*
* @param config map configuration
* @param registry The node registry of the current generation context
* @param instance the newly created instance
*/
default void postInstanceRegistration(Map<String, Object> config, NodeRegistry registry, T instance) {
}
/**
* If the node generates a class for this SEP, this callback will indicate
* the desired target.
*
* @param targetLanguage target language for generation
*/
default void setTargetLanguage(String targetLanguage) {
}
/**
* If the node generates a class for this SEP, this callback gives the node
* access to the GenerationContext before generation.
*
* @param context The context the Fluxtion SEC compiler uses
* @param auditorMap auditors map for client to populate, these will be added to the generated EventProcessor
*/
default void preSepGeneration(GenerationContext context, Map<String, Auditor> auditorMap) {
}
default String factoryName() {
return "";
}
/**
* Override the injection type inferred by the type variable
*
* @return
*/
default Class<T> injectionType() {
return null;
}
}