Skip to content

Commit a31be10

Browse files
authored
Add method to DataDictionary to get a value for a given enum value name (#280)
- this is useful for mapping from/to the XML representation obtained from Message.toXML() - since the DataDictionary is kind of a memory hog we do not store the values in yet another HashMap but iterate over the entries on each call to getValue(int, String) - it is up to the user's application to cache the return values if desired
1 parent 5a663bf commit a31be10

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

quickfixj-core/src/main/java/quickfix/DataDictionary.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ public String getValueName(int field, String value) {
172172
return valueNames.get(field, value);
173173
}
174174

175+
/**
176+
* Get the value, if any, for an enumerated value name.
177+
*
178+
* @param field the tag
179+
* @param name the value name
180+
* @return the value assigned to passed name
181+
*/
182+
public String getValue(int field, String name) {
183+
return valueNames.getValue(field, name);
184+
}
185+
175186
/**
176187
* Predicate for determining if a tag is a defined field.
177188
*
@@ -1255,6 +1266,18 @@ public V get(int field, String group) {
12551266
return map == null ? null : map.get(group);
12561267
}
12571268

1269+
public String getValue(int field, String name) {
1270+
Map<String, V> map = get(field);
1271+
if (map != null) {
1272+
for (Entry<String, V> entry : map.entrySet()) {
1273+
if (entry.getValue().equals(name)) {
1274+
return entry.getKey();
1275+
}
1276+
}
1277+
}
1278+
return null;
1279+
}
1280+
12581281
public void put(int field, String group, V value) {
12591282
computeIfAbsent(field, __ -> new HashMap<>())
12601283
.put(group, value);

quickfixj-core/src/test/java/quickfix/DataDictionaryTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void testDictionary() throws Exception {
8888

8989
assertEquals("wrong field name", "Currency", dd.getFieldName(15));
9090
assertEquals("wrong value description", "BUY", dd.getValueName(4, "B"));
91+
assertEquals("wrong value for given value name", "2", dd.getValue(54, "SELL"));
9192
assertEquals("wrong value type", FieldType.STRING, dd.getFieldType(1));
9293
assertEquals("wrong version", FixVersions.BEGINSTRING_FIX44, dd.getVersion());
9394
assertFalse("unexpected field values existence", dd.hasFieldValue(1));

0 commit comments

Comments
 (0)