Combine nested 'if' statement in 'else' block to 'else if'.

This commit is contained in:
Gary Gregory 2021-02-22 11:12:20 -05:00
parent 7e5be1cf84
commit 91928eed34
12 changed files with 187 additions and 219 deletions

View File

@ -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++) {

View File

@ -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;

View File

@ -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

View File

@ -420,17 +420,15 @@ public class CompareToBuilder implements Builder<Integer> {
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<Object> comparable = (Comparable<Object>) 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<Object> comparable = (Comparable<Object>) lhs;
comparison = comparable.compareTo(rhs);
} else {
@SuppressWarnings("unchecked") // assume this can be done; if not throw CCE as per Javadoc
final Comparator<Object> comparator2 = (Comparator<Object>) comparator;
comparison = comparator2.compare(lhs, rhs);
}
@SuppressWarnings("unchecked") // assume this can be done; if not throw CCE as per Javadoc
final Comparator<Object> comparator2 = (Comparator<Object>) comparator;
comparison = comparator2.compare(lhs, rhs);
}
return this;
}

View File

@ -532,17 +532,15 @@ public class EqualsBuilder implements Builder<Boolean> {
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<Boolean> {
// 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;
}

View File

@ -842,14 +842,12 @@ public class HashCodeBuilder implements Builder<Integer> {
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;
}

View File

@ -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);

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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("&#");

View File

@ -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

View File

@ -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();