From 675d30d88282e4ee8e96952d672ea1e7fa330ee6 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne <31027858+Isira-Seneviratne@users.noreply.github.com> Date: Mon, 20 Apr 2020 20:52:56 +0000 Subject: [PATCH] Simplify some if statements in StringUtils. (#521) * Simplify if-else statements at the start of abbreviate(). * Combine if statements in abbreviateMiddle(). * Simplify the first if condition of substringBetween(). * Simplify the first if condition of stripAll(). --- .../org/apache/commons/lang3/StringUtils.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index a585c2eb3..941dd5c45 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -330,11 +330,9 @@ public class StringUtils { * @since 3.6 */ public static String abbreviate(final String str, final String abbrevMarker, int offset, final int maxWidth) { - if (isEmpty(str) && isEmpty(abbrevMarker)) { - return str; - } else if (isNotEmpty(str) && EMPTY.equals(abbrevMarker) && maxWidth > 0) { - return str.substring(0, maxWidth); - } else if (isEmpty(str) || isEmpty(abbrevMarker)) { + if (isNotEmpty(str) && EMPTY.equals(abbrevMarker) && maxWidth > 0) { + return substring(str, 0, maxWidth); + } else if (isAnyEmpty(str, abbrevMarker)) { return str; } final int abbrevMarkerLength = abbrevMarker.length(); @@ -395,11 +393,7 @@ public class StringUtils { * @since 2.5 */ public static String abbreviateMiddle(final String str, final String middle, final int length) { - if (isEmpty(str) || isEmpty(middle)) { - return str; - } - - if (length >= str.length() || length < middle.length()+2) { + if (isAnyEmpty(str, middle) || length >= str.length() || length < middle.length()+2) { return str; } @@ -8274,8 +8268,8 @@ public class StringUtils { * @return the stripped Strings, {@code null} if null array input */ public static String[] stripAll(final String[] strs, final String stripChars) { - int strsLen; - if (strs == null || (strsLen = strs.length) == 0) { + int strsLen = ArrayUtils.getLength(strs); + if (strsLen == 0) { return strs; } final String[] newArr = new String[strsLen]; @@ -8772,7 +8766,7 @@ public class StringUtils { * @since 2.0 */ public static String substringBetween(final String str, final String open, final String close) { - if (str == null || open == null || close == null) { + if (!ObjectUtils.allNotNull(str, open, close)) { return null; } final int start = str.indexOf(open);