Skip to content
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

[TEXT-190] Document negative limit for WordUtils abbreviate method #181

Closed
wants to merge 3 commits into from

Conversation

bjmbing
Copy link
Contributor

@bjmbing bjmbing commented Oct 29, 2020

lower < 0 has no defined behavior in the document. Although it works similar to lower = 0. There is no related document for it.

lower < 0 has no defined behavior in the document
@@ -859,7 +859,7 @@ public static boolean isDelimiter(final int codePoint, final char[] delimiters)
public static String abbreviate(final String str, int lower, int upper, final String appendToEnd) {
Validate.isTrue(upper >= -1, "upper value cannot be less than -1");
Validate.isTrue(upper >= lower || upper == -1, "upper value is less than lower value");

Validate.isTrue(lower >= 0, "lower value cannot be lower than 0");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjmbing thanks for the pull request. Initially this change made sense to me. Then looking at the abbreviate code, looks like the lower limit is used for StringUtils.indexOf(str, " ", /* startPos */ lower).

StringUtils.indexOf javadocs say: "* @param startPos the start position, negative treated as zero".

Currently, if you pass -1 or any other negative value, it is treated as zero, in accord with the StringUtils.indexOf javadocs.

I think a better solution would be to document that negative values are treated as zero, essentially replicating the StringUtils docs. This way, existing code using negative values won't get a runtime exception (which Validate.isTrue would do). What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think the solution you provided is good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the prompt reply @bjmbing! If you update this PR with something similar to the javadocs from StringUtils for the lower limit, we can then create a JIRA issue, and merge this PR :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kinow . I have updated the javadoc for lower in WordUtils.abbreviate.

@@ -828,7 +828,7 @@ public static boolean isDelimiter(final int codePoint, final char[] delimiters)
*
* @param str the string to be abbreviated. If null is passed, null is returned.
* If the empty String is passed, the empty string is returned.
* @param lower the lower limit.
* @param lower the lower limit; < -1 value is treated as 0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better ", negative treated as zero", as any negative value, not only -1 is treated as 0

@kinow kinow changed the title Add validate for WordUtils.java [TEXT-190] Document negative limit for WordUtils abbreviate method Oct 29, 2020
@kinow
Copy link
Member

kinow commented Oct 29, 2020

I've created https://issues.apache.org/jira/browse/TEXT-190 for this issue, updated the title. Once the PR passes review, we should be ready to merge and include in the changelog 👍 Thanks @bjmbing !

@bjmbing
Copy link
Contributor Author

bjmbing commented Oct 29, 2020

I changed the doc as suggested. @kinow . Thank you for your help

@kinow kinow closed this in ee61b91 Oct 29, 2020
@kinow
Copy link
Member

kinow commented Oct 29, 2020

JIRA issue resolved, added your change with credit/thanks to @bjmbing, merged! Thanks again @bjmbing !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants