commons-lang icon indicating copy to clipboard operation
commons-lang copied to clipboard

Lang-1345 Enhance non-empty strings

Open ThrawnCA opened this issue 6 years ago • 7 comments

ThrawnCA avatar Jul 17 '17 23:07 ThrawnCA

Coverage Status

Coverage decreased (-0.2%) to 94.9% when pulling 3ec23f1f2606d6578340209b5a6540c911a7e6c8 on ThrawnCA:LANG-1345-enhance-non-empty-strings into f6923510352fc3fbfad68bc6c5ac5258a34671b7 on apache:master.

coveralls avatar Jul 17 '17 23:07 coveralls

For me is not clear importance of the new methods. I can't remember situations when I needed for something like this. And even if I need something like this I'll write the function myself and I'll not even think about to search something in Commons Lang library. Time spent to looking for the method will be ten time more then write myself. Maybe commons lang itself uses the function internally then it can make sense.

It would be great if you can give us an examples of usage and how they are important.

stokito avatar Jun 23 '18 18:06 stokito

A typical case would be that you're assembling a sentence, and if a component is not empty, you need to add a space beforehand. (Sometimes join will handle this, but other times it's not the right tool for the situation.)

Or you're building a URL, and if there's a non-blank query string, you need to put a question mark before it.

Really, any situation where part of a string is optional but requires special formatting if present.

@garydgregory The trouble with "default" names is that this functionality is the opposite. The defaultXXX methods have the logic, "if present, leave untouched; otherwise use the second parameter." This would instead be, "if present, apply the second parameter; otherwise, leave untouched." Could use "appendIfNotXXX" and "prependIfNotXXX" I suppose, but I'd rather combine prefixes and suffixes in one method.

ThrawnCA avatar Jun 24 '18 22:06 ThrawnCA

Could use "appendIfNotXXX" and "prependIfNotXXX" I suppose, but I'd rather combine prefixes and suffixes in one method.

I think this naming would be better and more granular.

aaabramov avatar Sep 26 '18 06:09 aaabramov

And I've come across another situation where I want this: assembling a filename from customer name parts, and adding a separator for middle name if (and only if) it's provided.

ThrawnCA avatar Nov 01 '18 06:11 ThrawnCA

Bump. I believe I've addressed the changes that were requested.

ThrawnCA avatar May 12 '21 23:05 ThrawnCA

Just came across another use case for this. One of our systems is retrieving customer email addresses, if provided, and decorating them with the customer's name, eg John Smith <[email protected]>. If there is no email address, then it should return an empty string, rather than John Smith <>.

ThrawnCA avatar May 24 '21 22:05 ThrawnCA