From 5beaae69c81daccb5885124ca64e68d3e41e8efe Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Fri, 11 Oct 2024 00:26:20 +0700 Subject: [PATCH 1/2] Reduce `else if` condition --- .../util/FastByteArrayOutputStream.java | 35 ++++++++++--------- .../annotation/ModelMethodProcessor.java | 5 +-- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java index 7e7f1430c7f0..837a8339efc7 100644 --- a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java +++ b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -508,30 +508,31 @@ public void updateMessageDigest(MessageDigest messageDigest, int len) { // This stream doesn't have any data in it... return; } - else if (len == 0) { + + if (len == 0) { return; } - else if (len < 0) { + + if (len < 0) { throw new IllegalArgumentException("len must be 0 or greater: " + len); } + + if (this.nextIndexInCurrentBuffer < this.currentBufferLength) { + int bytesToCopy = Math.min(len, this.currentBufferLength - this.nextIndexInCurrentBuffer); + messageDigest.update(this.currentBuffer, this.nextIndexInCurrentBuffer, bytesToCopy); + this.nextIndexInCurrentBuffer += bytesToCopy; + updateMessageDigest(messageDigest, len - bytesToCopy); + } else { - if (this.nextIndexInCurrentBuffer < this.currentBufferLength) { - int bytesToCopy = Math.min(len, this.currentBufferLength - this.nextIndexInCurrentBuffer); - messageDigest.update(this.currentBuffer, this.nextIndexInCurrentBuffer, bytesToCopy); - this.nextIndexInCurrentBuffer += bytesToCopy; - updateMessageDigest(messageDigest, len - bytesToCopy); + if (this.buffersIterator.hasNext()) { + this.currentBuffer = this.buffersIterator.next(); + updateCurrentBufferLength(); + this.nextIndexInCurrentBuffer = 0; } else { - if (this.buffersIterator.hasNext()) { - this.currentBuffer = this.buffersIterator.next(); - updateCurrentBufferLength(); - this.nextIndexInCurrentBuffer = 0; - } - else { - this.currentBuffer = null; - } - updateMessageDigest(messageDigest, len); + this.currentBuffer = null; } + updateMessageDigest(messageDigest, len); } } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelMethodProcessor.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelMethodProcessor.java index 2aef369c69a6..a68931b7cbd1 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelMethodProcessor.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelMethodProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,7 +65,8 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu if (returnValue == null) { return; } - else if (returnValue instanceof Model model) { + + if (returnValue instanceof Model model) { mavContainer.addAllAttributes(model.asMap()); } else { From acfbe02bd28717b587f27cb6da7f9d7aa258a7ca Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Fri, 11 Oct 2024 00:27:51 +0700 Subject: [PATCH 2/2] Polish --- .../springframework/util/MultiToSingleValueMapAdapter.java | 4 ++-- .../test/web/servlet/result/PrintingResultHandler.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/MultiToSingleValueMapAdapter.java b/spring-core/src/main/java/org/springframework/util/MultiToSingleValueMapAdapter.java index bc56680d7a93..1f279913e991 100644 --- a/spring-core/src/main/java/org/springframework/util/MultiToSingleValueMapAdapter.java +++ b/spring-core/src/main/java/org/springframework/util/MultiToSingleValueMapAdapter.java @@ -139,11 +139,11 @@ public Collection values() { Collection values = this.values; if (values == null) { Collection> targetValues = this.targetMap.values(); - values = new AbstractCollection() { + values = new AbstractCollection<>() { @Override public Iterator iterator() { Iterator> targetIterator = targetValues.iterator(); - return new Iterator() { + return new Iterator<>() { @Override public boolean hasNext() { return targetIterator.hasNext(); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java index 50baf1cd9634..61952f737389 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java @@ -207,7 +207,7 @@ protected void printResolvedException(@Nullable Exception resolvedException) thr protected void printModelAndView(@Nullable ModelAndView mav) throws Exception { this.printer.printValue("View name", (mav != null) ? mav.getViewName() : null); this.printer.printValue("View", (mav != null) ? mav.getView() : null); - if (mav == null || mav.getModel().size() == 0) { + if (mav == null || mav.getModel().isEmpty()) { this.printer.printValue("Model", null); } else {