Skip to content

Commit d3c7c1c

Browse files
authored
Add methods to FieldMap API to make it usable in for-each loop (#347)
* Add methods to FieldMap API to make it usable in for-each loop FieldMap already provides iterator() to inspect all fields, however does not implement Iterable - and so can't be used in for-each loop. This PR adds Iterable interface, as well as groupKeys() which exposes group keys as Iterable. The motivation for the change is that in some cases it is useful to traverse message as a tree of key-value pairs - e.g. when visualising message structure in UI, or transforming it to another intermediate representation. Being able to iterate in a more idiomatic Collection-like way improves readability of such code. * Deprecate groupKeyIterator() in favour of groupKeys(). Add javadoc.
1 parent c141074 commit d3c7c1c

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.Collection;
4444
import java.util.HashMap;
4545
import java.util.HashSet;
46-
import java.util.Iterator;
4746
import java.util.LinkedHashSet;
4847
import java.util.List;
4948
import java.util.Map;
@@ -751,9 +750,8 @@ private static boolean isVersionSpecified(DataDictionary dd) {
751750

752751
private void iterate(FieldMap map, String msgType, DataDictionary dd) throws IncorrectTagValue,
753752
IncorrectDataFormat {
754-
final Iterator<Field<?>> iterator = map.iterator();
755-
while (iterator.hasNext()) {
756-
final StringField field = (StringField) iterator.next();
753+
for (final Field<?> f : map) {
754+
final StringField field = (StringField) f;
757755

758756
checkHasValue(field);
759757

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
/**
4646
* Field container used by messages, groups, and composites.
4747
*/
48-
public abstract class FieldMap implements Serializable {
48+
public abstract class FieldMap implements Serializable, Iterable<Field<?>> {
4949

5050
static final long serialVersionUID = -3193357271891865972L;
5151

@@ -448,6 +448,7 @@ public void removeField(int field) {
448448
fields.remove(field);
449449
}
450450

451+
@Override
451452
public Iterator<Field<?>> iterator() {
452453
return fields.values().iterator();
453454
}
@@ -601,8 +602,21 @@ public int getGroupCount(int tag) {
601602
return getGroups(tag).size();
602603
}
603604

605+
/**
606+
* @deprecated use {@linkplain #groupKeys()} instead
607+
*/
608+
@Deprecated
604609
public Iterator<Integer> groupKeyIterator() {
605-
return groups.keySet().iterator();
610+
return groupKeys().iterator();
611+
}
612+
613+
/**
614+
* Returns tags which are repeating group counters as {@code Iterable}
615+
*
616+
* @return tags which are repeating group counters
617+
*/
618+
public Iterable<Integer> groupKeys() {
619+
return groups.keySet();
606620
}
607621

608622
Map<Integer, List<Group>> getGroups() {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
import javax.xml.transform.stream.StreamResult;
6767
import java.io.ByteArrayOutputStream;
6868
import java.text.DecimalFormat;
69-
import java.util.Iterator;
7069
import java.util.List;
7170

7271
/**
@@ -392,9 +391,7 @@ private void toXMLFields(Element message, String section, FieldMap fieldMap,
392391
final Document document = message.getOwnerDocument();
393392
final Element fields = document.createElement(section);
394393
message.appendChild(fields);
395-
final Iterator<Field<?>> fieldItr = fieldMap.iterator();
396-
while (fieldItr.hasNext()) {
397-
final Field<?> field = fieldItr.next();
394+
for (final Field<?> field : fieldMap) {
398395
final Element fieldElement = document.createElement("field");
399396
if (dataDictionary != null) {
400397
final String name = dataDictionary.getFieldName(field.getTag());
@@ -412,9 +409,7 @@ private void toXMLFields(Element message, String section, FieldMap fieldMap,
412409
fieldElement.appendChild(value);
413410
fields.appendChild(fieldElement);
414411
}
415-
final Iterator<Integer> groupKeyItr = fieldMap.groupKeyIterator();
416-
while (groupKeyItr.hasNext()) {
417-
final int groupKey = groupKeyItr.next();
412+
for (final int groupKey : fieldMap.groupKeys()) {
418413
final Element groupsElement = document.createElement("groups");
419414
fields.appendChild(groupsElement);
420415
if (dataDictionary != null) {

0 commit comments

Comments
 (0)