Is it ok to extend utilities?

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// 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;
}
</code>
<code>// 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; } </code>
// 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

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import my.almost.apache.MyStringUtils;
public static void shuffleUntilSurprised(CharSequence seq) {
while (!MyStringUtils.endsWith(seq, "?!")) {
seq = MyStringUtils.shuffled(seq);
}
}
</code>
<code>import my.almost.apache.MyStringUtils; public static void shuffleUntilSurprised(CharSequence seq) { while (!MyStringUtils.endsWith(seq, "?!")) { seq = MyStringUtils.shuffled(seq); } } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
}
</code>
<code>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); } } </code>
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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is it ok to extend utilities?

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// 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;
}
</code>
<code>// 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; } </code>
// 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

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import my.almost.apache.MyStringUtils;
public static void shuffleUntilSurprised(CharSequence seq) {
while (!MyStringUtils.endsWith(seq, "?!")) {
seq = MyStringUtils.shuffled(seq);
}
}
</code>
<code>import my.almost.apache.MyStringUtils; public static void shuffleUntilSurprised(CharSequence seq) { while (!MyStringUtils.endsWith(seq, "?!")) { seq = MyStringUtils.shuffled(seq); } } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
}
</code>
<code>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); } } </code>
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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật