Skip to content

Commit

Permalink
Merge pull request #2193 from SanojPunchihewa/prop-int-short
Browse files Browse the repository at this point in the history
Explicitly convert decimals to int or short as XPAth functions return decimals
  • Loading branch information
arunans23 authored Jun 28, 2024
2 parents 33b7406 + ac54e82 commit b2844eb
Showing 1 changed file with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public void setValue(String value) {
public void setValue(String value, String type) {
this.type = type;
// Convert the value into specified type
this.value = convertValue(value, type);
this.value = convertValue(value, type, false);
}

public String getType() {
Expand Down Expand Up @@ -422,14 +422,14 @@ private Object getResultValue(MessageContext synCtx) {
return valueElement.cloneOMElement();
} else {
if(expression != null) {
return convertValue(expression.stringValueOf(synCtx), type);
return convertValue(expression.stringValueOf(synCtx), type, true);
}
}

return null;
}

private Object convertValue(String value, String type) {
private Object convertValue(String value, String type, boolean isExpression) {
if (type == null) {
// If no type is set we simply return the string value
return value;
Expand All @@ -441,10 +441,10 @@ private Object convertValue(String value, String type) {
case BOOLEAN : return JavaUtils.isTrueExplicitly(value);
case DOUBLE : return Double.parseDouble(value);
case FLOAT : return Float.parseFloat(value);
case INTEGER : return Integer.parseInt(value);
case INTEGER : return parseInteger(value, isExpression);
case LONG : return Long.parseLong(value);
case OM : return buildOMElement(value);
case SHORT : return Short.parseShort(value);
case SHORT : return parseShort(value, isExpression);
case JSON : return buildJSONElement(value);
default : return value;
}
Expand All @@ -456,6 +456,34 @@ private Object convertValue(String value, String type) {
}
}

/**
* This method will explicitly convert decimals to int since XPAth functions return numbers with decimal.
*
* @param value String value returned from XPAth function
* @param isExpression Boolean to check whether the value is from XPAth function
* @return parsed Short value
*/
private int parseInteger(String value, boolean isExpression) {
if (isExpression && value.contains(".")) {
return (int) Double.parseDouble(value);
}
return Integer.parseInt(value);
}

/**
* This method will explicitly convert decimals to short since XPAth functions return numbers with decimal.
*
* @param value String value returned from XPAth function
* @param isExpression Boolean to check whether the value is from XPAth function
* @return parsed Short value
*/
private short parseShort(String value, boolean isExpression) {
if (isExpression && value.contains(".")) {
return (short) Double.parseDouble(value);
}
return Short.parseShort(value);
}

/**
* Apply the Regular expression on to the String value and choose the matching group.
* If a matching not found same string passed in to this method will be returned.
Expand Down

0 comments on commit b2844eb

Please sign in to comment.