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-41 : Added WordUtils.abbreviate support to commons-text #38
Conversation
Hi @chtompki @PascalSchumacher @kinow , I was going through each and every JIRA marked for 1.1 release and seems they are pending since long just like TEXT-41, pushing code for TEXT-41 for now, let me know if this looks good ? |
I'll try to give this a look sometime tonight or tomorrow morning. |
Had a quick (really quick) look today in between meetings. Looks good to me. Wonder if it'd be better to use a StringBuilder rather than StringBuffer. And perhaps add a test with other negative values, not just -1 (because even though the contract states -1 is a valid option, users can still pass -100, -10, etc... for typo, bad use, bug, etc). |
* This is appended ONLY if the string was indeed abbreviated. | ||
* The append does not count towards the lower or upper limits. | ||
* @return the abbreviated String. | ||
* @since 2.4 |
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.
should be 1.1
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.
Sorry I have to correct myself. The tag should be removed as the class already has @since 1.1
.
|
||
if (str.length() == 0) { | ||
return StringUtils.EMPTY; | ||
} |
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.
The two if clauses can be replaced by:
if (StringUtils.isEmpty(str)) {
return str;
}
+1to StringBuilder What about throwing IllegalArgumentExceptions for |
Makes sense IMO. The upper < -1 would be nice and could show users they have a bug (as values lower than -1 were not supposed to be used). upper < lower would be changing the behaviour from [lang]; which I'm fine... as this is a good chance, as we are migrating it to [text]... so +1 as well. |
StringBuffer makes it implicitly threadsafe, if we use StringBuilder users have to take care threadsafty themselves, that could be the reason Commons lang used it before. |
@ameyjadiye The thread-safety of |
My guess would be that [lang] uses it as this method existed probably before Java 1.5. If you look at the scope, there is no need to call synchronized methods. |
@PascalSchumacher , got it , will fix everything today, let me know if rest things are good I'm happy to make it perfect for release. |
2 similar comments
…pective testcases
@kinow @chtompki @PascalSchumacher , all discussed improvements and their tests are pushed and looks good to me now, let me know if anything else is required for this PR ? |
I'm open to these changes existing here, but do we have a solid reason for them not going in [lang]'s Consider this: Lastly we'll want to be sure that we fix this bug: Any thoughts here? I'm not very partial to either direction, putting this in |
I'm in favour of keeping version of |
I'm ok with that. I'm going to fiddle around with this a tad to see if I can re-create the problem that they were having in LANG-673. |
As it is functionally is different from the +1 to including the fix LANG-673. |
I'm still fiddling around with how to re-create the issue that LANG-673 is talking about. I couldn't get that test to cause an exception on the code here: apache/commons-lang@ead7d96 I'll keep fiddling around and let you know when I figure out what the issue is really getting after. |
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.
Thanks for all the updates so far! 👍
* the String there. It will also append any String passed as a parameter | ||
* to the end of the String. The upper limit can be specified to forcibly | ||
* abbreviate a String. | ||
* |
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 we should add some examples to the javadoc. I often find these very helpful. What do you think?
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.
Yeah Agreed [+1] , nothing is more helpful than samples in javadoc.
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 a bad idea. That's what [lang] seems to do.
// throw IllegalArgumentException if upper value is less than lower value. | ||
if (upper < lower && upper != -1) { | ||
throw new IllegalArgumentException("upper value is less than lower value"); | ||
} |
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 the validation should be done at the very start of the method.
Code could be slightly shortened by using the Validate
class from commons-lang.
Comments for the validation can be removed imho the do not add anything beyond the code.
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 have moved validation on method start but cant find appropriate Validation method to use the way we are doing validations, also i think code here is easy to understand anyway.
I reproduced the issue and confirmed that you don't have it in your code. The test that should cover it is: assertEquals("01 23 45 67 89",WordUtils.abbreviate("01 23 45 67 89", 40, 40, "")); can you add that? For the sake of rigor, that exception existed when the method appeared as: public static String abbreviate(String str, int lower, int upper, String appendToEnd) {
// initial parameter checks
if (str == null) {
return null;
}
if (str.length() == 0) {
return StringUtils.EMPTY;
}
// if the upper value is -1 (i.e. no limit) or is greater
// than the length of the string, set to the length of the string
if (upper == -1 || upper > str.length()) {
upper = str.length();
}
// if upper is less than lower, raise it to lower
if (upper < lower) {
upper = lower;
}
StringBuffer result = new StringBuffer();
int index = StringUtils.indexOf(str, " ", lower);
if (index == -1) {
result.append(str.substring(0, upper));
// only if abbreviation has occured do we append the appendToEnd value
if (upper != str.length()) {
result.append(StringUtils.defaultString(appendToEnd));
}
} else if (index > upper) {
result.append(str.substring(0, upper));
result.append(StringUtils.defaultString(appendToEnd));
} else {
result.append(str.substring(0, index));
result.append(StringUtils.defaultString(appendToEnd));
}
return result.toString();
} |
Thanks much @chtompki, added test and current implementation is handing scenario as expected and not throwing exception. |
Sure glad to help there. |
1 similar comment
2 similar comments
@chtompki , I think we have covered pretty much everything now, let me know anything pending/blocker to accept this PR. |
Thanks for the updates! I think the pull request is ready to merge now. 👍 |
No description provided.