Skip to content

Commit

Permalink
Merge pull request #842 from quickfix-j/replace-parse-int
Browse files Browse the repository at this point in the history
replaced some `Integer.parseInt()` by `IntConverter.convert()`
  • Loading branch information
chrjohn authored Jul 4, 2024
2 parents fcd9fba + 6a65e2b commit 53efb6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
31 changes: 23 additions & 8 deletions quickfixj-base/src/main/java/quickfix/DataDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -803,18 +803,28 @@ private void checkHasValue(ValidationSettings settings, StringField field) {
}
}

/** Check if group count matches number of groups in **/
/**
* Check if group count matches number of groups in message. *
*/
private void checkGroupCount(StringField field, FieldMap fieldMap, String msgType) {
final int fieldNum = field.getField();
if (isGroup(msgType, fieldNum)) {
if (fieldMap.getGroupCount(fieldNum) != Integer.parseInt(field.getValue())) {
throw new FieldException(
SessionRejectReason.INCORRECT_NUMINGROUP_COUNT_FOR_REPEATING_GROUP,
fieldNum);
try {
if (fieldMap.getGroupCount(fieldNum) != IntConverter.convert(field.getValue())) {
throwNewFieldException(fieldNum);
}
} catch (FieldConvertError ex) {
throwNewFieldException(fieldNum);
}
}
}

private void throwNewFieldException(final int fieldNum) throws FieldException {
throw new FieldException(
SessionRejectReason.INCORRECT_NUMINGROUP_COUNT_FOR_REPEATING_GROUP,
fieldNum);
}

/** Check if a message has all required fields. **/
void checkHasRequired(FieldMap header, FieldMap body, FieldMap trailer, String msgType,
boolean bodyOnly) {
Expand Down Expand Up @@ -972,7 +982,12 @@ private void load(InputStream inputStream, DocumentBuilderFactory factory) throw
throw new ConfigError("<field> " + name + " does not have a number attribute");
}

final int num = Integer.parseInt(number);
int num = 0;
try {
num = IntConverter.convert(number);
} catch (FieldConvertError ex) {
throw new ConfigError(ex);
}

final String type = getAttribute(fieldNode, "type");
if (type == null) {
Expand Down Expand Up @@ -1068,8 +1083,8 @@ private void load(InputStream inputStream, DocumentBuilderFactory factory) throw
private int getIntegerAttributeIfDefined(final Element documentElement, final String attribute) throws ConfigError {
try {
return documentElement.hasAttribute(attribute)
? Integer.parseInt(documentElement.getAttribute(attribute)) : 0;
} catch (NumberFormatException e) {
? IntConverter.convert(documentElement.getAttribute(attribute)) : 0;
} catch (FieldConvertError e) {
throw new ConfigError("Attribute " + attribute + " could not be parsed as Integer.", e);
}
}
Expand Down
9 changes: 5 additions & 4 deletions quickfixj-base/src/main/java/quickfix/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import quickfix.field.TargetSubID;
import quickfix.field.XmlData;
import quickfix.field.XmlDataLen;
import quickfix.field.converter.IntConverter;

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

int tag;
try {
tag = Integer.parseInt(messageData.substring(position, equalsOffset));
} catch (final NumberFormatException e) {
tag = IntConverter.convert(messageData.substring(position, equalsOffset));
} catch (final FieldConvertError e) {
position = messageData.indexOf('\001', position + 1) + 1;
throw MessageUtils.newInvalidMessageException("Bad tag format: " + e.getMessage() + " in " + messageData, this);
}
Expand Down
7 changes: 2 additions & 5 deletions quickfixj-core/src/main/java/quickfix/SessionSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import quickfix.field.converter.IntConverter;

/**
* Settings for sessions. Settings are grouped by FIX version and target company
Expand Down Expand Up @@ -339,11 +340,7 @@ public int getIntOrDefault(String key, int defaultValue) throws ConfigError, Fie
* @throws FieldConvertError error during field type conversion.
*/
public int getInt(SessionID sessionID, String key) throws ConfigError, FieldConvertError {
try {
return Integer.parseInt(getString(sessionID, key));
} catch (final NumberFormatException e) {
throw new FieldConvertError(e.getMessage());
}
return IntConverter.convert(getString(sessionID, key));
}

/**
Expand Down

0 comments on commit 53efb6d

Please sign in to comment.