commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benedikt Ritter (JIRA)" <>
Subject [jira] [Commented] (LANG-1030) StringUtils.same opposite method to StringUtils.differ
Date Sat, 26 Jul 2014 09:55:38 GMT


Benedikt Ritter commented on LANG-1030:

Example of normalization for input

public void doSomething(String foo) {
//foo either has to be BAR or BAZ...BAZ would be the default if it isn't the same
String fooNormalized = StringUtils.same("BAR", foo, "BAZ");

//comparing to a ternary operation
String fooNormalized = "BAR".equals(foo) ? "BAR" : "BAZ";

Two comments here:
# if you want either the one string or the other, {{same}} seems to be a bad method name.
It looks more like an extension to {{StringUtils.defaultString(String, String}}. Like so:
{{StringUtils.defaultString(String str, String defaultString, String... allowedValues)}}.
[~dmjones500] how would you feel about adding something like that?
# if you can achieve what you want with a one liner ternary operation, why do you want a specific
method in StringUtils for this?

> StringUtils.same opposite method to StringUtils.differ
> ------------------------------------------------------
>                 Key: LANG-1030
>                 URL:
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.3.2
>            Reporter: Michael Bazos
>            Priority: Trivial
>              Labels: StringUtils
>             Fix For: Review Patch, Discussion
> This is pretty easy to do but I have seen this come up and was surprised to find that
StringUtils didn't have a utility method.
> The difference() method is described as doing the following:
> Compares two Strings, and returns the portion where they differ.
> What I am proposing is essentially the opposite of the difference method called same().
 This method would compare two String and return a new String when they are the same.
> This is essentially the implementation:
> {code:java}
>     public static String same(String str1, String str2) {
>         if (str1 != null && StringUtils.equalsIgnoreCase(str1, str2)) {
>             return new String(str1);
>         }
>         return null;
>     }
> {code}
> {code:java}
> StringUtils.same(null, null) = null
> StringUtils.same("", "") = "";
> StringUtils.same("123", "") = null
> StringUtils.same("123", "123") = "123";
> {code}
> If there is already a way to do this using the apache lang library please point me in
the right direction.  Otherwise I would be more than happy to do the testing, coding and documentation
for this.

This message was sent by Atlassian JIRA

View raw message