5353import java .util .List ;
5454import java .util .Map ;
5555import 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 /**
0 commit comments