-
Notifications
You must be signed in to change notification settings - Fork 76
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
Optimize parameter parsing in text chunking processor #733
Changes from 5 commits
f0b0ec3
2ca0abf
b5ae017
4e60573
7e643ea
d009ffa
63e89d1
1ebab45
55cf7f5
988152a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,14 +39,36 @@ public static String parseStringParameter(final Map<String, Object> parameters, | |
return fieldValue.toString(); | ||
} | ||
|
||
/** | ||
* Parse integer type parameter without default value | ||
* Throw IllegalArgumentException if parameter is missing or is not an integer. | ||
*/ | ||
public static int parseIntegerParameter(final Map<String, Object> parameters, final String fieldName) { | ||
if (!parameters.containsKey(fieldName)) { | ||
throw new IllegalArgumentException(String.format(Locale.ROOT, "Parameter [%s] is missing", fieldName)); | ||
} | ||
String fieldValueString = parameters.get(fieldName).toString(); | ||
try { | ||
return NumberUtils.createInteger(fieldValueString); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException( | ||
String.format(Locale.ROOT, "Parameter [%s] must be of %s type", fieldName, Integer.class.getName()) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Parse integer type parameter. | ||
* Throw IllegalArgumentException if parameter is not an integer. | ||
* Throw IllegalArgumentException if both parameter and default value is missing or parameter is not an integer. | ||
*/ | ||
public static int parseIntegerParameter(final Map<String, Object> parameters, final String fieldName, final int defaultValue) { | ||
public static int parseIntegerParameter(final Map<String, Object> parameters, final String fieldName, final Integer defaultValue) { | ||
if (!parameters.containsKey(fieldName)) { | ||
// all integer parameters are optional | ||
return defaultValue; | ||
// return the default value when parameter is not existing | ||
if (defaultValue == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest splitting this method to three new methods:
This won't add repeated code also can make sure the method 2 have concise semantic by making the default value type to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since all integer parameters are optional. I will not keep the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, runtime parameters are always available while non-runtime parameters have default values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
throw new IllegalArgumentException(String.format(Locale.ROOT, "Parameter [%s] is missing", fieldName)); | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
String fieldValueString = parameters.get(fieldName).toString(); | ||
try { | ||
|
@@ -60,9 +82,13 @@ public static int parseIntegerParameter(final Map<String, Object> parameters, fi | |
|
||
/** | ||
* Parse integer type parameter with positive value. | ||
* Throw IllegalArgumentException if parameter is not a positive integer. | ||
* Throw IllegalArgumentException if both parameter and default value is missing or parameter is not a positive integer. | ||
*/ | ||
public static int parsePositiveIntegerParameter(final Map<String, Object> parameters, final String fieldName, final int defaultValue) { | ||
public static int parsePositiveIntegerParameter( | ||
final Map<String, Object> parameters, | ||
final String fieldName, | ||
final Integer defaultValue | ||
) { | ||
int fieldValueInt = parseIntegerParameter(parameters, fieldName, defaultValue); | ||
if (fieldValueInt <= 0) { | ||
throw new IllegalArgumentException(String.format(Locale.ROOT, "Parameter [%s] must be positive.", fieldName)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this design doesn't follow conventions in other place in Java API.
If you calling parse(argument) than it should execute parsing logic
If you calling parse(argument, defaultValue) than it should try to execute parsing logic, and return default value in case that parsing logic return empty value
In your code everywhere where this new method called with null you can call your new method without default value.
And in that new method implementation you can just call this method with defaultValue = null, see my previous comment with suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I don't think the function is necessary. I have removed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not very sure what you mean here. We should throw illegal argument exception if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated by Zan's comment. You can review now.