Skip to content

Commit a02861f

Browse files
quaffjhoeller
authored andcommitted
Insist on using CollectionUtils.isEmpty() and StringUtils.hasLength()
search `(\w+) != null && !(\1).isEmpty\(\)`
1 parent 8137cc9 commit a02861f

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public MultiValueMapAdapter(Map<K, List<V>> targetMap) {
5959
@Nullable
6060
public V getFirst(K key) {
6161
List<V> values = this.targetMap.get(key);
62-
return (values != null && !values.isEmpty() ? values.get(0) : null);
62+
return (!CollectionUtils.isEmpty(values) ? values.get(0) : null);
6363
}
6464

6565
@Override
@@ -95,7 +95,7 @@ public void setAll(Map<K, V> values) {
9595
public Map<K, V> toSingleValueMap() {
9696
Map<K, V> singleValueMap = CollectionUtils.newLinkedHashMap(this.targetMap.size());
9797
this.targetMap.forEach((key, values) -> {
98-
if (values != null && !values.isEmpty()) {
98+
if (!CollectionUtils.isEmpty(values)) {
9999
singleValueMap.put(key, values.get(0));
100100
}
101101
});

Diff for: spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.stereotype.Controller;
3737
import org.springframework.test.web.reactive.server.WebTestClient;
3838
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
39+
import org.springframework.util.CollectionUtils;
3940
import org.springframework.web.bind.annotation.PostMapping;
4041
import org.springframework.web.bind.annotation.PutMapping;
4142
import org.springframework.web.bind.annotation.RequestParam;
@@ -271,7 +272,7 @@ public String processMultipartFileArray(@RequestParam(required = false) Multipar
271272
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
272273
@RequestPart(required = false) Map<String, String> json) throws IOException {
273274

274-
if (file != null && !file.isEmpty()) {
275+
if (!CollectionUtils.isEmpty(file)) {
275276
byte[] content = file.get(0).getBytes();
276277
assertThat(file.get(1).getBytes()).isEqualTo(content);
277278
}

Diff for: spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.stereotype.Controller;
4040
import org.springframework.test.web.servlet.MockMvc;
4141
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
42+
import org.springframework.util.CollectionUtils;
4243
import org.springframework.validation.BindingResult;
4344
import org.springframework.web.bind.annotation.PostMapping;
4445
import org.springframework.web.bind.annotation.PutMapping;
@@ -271,7 +272,7 @@ public String processMultipartFileArray(@RequestParam(required = false) Multipar
271272
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
272273
@RequestPart(required = false) Map<String, String> json) throws IOException {
273274

274-
if (file != null && !file.isEmpty()) {
275+
if (!CollectionUtils.isEmpty(file)) {
275276
byte[] content = file.get(0).getBytes();
276277
assertThat(file.get(1).getBytes()).isEqualTo(content);
277278
}

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractFlashMapManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public final void saveOutputFlashMap(FlashMap flashMap, HttpServletRequest reque
220220

221221
@Nullable
222222
private String decodeAndNormalizePath(@Nullable String path, HttpServletRequest request) {
223-
if (path != null && !path.isEmpty()) {
223+
if (StringUtils.hasLength(path)) {
224224
path = getUrlPathHelper().decodeRequestString(request, path);
225225
if (path.charAt(0) != '/') {
226226
String requestUri = getUrlPathHelper().getRequestUri(request);

0 commit comments

Comments
 (0)