Skip to content

Commit 53efb6d

Browse files
authored
Merge pull request #842 from quickfix-j/replace-parse-int
replaced some `Integer.parseInt()` by `IntConverter.convert()`
2 parents fcd9fba + 6a65e2b commit 53efb6d

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -803,18 +803,28 @@ private void checkHasValue(ValidationSettings settings, StringField field) {
803803
}
804804
}
805805

806-
/** Check if group count matches number of groups in **/
806+
/**
807+
* Check if group count matches number of groups in message. *
808+
*/
807809
private void checkGroupCount(StringField field, FieldMap fieldMap, String msgType) {
808810
final int fieldNum = field.getField();
809811
if (isGroup(msgType, fieldNum)) {
810-
if (fieldMap.getGroupCount(fieldNum) != Integer.parseInt(field.getValue())) {
811-
throw new FieldException(
812-
SessionRejectReason.INCORRECT_NUMINGROUP_COUNT_FOR_REPEATING_GROUP,
813-
fieldNum);
812+
try {
813+
if (fieldMap.getGroupCount(fieldNum) != IntConverter.convert(field.getValue())) {
814+
throwNewFieldException(fieldNum);
815+
}
816+
} catch (FieldConvertError ex) {
817+
throwNewFieldException(fieldNum);
814818
}
815819
}
816820
}
817821

822+
private void throwNewFieldException(final int fieldNum) throws FieldException {
823+
throw new FieldException(
824+
SessionRejectReason.INCORRECT_NUMINGROUP_COUNT_FOR_REPEATING_GROUP,
825+
fieldNum);
826+
}
827+
818828
/** Check if a message has all required fields. **/
819829
void checkHasRequired(FieldMap header, FieldMap body, FieldMap trailer, String msgType,
820830
boolean bodyOnly) {
@@ -972,7 +982,12 @@ private void load(InputStream inputStream, DocumentBuilderFactory factory) throw
972982
throw new ConfigError("<field> " + name + " does not have a number attribute");
973983
}
974984

975-
final int num = Integer.parseInt(number);
985+
int num = 0;
986+
try {
987+
num = IntConverter.convert(number);
988+
} catch (FieldConvertError ex) {
989+
throw new ConfigError(ex);
990+
}
976991

977992
final String type = getAttribute(fieldNode, "type");
978993
if (type == null) {
@@ -1068,8 +1083,8 @@ private void load(InputStream inputStream, DocumentBuilderFactory factory) throw
10681083
private int getIntegerAttributeIfDefined(final Element documentElement, final String attribute) throws ConfigError {
10691084
try {
10701085
return documentElement.hasAttribute(attribute)
1071-
? Integer.parseInt(documentElement.getAttribute(attribute)) : 0;
1072-
} catch (NumberFormatException e) {
1086+
? IntConverter.convert(documentElement.getAttribute(attribute)) : 0;
1087+
} catch (FieldConvertError e) {
10731088
throw new ConfigError("Attribute " + attribute + " could not be parsed as Integer.", e);
10741089
}
10751090
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import quickfix.field.TargetSubID;
5858
import quickfix.field.XmlData;
5959
import quickfix.field.XmlDataLen;
60+
import quickfix.field.converter.IntConverter;
6061

6162
import javax.xml.parsers.DocumentBuilderFactory;
6263
import javax.xml.transform.OutputKeys;
@@ -727,8 +728,8 @@ private void parseGroup(String msgType, StringField field, DataDictionary dd, Da
727728
// QFJ-533
728729
int declaredGroupCount = 0;
729730
try {
730-
declaredGroupCount = Integer.parseInt(field.getValue());
731-
} catch (final NumberFormatException e) {
731+
declaredGroupCount = IntConverter.convert(field.getValue());
732+
} catch (final FieldConvertError e) {
732733
throw MessageUtils.newInvalidMessageException("Repeating group count requires an Integer but found '" + field.getValue() + "' in " + messageData, this);
733734
}
734735
parent.setField(groupCountTag, field);
@@ -927,8 +928,8 @@ private StringField extractField(DataDictionary dataDictionary, FieldMap fields)
927928

928929
int tag;
929930
try {
930-
tag = Integer.parseInt(messageData.substring(position, equalsOffset));
931-
} catch (final NumberFormatException e) {
931+
tag = IntConverter.convert(messageData.substring(position, equalsOffset));
932+
} catch (final FieldConvertError e) {
932933
position = messageData.indexOf('\001', position + 1) + 1;
933934
throw MessageUtils.newInvalidMessageException("Bad tag format: " + e.getMessage() + " in " + messageData, this);
934935
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.regex.Matcher;
5050
import java.util.regex.Pattern;
5151
import java.util.stream.Collectors;
52+
import quickfix.field.converter.IntConverter;
5253

5354
/**
5455
* Settings for sessions. Settings are grouped by FIX version and target company
@@ -339,11 +340,7 @@ public int getIntOrDefault(String key, int defaultValue) throws ConfigError, Fie
339340
* @throws FieldConvertError error during field type conversion.
340341
*/
341342
public int getInt(SessionID sessionID, String key) throws ConfigError, FieldConvertError {
342-
try {
343-
return Integer.parseInt(getString(sessionID, key));
344-
} catch (final NumberFormatException e) {
345-
throw new FieldConvertError(e.getMessage());
346-
}
343+
return IntConverter.convert(getString(sessionID, key));
347344
}
348345

349346
/**

0 commit comments

Comments
 (0)