diff --git a/quickfixj-core/src/main/java/quickfix/DataDictionary.java b/quickfixj-core/src/main/java/quickfix/DataDictionary.java index 49cdefc2ab..a8732dec06 100644 --- a/quickfixj-core/src/main/java/quickfix/DataDictionary.java +++ b/quickfixj-core/src/main/java/quickfix/DataDictionary.java @@ -84,8 +84,8 @@ public class DataDictionary { private final Map> fieldValues = new HashMap<>(); private final Map fieldNames = new HashMap<>(); private final Map names = new HashMap<>(); - private final Map valueNames = new HashMap<>(); - private final Map groups = new HashMap<>(); + private final IntegerStringMap valueNames = new IntegerStringMap<>(); + private final StringIntegerMap groups = new StringIntegerMap<>(); private final Map components = new HashMap<>(); private DataDictionary() { @@ -156,7 +156,7 @@ public String getFieldName(int field) { } private void addValueName(int field, String value, String name) { - valueNames.put(new IntStringPair(field, value), name); + valueNames.put(field, value, name); } /** @@ -167,7 +167,7 @@ private void addValueName(int field, String value, String name) { * @return the value's name */ public String getValueName(int field, String value) { - return valueNames.get(new IntStringPair(field, value)); + return valueNames.get(field, value); } /** @@ -377,7 +377,7 @@ public boolean isFieldValue(int field, String value) { } private void addGroup(String msg, int field, int delim, DataDictionary dataDictionary) { - groups.put(new IntStringPair(field, msg), new GroupInfo(delim, dataDictionary)); + groups.put(msg, field, new GroupInfo(delim, dataDictionary)); } /** @@ -389,7 +389,7 @@ private void addGroup(String msg, int field, int delim, DataDictionary dataDicti * @return true if field starts a repeating group, false otherwise */ public boolean isGroup(String msg, int field) { - return groups.containsKey(new IntStringPair(field, msg)); + return groups.contains(msg, field); } /** @@ -399,7 +399,7 @@ public boolean isGroup(String msg, int field) { * @return true if field starts a repeating group, false otherwise */ public boolean isHeaderGroup(int field) { - return groups.containsKey(new IntStringPair(field, HEADER_ID)); + return groups.contains(HEADER_ID, field); } /** @@ -410,7 +410,7 @@ public boolean isHeaderGroup(int field) { * @return an object containing group-related metadata */ public GroupInfo getGroup(String msg, int field) { - return groups.get(new IntStringPair(field, msg)); + return groups.get(msg, field); } /** @@ -464,8 +464,10 @@ public boolean isAllowUnknownMessageFields() { */ public void setCheckUnorderedGroupFields(boolean flag) { checkUnorderedGroupFields = flag; - for (GroupInfo gi : groups.values()) { - gi.getDataDictionary().setCheckUnorderedGroupFields(flag); + for (Map gm : groups.values()) { + for (GroupInfo gi : gm.values()) { + gi.getDataDictionary().setCheckUnorderedGroupFields(flag); + } } } @@ -476,8 +478,10 @@ public void setCheckUnorderedGroupFields(boolean flag) { */ public void setCheckFieldsHaveValues(boolean flag) { checkFieldsHaveValues = flag; - for (GroupInfo gi : groups.values()) { - gi.getDataDictionary().setCheckFieldsHaveValues(flag); + for (Map gm : groups.values()) { + for (GroupInfo gi : gm.values()) { + gi.getDataDictionary().setCheckFieldsHaveValues(flag); + } } } @@ -488,15 +492,19 @@ public void setCheckFieldsHaveValues(boolean flag) { */ public void setCheckUserDefinedFields(boolean flag) { checkUserDefinedFields = flag; - for (GroupInfo gi : groups.values()) { - gi.getDataDictionary().setCheckUserDefinedFields(flag); + for (Map gm : groups.values()) { + for (GroupInfo gi : gm.values()) { + gi.getDataDictionary().setCheckUserDefinedFields(flag); + } } } public void setAllowUnknownMessageFields(boolean allowUnknownFields) { allowUnknownMessageFields = allowUnknownFields; - for (GroupInfo gi : groups.values()) { - gi.getDataDictionary().setAllowUnknownMessageFields(allowUnknownFields); + for (Map gm : groups.values()) { + for (GroupInfo gi : gm.values()) { + gi.getDataDictionary().setAllowUnknownMessageFields(allowUnknownFields); + } } } @@ -545,15 +553,18 @@ private static void copyMap(Map lhs, Map rhs) { } /** copy groups including their data dictionaries and validation settings - * + * * @param lhs target * @param rhs source */ - private static void copyGroups(Map lhs, Map rhs) { + private static void copyGroups(StringIntegerMap lhs, StringIntegerMap rhs) { lhs.clear(); - for (Map.Entry entry : rhs.entrySet()) { - GroupInfo value = new GroupInfo(entry.getValue().getDelimiterField(), new DataDictionary(entry.getValue().getDataDictionary())); - lhs.put(entry.getKey(), value); + for (Map.Entry> outer : rhs.entrySet()) { + for (Map.Entry entry : outer.getValue().entrySet()) { + GroupInfo value = new GroupInfo(entry.getValue().getDelimiterField(), + new DataDictionary(entry.getValue().getDataDictionary())); + lhs.put(outer.getKey(), entry.getKey(), value); + } } } @@ -1199,36 +1210,42 @@ private String getAttribute(Node node, String name, String defaultValue) { return defaultValue; } - private static final class IntStringPair { - private final int intValue; + private static class StringIntegerMap extends HashMap> { - private final String stringValue; + public boolean contains(String group, int field) { + Map map = get(group); + return map != null && map.containsKey(field); + } - public IntStringPair(int value, String value2) { - intValue = value; - stringValue = value2; + public V get(String group, int field) { + Map map = get(group); + return map == null ? null : map.get(field); } - @Override - public boolean equals(Object other) { - return this == other - || other instanceof IntStringPair - && intValue == ((IntStringPair) other).intValue - && stringValue.equals(((IntStringPair) other).stringValue); + public void put(String group, int field, V value) { + computeIfAbsent(group, __ -> new HashMap<>()) + .put(field, value); } - @Override - public int hashCode() { - return stringValue.hashCode() + intValue; + } + + private static class IntegerStringMap extends HashMap> { + + public boolean contains(int field, String group) { + Map map = get(field); + return map != null && map.containsKey(group); } - /** - * For debugging - */ - @Override - public String toString() { - return '(' + intValue + ',' + stringValue + ')'; + public V get(int field, String group) { + Map map = get(field); + return map == null ? null : map.get(group); } + + public void put(int field, String group, V value) { + computeIfAbsent(field, __ -> new HashMap<>()) + .put(group, value); + } + } /**