Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
midumitrescu committed May 28, 2020
1 parent d706899 commit 6db35fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Expand Up @@ -483,8 +483,7 @@ public MultiValueMapAdapter(Map<K, List<V>> map) {
@Override
@Nullable
public V getFirst(K key) {
List<V> values = this.map.get(key);
return (values != null ? values.get(0) : null);
return firstElement(this.map.get(key));
}

@Override
Expand Down Expand Up @@ -521,7 +520,7 @@ public void setAll(Map<K, V> values) {
@Override
public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.map.size());
this.map.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
this.map.forEach((key, value) -> singleValueMap.put(key, firstElement(value)));
return singleValueMap;
}

Expand Down Expand Up @@ -603,5 +602,4 @@ public String toString() {
return this.map.toString();
}
}

}
Expand Up @@ -17,6 +17,7 @@
package org.springframework.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -28,6 +29,8 @@
import java.util.Properties;
import java.util.Set;

import org.assertj.core.util.Maps;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -210,6 +213,21 @@ void hasUniqueObject() {
assertThat(CollectionUtils.hasUniqueObject(list)).isFalse();
}

@Test
void noArrayIndexOutOfBoundsException() {
HashMap<String, List<Object>> mostlyEmptyMap = new HashMap<>();
mostlyEmptyMap.put("test", new ArrayList<>());


MultiValueMap<String, Object> multiValueMap = CollectionUtils.toMultiValueMap(mostlyEmptyMap);
Assertions.assertAll(
() -> assertThat(multiValueMap.getFirst(null)).isNull(),
() -> assertThat(multiValueMap.getFirst("test")).isNull(),
() -> assertThat(multiValueMap.toSingleValueMap().get(null)).isNull(),
() -> assertThat(multiValueMap.toSingleValueMap().get("test")).isNull()
);
}


private static final class Instance {

Expand Down

0 comments on commit 6db35fd

Please sign in to comment.