Skip to content

Commit 2494ecb

Browse files
polyglot-ksbrannen
authored andcommitted
Simplify utility implementations in spring-core
Closes gh-33903
1 parent 241b8b4 commit 2494ecb

File tree

5 files changed

+29
-45
lines changed

5 files changed

+29
-45
lines changed

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

+14-18
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

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

+6-8
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
}

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,7 @@ 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("'")));
249247
}
250248

251249
protected String unquote(String s) {

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

+2-4
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());

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

+6-12
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);

0 commit comments

Comments
 (0)