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 9ce1fb6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Expand Up @@ -482,9 +482,8 @@ 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);
public V getFirst(@Nullable K key) {
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();
}
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.Properties;
import java.util.Set;

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 +211,20 @@ 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 9ce1fb6

Please sign in to comment.