2

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?

4
  • 1
    I don't understand what benefit you see by extending StringUtils. Do you need access to protected methods inside StringUtils?
    – Doc Brown
    Commented Sep 4 at 17:18
  • @DocBrown you can use one utility (the extension) instead of two Commented Sep 5 at 2:56
  • So you see it as a benefit that you can write an even longer StringUtilsEx.Rotate(x,n) instead of StringUtils.Rotate(x,n)? I would understand this when the implementation of shuffled would become significantly simpler, but I don't see any indications for that.
    – Doc Brown
    Commented Sep 5 at 5:41
  • 2
    ... Moreover, this question is quite Java specific (even in C#, which is very similar to Java, I would solve this differently with extension methods). I took the freedom to add a Java tag - I have never understood why people make the assumption that when no language is mentioned for some code, everyone shall assume it is Java.
    – Doc Brown
    Commented Sep 5 at 5:46

2 Answers 2

5

I don't think there's any justification for extending a util class like that - or even if extending has any technical meaning for a static class with static utils.

Let's say you do create your own MyStringUtils with the new shuffled method, your usage will look something like this contrived example, which uses both your method and a standard one:

import my.almost.apache.MyStringUtils;

public static void shuffleUntilSurprised(CharSequence seq) {
   while (!MyStringUtils.endsWith(seq, "?!")) {
      seq = MyStringUtils.shuffled(seq);
   }
}

As you can see, you use the same static class name for both methods, which keeps your func relatively clear, but can introduce complications later on, as you say, if StringUtils ever adds conflicting methods, but also makes anyone reading your code have to check whether MyStringUtils.endsWith is really the same as StringUtils.endsWith, or whether there's any overrides involved.

This is the alternative, if you create a completely separate class for your shuffled method, without touching the Apache class:

import org.apache.commons.lang3.StringUtils;
import my.really.not.apache.MyStringUtils;

public static void shuffleUntilSurprised(CharSequence seq) {
   while (!StringUtils.endsWith(seq, "?!")) {
      seq = MyStringUtils.shuffled(seq);
   }
}

The difference is that endsWith and shuffled are called from different classes, but the code is no less clear, has no dependency issue with future updates to StringUtil, and is clear to anyone familiar with Apache StringUtil what is going on.

My suggestion? Just create a new class for your new static helpers, and use Apache for the existing ones, side by side.

4
  • Thank you. But what should I call it? I'd have to come up with some contrived name (like MyStringUtils) Commented Sep 4 at 8:08
  • 1
    There are many examples you can find. In Microsoft-land, last time I was there, it was realtively common to add Ex to signify an extended version of a library or function (e.g. StringUtilsEx). In Go, it's common to keep the same package name, and alias one of them on import (e.g.: import ( original.stringutil stringutil2 = my.stringutil ) Commented Sep 4 at 8:16
  • 2
    @SergeyZolotarev ah, naming. Now you're talking about the real programing problem. ;) But seriously, StringUtils or StringHelpers inside your own namespace sounds good to me.
    – freakish
    Commented Sep 4 at 9:34
  • 1
    @freakish, to be honest, naming is the root cause of the OP's question. It is one of the two hardest things.... Commented Sep 4 at 14:12
0

It's a weighing of concerns, but I would extend it.

  1. It's where it belongs conceptually - that's the StringUtils type for string utils
  2. You implement it in a way you expect StringUtils to behave and implement it if it were to
  3. If StringUtils ever implements it you can drop your own - now the same concern is provided natively
  4. If StringUtils ever implements it and causes you a conflict (duplicate declaration error) that's an intentional side effect that forces you to clear up (instead of now having two util types and two shuffle methods and subsequently both being used, hopefully with same or acceptably similar behavior)
  5. You FIND the method through intellisense/completion when you access/type StringUtils. instead of requiring project domain knowledge
  6. You don't have to import additional, different namespaces for string utility, to find string util types, or string util methods

From a system design, component concern viewpoint it belongs into StringUtils.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.