Skip to content

Commit fae73d4

Browse files
committed
refactor: Simplify Util code in Spring-core directory
- enhancement by adding early return - delete the unnecessary 'else'
1 parent 7f8102a commit fae73d4

8 files changed

+40
-61
lines changed

spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ public void write(int datum) throws IOException {
9898
if (this.closed) {
9999
throw new IOException("Stream closed");
100100
}
101-
else {
102-
if (this.buffers.peekLast() == null || this.buffers.getLast().length == this.index) {
103-
addBuffer(1);
104-
}
105-
// store the byte
106-
this.buffers.getLast()[this.index++] = (byte) datum;
101+
if (this.buffers.peekLast() == null || this.buffers.getLast().length == this.index) {
102+
addBuffer(1);
107103
}
104+
// store the byte
105+
this.buffers.getLast()[this.index++] = (byte) datum;
108106
}
109107

110108
@Override
@@ -384,22 +382,20 @@ public int read() {
384382
// This stream doesn't have any data in it...
385383
return -1;
386384
}
385+
if (this.nextIndexInCurrentBuffer < this.currentBufferLength) {
386+
this.totalBytesRead++;
387+
return this.currentBuffer[this.nextIndexInCurrentBuffer++] & 0xFF;
388+
}
387389
else {
388-
if (this.nextIndexInCurrentBuffer < this.currentBufferLength) {
389-
this.totalBytesRead++;
390-
return this.currentBuffer[this.nextIndexInCurrentBuffer++] & 0xFF;
390+
if (this.buffersIterator.hasNext()) {
391+
this.currentBuffer = this.buffersIterator.next();
392+
updateCurrentBufferLength();
393+
this.nextIndexInCurrentBuffer = 0;
391394
}
392395
else {
393-
if (this.buffersIterator.hasNext()) {
394-
this.currentBuffer = this.buffersIterator.next();
395-
updateCurrentBufferLength();
396-
this.nextIndexInCurrentBuffer = 0;
397-
}
398-
else {
399-
this.currentBuffer = null;
400-
}
401-
return read();
396+
this.currentBuffer = null;
402397
}
398+
return read();
403399
}
404400
}
405401

@@ -504,13 +500,10 @@ public void updateMessageDigest(MessageDigest messageDigest) {
504500
*/
505501
@Override
506502
public void updateMessageDigest(MessageDigest messageDigest, int len) {
507-
if (this.currentBuffer == null) {
503+
if (this.currentBuffer == null || len == 0) {
508504
// This stream doesn't have any data in it...
509505
return;
510506
}
511-
else if (len == 0) {
512-
return;
513-
}
514507
else if (len < 0) {
515508
throw new IllegalArgumentException("len must be 0 or greater: " + len);
516509
}

spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ public boolean containsKey(Object key) {
119119
}
120120
@Override
121121
protected boolean removeEldestEntry(Map.Entry<String, V> eldest) {
122-
boolean doRemove = LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest);
123-
if (doRemove) {
122+
if (LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest)) {
124123
removeCaseInsensitiveKey(eldest.getKey());
125124
}
126-
return doRemove;
125+
return false;
127126
}
128127
};
129128
this.caseInsensitiveKeys = CollectionUtils.newHashMap(expectedSize);

spring-core/src/main/java/org/springframework/util/MethodInvoker.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,12 @@ protected Method findMatchingMethod() {
226226
Method matchingMethod = null;
227227

228228
for (Method candidate : candidates) {
229-
if (candidate.getName().equals(targetMethod)) {
230-
if (candidate.getParameterCount() == argCount) {
231-
Class<?>[] paramTypes = candidate.getParameterTypes();
232-
int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments);
233-
if (typeDiffWeight < minTypeDiffWeight) {
234-
minTypeDiffWeight = typeDiffWeight;
235-
matchingMethod = candidate;
236-
}
229+
if (candidate.getName().equals(targetMethod) && candidate.getParameterCount() == argCount) {
230+
Class<?>[] paramTypes = candidate.getParameterTypes();
231+
int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments);
232+
if (typeDiffWeight < minTypeDiffWeight) {
233+
minTypeDiffWeight = typeDiffWeight;
234+
matchingMethod = candidate;
237235
}
238236
}
239237
}

spring-core/src/main/java/org/springframework/util/MimeType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,8 @@ private boolean isQuotedString(String s) {
243243
if (s.length() < 2) {
244244
return false;
245245
}
246-
else {
247-
return ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'")));
248-
}
246+
return ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'")));
247+
249248
}
250249

251250
protected String unquote(String s) {

spring-core/src/main/java/org/springframework/util/NumberUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,8 @@ else if (BigInteger.class == targetClass) {
110110
// do not lose precision - use BigDecimal's own conversion
111111
return (T) bigDecimal.toBigInteger();
112112
}
113-
else {
114-
// original value is not a Big* number - use standard long conversion
115-
return (T) BigInteger.valueOf(number.longValue());
116-
}
113+
// original value is not a Big* number - use standard long conversion
114+
return (T) BigInteger.valueOf(number.longValue());
117115
}
118116
else if (Float.class == targetClass) {
119117
return (T) Float.valueOf(number.floatValue());

spring-core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,7 @@ public static String nullSafeToString(@Nullable byte[] array) {
702702
if (array == null) {
703703
return NULL_STRING;
704704
}
705-
int length = array.length;
706-
if (length == 0) {
705+
if (array.length == 0) {
707706
return EMPTY_ARRAY;
708707
}
709708
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
@@ -726,8 +725,7 @@ public static String nullSafeToString(@Nullable char[] array) {
726725
if (array == null) {
727726
return NULL_STRING;
728727
}
729-
int length = array.length;
730-
if (length == 0) {
728+
if (array.length == 0) {
731729
return EMPTY_ARRAY;
732730
}
733731
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
@@ -750,8 +748,7 @@ public static String nullSafeToString(@Nullable double[] array) {
750748
if (array == null) {
751749
return NULL_STRING;
752750
}
753-
int length = array.length;
754-
if (length == 0) {
751+
if (array.length == 0) {
755752
return EMPTY_ARRAY;
756753
}
757754
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
@@ -774,8 +771,7 @@ public static String nullSafeToString(@Nullable float[] array) {
774771
if (array == null) {
775772
return NULL_STRING;
776773
}
777-
int length = array.length;
778-
if (length == 0) {
774+
if (array.length == 0) {
779775
return EMPTY_ARRAY;
780776
}
781777
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
@@ -798,8 +794,7 @@ public static String nullSafeToString(@Nullable int[] array) {
798794
if (array == null) {
799795
return NULL_STRING;
800796
}
801-
int length = array.length;
802-
if (length == 0) {
797+
if (array.length == 0) {
803798
return EMPTY_ARRAY;
804799
}
805800
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
@@ -846,8 +841,7 @@ public static String nullSafeToString(@Nullable short[] array) {
846841
if (array == null) {
847842
return NULL_STRING;
848843
}
849-
int length = array.length;
850-
if (length == 0) {
844+
if (array.length == 0) {
851845
return EMPTY_ARRAY;
852846
}
853847
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);

spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public static boolean simpleMatch(@Nullable String pattern, @Nullable String str
8484
* @return whether the String matches any of the given patterns
8585
*/
8686
public static boolean simpleMatch(@Nullable String[] patterns, @Nullable String str) {
87-
if (patterns != null) {
88-
for (String pattern : patterns) {
89-
if (simpleMatch(pattern, str)) {
90-
return true;
91-
}
87+
if (patterns == null) {
88+
return false;
89+
}
90+
for (String pattern : patterns) {
91+
if (simpleMatch(pattern, str)) {
92+
return true;
9293
}
9394
}
94-
return false;
9595
}
9696

9797
}

spring-core/src/main/java/org/springframework/util/PlaceholderParser.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ final class PlaceholderParser {
104104
if (simplePrefixForSuffix != null && this.prefix.endsWith(simplePrefixForSuffix)) {
105105
this.simplePrefix = simplePrefixForSuffix;
106106
}
107-
else {
108-
this.simplePrefix = this.prefix;
109-
}
107+
this.simplePrefix = this.prefix;
110108
this.separator = separator;
111109
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
112110
this.escape = escape;

0 commit comments

Comments
 (0)