Apache Commons has StringUtils
. It's great, but I wish it had a shuffle()
method or similar
Is it ok to create my own StringUtils
that extends Apache's StringUtils
and adds the method (Apache's class is not final
)? That way, I will get the best of both worlds
// something like this
public static CharSequence shuffled(CharSequence originalSequence) {
List<Character> characters = originalSequence.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.toList());
Collections.shuffle(characters, random);
StringBuilder shuffledSequence = new StringBuilder();
characters.forEach(shuffledSequence::append);
return shuffledSequence;
}
However, extending utilities feels fishy. For example, if Apache's class adds the shuffle()
method at some point (or shuffled()
), I may get an unwanted hiding on update. It's not likely it's going to break anything, and we're not upgrading the artifact version any time soon, but it's still something to consider
I obviously don't want to wrap and write (or even generate) a ton of delegating methods
What is the best way forward?
StringUtils
. Do you need access to protected methods insideStringUtils
?StringUtilsEx.Rotate(x,n)
instead ofStringUtils.Rotate(x,n)
? I would understand this when the implementation ofshuffled
would become significantly simpler, but I don't see any indications for that.