From d777c73f78ac7435fc080743e7027509b18f91d6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 7 May 2019 14:02:25 +0200 Subject: [PATCH] Consistent handling of empty List entries in LinkedMultiValueMap Closes gh-22912 --- .../util/LinkedMultiValueMap.java | 21 +++++--- .../util/LinkedMultiValueMapTests.java | 49 ++++++++++++++----- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java index 0bc7655ab48..bbb34c35728 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -34,6 +34,8 @@ import java.util.Set; * @author Arjen Poutsma * @author Juergen Hoeller * @since 3.0 + * @param the key type + * @param the value element type */ public class LinkedMultiValueMap implements MultiValueMap, Serializable, Cloneable { @@ -73,6 +75,12 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa // MultiValueMap implementation + @Override + public V getFirst(K key) { + List values = this.targetMap.get(key); + return (values != null && !values.isEmpty() ? values.get(0) : null); + } + @Override public void add(K key, V value) { List values = this.targetMap.get(key); @@ -83,12 +91,6 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa values.add(value); } - @Override - public V getFirst(K key) { - List values = this.targetMap.get(key); - return (values != null ? values.get(0) : null); - } - @Override public void set(K key, V value) { List values = new LinkedList(); @@ -107,7 +109,10 @@ public class LinkedMultiValueMap implements MultiValueMap, Serializa public Map toSingleValueMap() { LinkedHashMap singleValueMap = new LinkedHashMap(this.targetMap.size()); for (Entry> entry : this.targetMap.entrySet()) { - singleValueMap.put(entry.getKey(), entry.getValue().get(0)); + List values = entry.getValue(); + if (values != null && !values.isEmpty()) { + singleValueMap.put(entry.getKey(), values.get(0)); + } } return singleValueMap; } diff --git a/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java b/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java index 402a7886a70..e0fe8b92e0d 100644 --- a/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java +++ b/spring-core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -28,10 +28,11 @@ import static org.junit.Assert.*; /** * @author Arjen Poutsma + * @author Juergen Hoeller */ public class LinkedMultiValueMapTests { - private final LinkedMultiValueMap map = new LinkedMultiValueMap(); + private final LinkedMultiValueMap map = new LinkedMultiValueMap<>(); @Test @@ -39,15 +40,23 @@ public class LinkedMultiValueMapTests { map.add("key", "value1"); map.add("key", "value2"); assertEquals(1, map.size()); - List expected = new ArrayList(2); + List expected = new ArrayList<>(2); expected.add("value1"); expected.add("value2"); assertEquals(expected, map.get("key")); } + @Test + public void set() { + map.set("key", "value1"); + map.set("key", "value2"); + assertEquals(1, map.size()); + assertEquals(Collections.singletonList("value2"), map.get("key")); + } + @Test public void getFirst() { - List values = new ArrayList(2); + List values = new ArrayList<>(2); values.add("value1"); values.add("value2"); map.put("key", values); @@ -56,22 +65,40 @@ public class LinkedMultiValueMapTests { } @Test - public void set() { - map.set("key", "value1"); - map.set("key", "value2"); - assertEquals(1, map.size()); - assertEquals(Collections.singletonList("value2"), map.get("key")); + public void getFirstWithEmptyList() { + map.put("key", Collections.emptyList()); + assertNull(map.getFirst("key")); + assertNull(map.getFirst("other")); + } + + @Test + public void toSingleValueMap() { + List values = new ArrayList<>(2); + values.add("value1"); + values.add("value2"); + map.put("key", values); + Map svm = map.toSingleValueMap(); + assertEquals(1, svm.size()); + assertEquals("value1", svm.get("key")); + } + + @Test + public void toSingleValueMapWithEmptyList() { + map.put("key", Collections.emptyList()); + Map svm = map.toSingleValueMap(); + assertEquals(0, svm.size()); + assertNull(svm.get("key")); } @Test public void equals() { map.set("key1", "value1"); assertEquals(map, map); - MultiValueMap o1 = new LinkedMultiValueMap(); + MultiValueMap o1 = new LinkedMultiValueMap<>(); o1.set("key1", "value1"); assertEquals(map, o1); assertEquals(o1, map); - Map> o2 = new HashMap>(); + Map> o2 = new HashMap<>(); o2.put("key1", Collections.singletonList("value1")); assertEquals(map, o2); assertEquals(o2, map);