Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field ordering functionality. #235

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions quickfixj-core/src/main/java/quickfix/DataDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class DataDictionary {
private final IntegerStringMap<String> valueNames = new IntegerStringMap<>();
private final StringIntegerMap<GroupInfo> groups = new StringIntegerMap<>();
private final Map<String, Node> components = new HashMap<>();
private int[] orderedFieldsArray;


private DataDictionary() {
}
Expand Down Expand Up @@ -243,7 +245,7 @@ public boolean isAppMessage(String msgType) {
}

private void addMsgField(String msgType, int field) {
messageFields.computeIfAbsent(msgType, k -> new HashSet<>()).add(field);
messageFields.computeIfAbsent(msgType, k -> new LinkedHashSet<>()).add(field);
}

/**
Expand Down Expand Up @@ -294,7 +296,7 @@ public int getFieldTag(String name) {
}

private void addRequiredField(String msgType, int field) {
requiredFields.computeIfAbsent(msgType, k -> new HashSet<>()).add(field);
requiredFields.computeIfAbsent(msgType, k -> new LinkedHashSet<>()).add(field);
}

/**
Expand Down Expand Up @@ -330,7 +332,7 @@ public boolean isRequiredTrailerField(int field) {
}

private void addFieldValue(int field, String value) {
fieldValues.computeIfAbsent(field, k -> new HashSet<>()).add(value);
fieldValues.computeIfAbsent(field, k -> new LinkedHashSet<>()).add(value);
}

/**
Expand Down Expand Up @@ -1069,8 +1071,6 @@ private void load(Document document, String msgtype, Node node) throws ConfigErr
}
}

private int[] orderedFieldsArray;

public int[] getOrderedFields() {
if (orderedFieldsArray == null) {
orderedFieldsArray = new int[fields.size()];
Expand All @@ -1083,6 +1083,68 @@ public int[] getOrderedFields() {
return orderedFieldsArray;
}

/**
* Concatenates the Integer Sets into a single int[], with the header followed
* by the fields and finally the trailer.
* @param fieldsCollection
* @param headerCollection
* @param trailerCollection
* @return
*/
private int[] getOrderedFieldsFrom(Set<Integer> fieldsCollection,
Set<Integer> headerCollection, Set<Integer> trailerCollection) {

if (fieldsCollection == null) {
fieldsCollection = new LinkedHashSet<Integer>();
}
if (headerCollection == null) {
headerCollection = new LinkedHashSet<Integer>();
}
if (trailerCollection == null) {
trailerCollection = new LinkedHashSet<Integer>();
}

int[] fields = new int[fieldsCollection.size() + headerCollection.size() + trailerCollection.size()];
Integer[] headerArray = headerCollection.toArray(new Integer[headerCollection.size()]);
Integer[] fieldsArray = fieldsCollection.toArray(new Integer[fieldsCollection.size()]);
Integer[] trailerArray = trailerCollection.toArray(new Integer[trailerCollection.size()]);

int overallIndex = 0;

for (; overallIndex < headerArray.length; overallIndex++)
fields[overallIndex] = headerArray[overallIndex].intValue();

for (int i = 0; i < fieldsArray.length; i++, overallIndex++)
fields[overallIndex] = fieldsArray[i].intValue();

for (int i = 0; i < trailerArray.length; i++, overallIndex++)
fields[overallIndex] = trailerArray[i].intValue();

return fields;
}

/**
* Returns the required ordered fields for a message type, including the Header and Trailer.
* @param messageType
* @return
*/
public int[] getOrderedRequiredFieldsForMessage(String messageType){
return getOrderedFieldsFrom(
requiredFields.get(messageType), requiredFields.get(HEADER_ID), requiredFields.get(TRAILER_ID));
}

/**
* Returns the ordered fields for a message type, including the Header and Trailer.
* @param messageType
* @return
*/
public int[] getOrderedFieldsForMessage(String messageType){
return getOrderedFieldsFrom(
messageFields.get(messageType), messageFields.get(HEADER_ID), messageFields.get(TRAILER_ID));
}



private int lookupXMLFieldNumber(Document document, Node node) throws ConfigError {
final Element element = (Element) node;
if (!element.hasAttribute("name")) {
Expand Down
9 changes: 6 additions & 3 deletions quickfixj-core/src/main/java/quickfix/FieldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public abstract class FieldMap implements Serializable {

static final long serialVersionUID = -3193357271891865972L;

private final int[] fieldOrder;
private int[] fieldOrder;

private final TreeMap<Integer, Field<?>> fields;
private final Map<Integer, Field<?>> fields;

private final TreeMap<Integer, List<Group>> groups = new TreeMap<>();

Expand All @@ -62,7 +62,9 @@ public abstract class FieldMap implements Serializable {
*/
protected FieldMap(int[] fieldOrder) {
this.fieldOrder = fieldOrder;
fields = new TreeMap<>(fieldOrder != null ? new FieldOrderComparator() : null);
fields = (fieldOrder != null) ?
new TreeMap<>(new FieldOrderComparator()) :
new LinkedHashMap<>();
}

protected FieldMap() {
Expand Down Expand Up @@ -440,6 +442,7 @@ public Iterator<Field<?>> iterator() {
}

protected void initializeFrom(FieldMap source) {
fieldOrder = source.getFieldOrder();
fields.clear();
fields.putAll(source.fields);
for (Entry<Integer, List<Group>> entry : source.groups.entrySet()) {
Expand Down
2 changes: 1 addition & 1 deletion quickfixj-core/src/main/java/quickfix/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Message() {
initializeHeader();
}

protected Message(int[] fieldOrder) {
public Message(int[] fieldOrder) {
super(fieldOrder);
initializeHeader();
}
Expand Down
24 changes: 20 additions & 4 deletions quickfixj-core/src/test/java/quickfix/DataDictionaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasProperty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class DataDictionaryTest {

Expand Down Expand Up @@ -105,6 +103,24 @@ public void testDictionary() throws Exception {
assertFalse(dd.isMsgField("UNKNOWN_TYPE", 1));
}

@Test
public void testGetOrderedRequiredFieldsForMessage() throws Exception {
DataDictionary dictionary = getDictionary();
assertArrayEquals("incorrect field ordering", new int[]{8, 9, 35, 49, 56, 34, 52, 98, 108, 10},
dictionary.getOrderedRequiredFieldsForMessage("A"));
}

@Test
public void testGetOrderedFieldsForMessage() throws Exception {
DataDictionary dictionary = getDictionary();
assertArrayEquals("incorrect field ordering",
new int[]{8, 9, 35, 49, 56, 115, 128, 90, 91, 34, 50, 142, 57, 143, 116, 144, 129, 145,
43, 97, 52, 122, 212, 213, 347, 369, 627, 98, 108, 95, 96, 141, 789, 383, 384,
464, 553, 554, 93, 89, 10},
dictionary.getOrderedFieldsForMessage("A"));
}


@Test
public void testMissingFieldAttributeForRequired() throws Exception {
String data = "";
Expand Down
9 changes: 8 additions & 1 deletion quickfixj-core/src/test/java/quickfix/FieldMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public static Test suite() {
return new TestSuite(FieldMapTest.class);
}

public void testDefaultFieldOrderIsInsertionOrdering() {
FieldMap map = new Message();
map.setString(2, "A");
map.setString(1, "B");
assertEquals("9=82=A1=B10=017",map.toString());
}

public void testSetUtcTimeStampField() throws Exception {
FieldMap map = new Message();
LocalDateTime aDate = LocalDateTime.now();
Expand Down Expand Up @@ -82,7 +89,7 @@ private void testOrdering(int[] vals, int[] order, int[] expected) {

public void testOrdering() {
testOrdering(new int[] { 1, 2, 3 }, null, new int[] { 1, 2, 3 });
testOrdering(new int[] { 3, 2, 1 }, null, new int[] { 1, 2, 3 });
testOrdering(new int[] { 3, 2, 1 }, null, new int[] { 3, 2, 1 });
testOrdering(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });
testOrdering(new int[] { 3, 2, 1 }, new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });
testOrdering(new int[] { 1, 2, 3 }, new int[] { 1, 3, 2 }, new int[] { 1, 3, 2 });
Expand Down