diff --git a/src/main/java/org/apache/commons/lang3/ClassUtils.java b/src/main/java/org/apache/commons/lang3/ClassUtils.java index d1462ed3e..e6a0931d8 100644 --- a/src/main/java/org/apache/commons/lang3/ClassUtils.java +++ b/src/main/java/org/apache/commons/lang3/ClassUtils.java @@ -1458,10 +1458,8 @@ public class ClassUtils { className.endsWith(";") ? className.length() - 1 : className.length()); - } else { - if (!className.isEmpty()) { - className = reverseAbbreviationMap.get(className.substring(0, 1)); - } + } else if (!className.isEmpty()) { + className = reverseAbbreviationMap.get(className.substring(0, 1)); } final StringBuilder canonicalClassNameBuffer = new StringBuilder(className); for (int i = 0; i < dim; i++) { diff --git a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java index f089394b1..23d216115 100644 --- a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java +++ b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java @@ -372,18 +372,14 @@ public class RandomStringUtils { if (start == 0 && end == 0) { if (chars != null) { end = chars.length; + } else if (!letters && !numbers) { + end = Character.MAX_CODE_POINT; } else { - if (!letters && !numbers) { - end = Character.MAX_CODE_POINT; - } else { - end = 'z' + 1; - start = ' '; - } - } - } else { - if (end <= start) { - throw new IllegalArgumentException("Parameter end (" + end + ") must be greater than start (" + start + ")"); + end = 'z' + 1; + start = ' '; } + } else if (end <= start) { + throw new IllegalArgumentException("Parameter end (" + end + ") must be greater than start (" + start + ")"); } final int zero_digit_ascii = 48; diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index cea878f16..284c20256 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -2946,10 +2946,8 @@ public class StringUtils { if (chFound && CharSequenceUtils.indexOf(searchChars, ch2, 0) < 0) { return i; } - } else { - if (!chFound) { - return i; - } + } else if (!chFound) { + return i; } } return INDEX_NOT_FOUND; @@ -6747,11 +6745,9 @@ public class StringUtils { // see if we need to keep searching for this if (tempIndex == -1) { noMoreMatchesForReplIndex[i] = true; - } else { - if (textIndex == -1 || tempIndex < textIndex) { - textIndex = tempIndex; - replaceIndex = i; - } + } else if (textIndex == -1 || tempIndex < textIndex) { + textIndex = tempIndex; + replaceIndex = i; } } // NOTE: logic mostly below END @@ -6804,11 +6800,9 @@ public class StringUtils { // see if we need to keep searching for this if (tempIndex == -1) { noMoreMatchesForReplIndex[i] = true; - } else { - if (textIndex == -1 || tempIndex < textIndex) { - textIndex = tempIndex; - replaceIndex = i; - } + } else if (textIndex == -1 || tempIndex < textIndex) { + textIndex = tempIndex; + replaceIndex = i; } } // NOTE: logic duplicated above END diff --git a/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java b/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java index ecb9bcc96..8a8580bb5 100644 --- a/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java @@ -420,17 +420,15 @@ public class CompareToBuilder implements Builder { if (lhs.getClass().isArray()) { // factor out array case in order to keep method small enough to be inlined appendArray(lhs, rhs, comparator); + } else // the simple case, not an array, just test the element + if (comparator == null) { + @SuppressWarnings("unchecked") // assume this can be done; if not throw CCE as per Javadoc + final Comparable comparable = (Comparable) lhs; + comparison = comparable.compareTo(rhs); } else { - // the simple case, not an array, just test the element - if (comparator == null) { - @SuppressWarnings("unchecked") // assume this can be done; if not throw CCE as per Javadoc - final Comparable comparable = (Comparable) lhs; - comparison = comparable.compareTo(rhs); - } else { - @SuppressWarnings("unchecked") // assume this can be done; if not throw CCE as per Javadoc - final Comparator comparator2 = (Comparator) comparator; - comparison = comparator2.compare(lhs, rhs); - } + @SuppressWarnings("unchecked") // assume this can be done; if not throw CCE as per Javadoc + final Comparator comparator2 = (Comparator) comparator; + comparison = comparator2.compare(lhs, rhs); } return this; } diff --git a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java index 0911def2f..41b69486e 100644 --- a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java @@ -532,17 +532,15 @@ public class EqualsBuilder implements Builder { try { if (testClass.isArray()) { append(lhs, rhs); + } else //If either class is being excluded, call normal object equals method on lhsClass. + if (bypassReflectionClasses != null + && (bypassReflectionClasses.contains(lhsClass) || bypassReflectionClasses.contains(rhsClass))) { + isEquals = lhs.equals(rhs); } else { - //If either class is being excluded, call normal object equals method on lhsClass. - if (bypassReflectionClasses != null - && (bypassReflectionClasses.contains(lhsClass) || bypassReflectionClasses.contains(rhsClass))) { - isEquals = lhs.equals(rhs); - } else { + reflectionAppend(lhs, rhs, testClass); + while (testClass.getSuperclass() != null && testClass != reflectUpToClass) { + testClass = testClass.getSuperclass(); reflectionAppend(lhs, rhs, testClass); - while (testClass.getSuperclass() != null && testClass != reflectUpToClass) { - testClass = testClass.getSuperclass(); - reflectionAppend(lhs, rhs, testClass); - } } } } catch (final IllegalArgumentException e) { @@ -644,13 +642,11 @@ public class EqualsBuilder implements Builder { // factor out array case in order to keep method small enough // to be inlined appendArray(lhs, rhs); + } else // The simple case, not an array, just test the element + if (testRecursive && !ClassUtils.isPrimitiveOrWrapper(lhsClass)) { + reflectionAppend(lhs, rhs); } else { - // The simple case, not an array, just test the element - if (testRecursive && !ClassUtils.isPrimitiveOrWrapper(lhsClass)) { - reflectionAppend(lhs, rhs); - } else { - isEquals = lhs.equals(rhs); - } + isEquals = lhs.equals(rhs); } return this; } diff --git a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java index 319f70457..b1cc73dde 100644 --- a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java @@ -842,14 +842,12 @@ public class HashCodeBuilder implements Builder { if (object == null) { iTotal = iTotal * iConstant; + } else if (object.getClass().isArray()) { + // factor out array case in order to keep method small enough + // to be inlined + appendArray(object); } else { - if (object.getClass().isArray()) { - // factor out array case in order to keep method small enough - // to be inlined - appendArray(object); - } else { - iTotal = iTotal * iConstant + object.hashCode(); - } + iTotal = iTotal * iConstant + object.hashCode(); } return this; } diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java b/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java index 3497c3646..c2860d2c6 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java @@ -575,12 +575,10 @@ public abstract class ToStringStyle implements Serializable { appendSummary(buffer, fieldName, (Object[]) value); } + } else if (detail) { + appendDetail(buffer, fieldName, value); } else { - if (detail) { - appendDetail(buffer, fieldName, value); - } else { - appendSummary(buffer, fieldName, value); - } + appendSummary(buffer, fieldName, value); } } finally { unregister(value); diff --git a/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java b/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java index 44d16a1f6..bc528847a 100644 --- a/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java +++ b/src/main/java/org/apache/commons/lang3/text/StrSubstitutor.java @@ -773,114 +773,112 @@ public class StrSubstitutor { bufEnd); if (startMatchLen == 0) { pos++; + } else // found variable start marker + if (pos > offset && chars[pos - 1] == escape) { + // escaped + if (preserveEscapes) { + pos++; + continue; + } + buf.deleteCharAt(pos - 1); + chars = buf.buffer; // in case buffer was altered + lengthChange--; + altered = true; + bufEnd--; } else { - // found variable start marker - if (pos > offset && chars[pos - 1] == escape) { - // escaped - if (preserveEscapes) { - pos++; + // find suffix + final int startPos = pos; + pos += startMatchLen; + int endMatchLen = 0; + int nestedVarCount = 0; + while (pos < bufEnd) { + if (substitutionInVariablesEnabled + && (endMatchLen = pfxMatcher.isMatch(chars, + pos, offset, bufEnd)) != 0) { + // found a nested variable start + nestedVarCount++; + pos += endMatchLen; continue; } - buf.deleteCharAt(pos - 1); - chars = buf.buffer; // in case buffer was altered - lengthChange--; - altered = true; - bufEnd--; - } else { - // find suffix - final int startPos = pos; - pos += startMatchLen; - int endMatchLen = 0; - int nestedVarCount = 0; - while (pos < bufEnd) { - if (substitutionInVariablesEnabled - && (endMatchLen = pfxMatcher.isMatch(chars, - pos, offset, bufEnd)) != 0) { - // found a nested variable start - nestedVarCount++; + + endMatchLen = suffMatcher.isMatch(chars, pos, offset, + bufEnd); + if (endMatchLen == 0) { + pos++; + } else { + // found variable end marker + if (nestedVarCount == 0) { + String varNameExpr = new String(chars, startPos + + startMatchLen, pos - startPos + - startMatchLen); + if (substitutionInVariablesEnabled) { + final StrBuilder bufName = new StrBuilder(varNameExpr); + substitute(bufName, 0, bufName.length()); + varNameExpr = bufName.toString(); + } pos += endMatchLen; - continue; - } + final int endPos = pos; - endMatchLen = suffMatcher.isMatch(chars, pos, offset, - bufEnd); - if (endMatchLen == 0) { - pos++; - } else { - // found variable end marker - if (nestedVarCount == 0) { - String varNameExpr = new String(chars, startPos - + startMatchLen, pos - startPos - - startMatchLen); - if (substitutionInVariablesEnabled) { - final StrBuilder bufName = new StrBuilder(varNameExpr); - substitute(bufName, 0, bufName.length()); - varNameExpr = bufName.toString(); - } - pos += endMatchLen; - final int endPos = pos; + String varName = varNameExpr; + String varDefaultValue = null; - String varName = varNameExpr; - String varDefaultValue = null; - - if (valueDelimMatcher != null) { - final char [] varNameExprChars = varNameExpr.toCharArray(); - int valueDelimiterMatchLen = 0; - for (int i = 0; i < varNameExprChars.length; i++) { - // if there's any nested variable when nested variable substitution disabled, then stop resolving name and default value. - if (!substitutionInVariablesEnabled - && pfxMatcher.isMatch(varNameExprChars, i, i, varNameExprChars.length) != 0) { - break; - } - if ((valueDelimiterMatchLen = valueDelimMatcher.isMatch(varNameExprChars, i)) != 0) { - varName = varNameExpr.substring(0, i); - varDefaultValue = varNameExpr.substring(i + valueDelimiterMatchLen); - break; - } + if (valueDelimMatcher != null) { + final char [] varNameExprChars = varNameExpr.toCharArray(); + int valueDelimiterMatchLen = 0; + for (int i = 0; i < varNameExprChars.length; i++) { + // if there's any nested variable when nested variable substitution disabled, then stop resolving name and default value. + if (!substitutionInVariablesEnabled + && pfxMatcher.isMatch(varNameExprChars, i, i, varNameExprChars.length) != 0) { + break; + } + if ((valueDelimiterMatchLen = valueDelimMatcher.isMatch(varNameExprChars, i)) != 0) { + varName = varNameExpr.substring(0, i); + varDefaultValue = varNameExpr.substring(i + valueDelimiterMatchLen); + break; } } - - // on the first call initialize priorVariables - if (priorVariables == null) { - priorVariables = new ArrayList<>(); - priorVariables.add(new String(chars, - offset, length)); - } - - // handle cyclic substitution - checkCyclicSubstitution(varName, priorVariables); - priorVariables.add(varName); - - // resolve the variable - String varValue = resolveVariable(varName, buf, - startPos, endPos); - if (varValue == null) { - varValue = varDefaultValue; - } - if (varValue != null) { - // recursive replace - final int varLen = varValue.length(); - buf.replace(startPos, endPos, varValue); - altered = true; - int change = substitute(buf, startPos, - varLen, priorVariables); - change = change - + varLen - (endPos - startPos); - pos += change; - bufEnd += change; - lengthChange += change; - chars = buf.buffer; // in case buffer was - // altered - } - - // remove variable from the cyclic stack - priorVariables - .remove(priorVariables.size() - 1); - break; } - nestedVarCount--; - pos += endMatchLen; + + // on the first call initialize priorVariables + if (priorVariables == null) { + priorVariables = new ArrayList<>(); + priorVariables.add(new String(chars, + offset, length)); + } + + // handle cyclic substitution + checkCyclicSubstitution(varName, priorVariables); + priorVariables.add(varName); + + // resolve the variable + String varValue = resolveVariable(varName, buf, + startPos, endPos); + if (varValue == null) { + varValue = varDefaultValue; + } + if (varValue != null) { + // recursive replace + final int varLen = varValue.length(); + buf.replace(startPos, endPos, varValue); + altered = true; + int change = substitute(buf, startPos, + varLen, priorVariables); + change = change + + varLen - (endPos - startPos); + pos += change; + bufEnd += change; + lengthChange += change; + chars = buf.buffer; // in case buffer was + // altered + } + + // remove variable from the cyclic stack + priorVariables + .remove(priorVariables.size() - 1); + break; } + nestedVarCount--; + pos += endMatchLen; } } } diff --git a/src/main/java/org/apache/commons/lang3/text/WordUtils.java b/src/main/java/org/apache/commons/lang3/text/WordUtils.java index 99ec9989f..5a43dc777 100644 --- a/src/main/java/org/apache/commons/lang3/text/WordUtils.java +++ b/src/main/java/org/apache/commons/lang3/text/WordUtils.java @@ -315,28 +315,26 @@ public class WordUtils { wrappedLine.append(newLineStr); offset = spaceToWrapAt + 1; + } else // really long word or URL + if (wrapLongWords) { + // wrap really long word one line at a time + wrappedLine.append(str, offset, wrapLength + offset); + wrappedLine.append(newLineStr); + offset += wrapLength; } else { - // really long word or URL - if (wrapLongWords) { - // wrap really long word one line at a time - wrappedLine.append(str, offset, wrapLength + offset); - wrappedLine.append(newLineStr); - offset += wrapLength; - } else { - // do not wrap really long word, just extend beyond limit - matcher = patternToWrapOn.matcher(str.substring(offset + wrapLength)); - if (matcher.find()) { - spaceToWrapAt = matcher.start() + offset + wrapLength; - } + // do not wrap really long word, just extend beyond limit + matcher = patternToWrapOn.matcher(str.substring(offset + wrapLength)); + if (matcher.find()) { + spaceToWrapAt = matcher.start() + offset + wrapLength; + } - if (spaceToWrapAt >= 0) { - wrappedLine.append(str, offset, spaceToWrapAt); - wrappedLine.append(newLineStr); - offset = spaceToWrapAt + 1; - } else { - wrappedLine.append(str, offset, str.length()); - offset = inputLineLength; - } + if (spaceToWrapAt >= 0) { + wrappedLine.append(str, offset, spaceToWrapAt); + wrappedLine.append(newLineStr); + offset = spaceToWrapAt + 1; + } else { + wrappedLine.append(str, offset, str.length()); + offset = inputLineLength; } } } diff --git a/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java b/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java index 6f82af1a2..a57e79f5e 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java @@ -108,10 +108,8 @@ public class NumericEntityEscaper extends CodePointTranslator { if (codepoint < below || codepoint > above) { return false; } - } else { - if (codepoint >= below && codepoint <= above) { - return false; - } + } else if (codepoint >= below && codepoint <= above) { + return false; } out.write("&#"); diff --git a/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java b/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java index 709b30aab..4b1582685 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java @@ -108,10 +108,8 @@ public class UnicodeEscaper extends CodePointTranslator { if (codepoint < below || codepoint > above) { return false; } - } else { - if (codepoint >= below && codepoint <= above) { - return false; - } + } else if (codepoint >= below && codepoint <= above) { + return false; } // TODO: Handle potential + sign per various Unicode escape implementations diff --git a/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java b/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java index 64b04a2d9..60a895e25 100644 --- a/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java @@ -431,35 +431,33 @@ public class DurationFormatUtils { final int count = token.getCount(); if (value instanceof StringBuilder) { buffer.append(value.toString()); - } else { - if (value.equals(y)) { - buffer.append(paddedValue(years, padWithZeros, count)); - lastOutputSeconds = false; - } else if (value.equals(M)) { - buffer.append(paddedValue(months, padWithZeros, count)); - lastOutputSeconds = false; - } else if (value.equals(d)) { - buffer.append(paddedValue(days, padWithZeros, count)); - lastOutputSeconds = false; - } else if (value.equals(H)) { - buffer.append(paddedValue(hours, padWithZeros, count)); - lastOutputSeconds = false; - } else if (value.equals(m)) { - buffer.append(paddedValue(minutes, padWithZeros, count)); - lastOutputSeconds = false; - } else if (value.equals(s)) { - buffer.append(paddedValue(seconds, padWithZeros, count)); - lastOutputSeconds = true; - } else if (value.equals(S)) { - if (lastOutputSeconds) { - // ensure at least 3 digits are displayed even if padding is not selected - final int width = padWithZeros ? Math.max(3, count) : 3; - buffer.append(paddedValue(milliseconds, true, width)); - } else { - buffer.append(paddedValue(milliseconds, padWithZeros, count)); - } - lastOutputSeconds = false; + } else if (value.equals(y)) { + buffer.append(paddedValue(years, padWithZeros, count)); + lastOutputSeconds = false; + } else if (value.equals(M)) { + buffer.append(paddedValue(months, padWithZeros, count)); + lastOutputSeconds = false; + } else if (value.equals(d)) { + buffer.append(paddedValue(days, padWithZeros, count)); + lastOutputSeconds = false; + } else if (value.equals(H)) { + buffer.append(paddedValue(hours, padWithZeros, count)); + lastOutputSeconds = false; + } else if (value.equals(m)) { + buffer.append(paddedValue(minutes, padWithZeros, count)); + lastOutputSeconds = false; + } else if (value.equals(s)) { + buffer.append(paddedValue(seconds, padWithZeros, count)); + lastOutputSeconds = true; + } else if (value.equals(S)) { + if (lastOutputSeconds) { + // ensure at least 3 digits are displayed even if padding is not selected + final int width = padWithZeros ? Math.max(3, count) : 3; + buffer.append(paddedValue(milliseconds, true, width)); + } else { + buffer.append(paddedValue(milliseconds, padWithZeros, count)); } + lastOutputSeconds = false; } } return buffer.toString();