Skip to content

Commit fcc3e76

Browse files
committed
Enable registration of custom Fix commands at runtime.
The Fix definition is parsed as soon as the Metafix instance is created, thus any custom Fix commands that are going to be used must be registered in the Metafix instance's registry at construction time.
1 parent acdffe9 commit fcc3e76

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

metafix/src/main/java/org/metafacture/metafix/Metafix.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.List;
5454
import java.util.Map;
5555
import java.util.function.BiConsumer;
56+
import java.util.function.Function;
5657

5758
/**
5859
* Transforms a data stream sent via the {@link StreamReceiver} interface. Uses
@@ -119,7 +120,17 @@ public Metafix() {
119120
* @param vars the Fix variables as a Map
120121
*/
121122
public Metafix(final Map<String, String> vars) {
122-
init(vars);
123+
this(m -> vars);
124+
}
125+
126+
/**
127+
* Creates an instance of {@link Metafix}.
128+
*
129+
* @param function an optional function that accepts the Metafix instance
130+
* and returns the Fix variables as a Map, or {@code null}
131+
*/
132+
public Metafix(final Function<Metafix, Map<String, String>> function) {
133+
init(function);
123134
recordTransformer = null;
124135
}
125136

@@ -143,7 +154,20 @@ public Metafix(final String fixDef) throws IOException {
143154
* @throws IOException if an I/O error occurs
144155
*/
145156
public Metafix(final String fixDef, final Map<String, String> vars) throws IOException {
146-
init(vars);
157+
this(fixDef, m -> vars);
158+
}
159+
160+
/**
161+
* Creates an instance of {@link Metafix}.
162+
*
163+
* @param fixDef the Fix definition
164+
* @param function an optional function that accepts the Metafix instance
165+
* and returns the Fix variables as a Map, or {@code null}
166+
*
167+
* @throws IOException if an I/O error occurs
168+
*/
169+
public Metafix(final String fixDef, final Function<Metafix, Map<String, String>> function) throws IOException {
170+
init(function);
147171

148172
if (isFixFile(fixDef)) {
149173
fixFile = fixDef;
@@ -172,11 +196,22 @@ public Metafix(final Reader fixDef) {
172196
* @param vars the Fix variables as a Map
173197
*/
174198
public Metafix(final Reader fixDef, final Map<String, String> vars) {
175-
init(vars);
199+
this(fixDef, m -> vars);
200+
}
201+
202+
/**
203+
* Creates an instance of {@link Metafix}.
204+
*
205+
* @param fixDef the Fix definition
206+
* @param function an optional function that accepts the Metafix instance
207+
* and returns the Fix variables as a Map, or {@code null}
208+
*/
209+
public Metafix(final Reader fixDef, final Function<Metafix, Map<String, String>> function) {
210+
init(function);
176211
recordTransformer = getRecordTransformer(fixDef);
177212
}
178213

179-
private void init(final Map<String, String> newVars) {
214+
private void init(final Function<Metafix, Map<String, String>> function) {
180215
flattener.setReceiver(new DefaultStreamReceiver() {
181216

182217
@Override
@@ -189,7 +224,12 @@ public void literal(final String name, final String value) {
189224

190225
});
191226

192-
vars.putAll(newVars);
227+
if (function != null) {
228+
final Map<String, String> newVars = function.apply(this);
229+
if (newVars != null) {
230+
vars.putAll(newVars);
231+
}
232+
}
193233
}
194234

195235
/**

metafix/src/test/java/org/metafacture/metafix/MetafixTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ public void shouldNotRegisterCommandWithoutClass() {
315315
Assertions.assertThrows(NullPointerException.class, () -> registry.registerCommand(name, clazz));
316316
}
317317

318+
@Test
319+
public void shouldRegisterCommandInConstructor() {
320+
final String name = "bla";
321+
final Class<?> clazz = Once.class;
322+
323+
final Metafix metafix = new Metafix(m -> {
324+
final FixRegistry registry = m.getRegistry();
325+
326+
Assertions.assertFalse(registry.isRegisteredCommand(name));
327+
Assertions.assertTrue(registry.registerCommand(name, clazz));
328+
329+
return null;
330+
});
331+
332+
final FixRegistry registry = metafix.getRegistry();
333+
Assertions.assertTrue(registry.isRegisteredCommand(name));
334+
}
335+
318336
@FixCommand("test_command")
319337
private static class TestCommand1 implements FixFunction {
320338

0 commit comments

Comments
 (0)