Browse Source

add a Utility method to concatenate CharSequences

pull/110/head
Jochen Berger 9 years ago
parent
commit
e29858db73
  1. 36
      json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java

36
json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java

@ -74,6 +74,42 @@ public final class Utils {
public static String join(String delimiter, Iterable<? extends Object> objs) {
return join(delimiter, "", objs);
}
public static String concat(CharSequence... strings) {
if (strings.length == 0){
return "";
}
if (strings.length == 1){
return strings[0].toString();
}
int length = 0;
// -1 = no result, -2 = multiple results
int indexOfSingleNonEmptyString = -1;
for (int i = 0; i< strings.length; i++) {
CharSequence charSequence = strings[i];
int len = charSequence.length();
length += len;
if (indexOfSingleNonEmptyString != -2 && len > 0){
if (indexOfSingleNonEmptyString == -1){
indexOfSingleNonEmptyString = i;
} else {
indexOfSingleNonEmptyString = -2;
}
}
}
if (length == 0){
return "";
}
if (indexOfSingleNonEmptyString > 0){
return strings[indexOfSingleNonEmptyString].toString();
}
StringBuilder sb = new StringBuilder(length);
for (CharSequence charSequence : strings) {
sb.append(charSequence);
}
return sb.toString();
}
//---------------------------------------------------------
//

Loading…
Cancel
Save