Skip to content

Commit 0a1a825

Browse files
committed
Optimize MapUtils slightly
1 parent 331ab01 commit 0a1a825

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

smithy-utils/src/main/java/software/amazon/smithy/utils/MapUtils.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ public static <K, V> Map<K, V> of() {
7676
* @throws NullPointerException if the key or the value is {@code null}
7777
*/
7878
public static <K, V> Map<K, V> of(K k1, V v1) {
79-
Map<K, V> result = new HashMap<>(1);
80-
result.put(k1, v1);
81-
return Collections.unmodifiableMap(result);
79+
return Collections.singletonMap(k1, v1);
8280
}
8381

8482
/**
@@ -399,11 +397,17 @@ public static <K, V> Map.Entry<K, V> entry(K key, V value) {
399397
@SafeVarargs
400398
@SuppressWarnings("varargs")
401399
public static <K, V> Map<K, V> ofEntries(Map.Entry<? extends K, ? extends V>... entries) {
402-
Map<K, V> result = new HashMap<>(entries.length);
403-
for (Map.Entry<? extends K, ? extends V> entry : entries) {
404-
result.put(entry.getKey(), entry.getValue());
400+
if (entries.length == 0) {
401+
return MapUtils.of();
402+
} else if (entries.length == 1) {
403+
return MapUtils.of(entries[0].getKey(), entries[0].getValue());
404+
} else {
405+
Map<K, V> result = new HashMap<>(entries.length);
406+
for (Map.Entry<? extends K, ? extends V> entry : entries) {
407+
result.put(entry.getKey(), entry.getValue());
408+
}
409+
return Collections.unmodifiableMap(result);
405410
}
406-
return Collections.unmodifiableMap(result);
407411
}
408412

409413
/**

smithy-utils/src/test/java/software/amazon/smithy/utils/MapUtilsTest.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import static java.util.function.Function.identity;
1919
import static org.hamcrest.MatcherAssert.assertThat;
2020
import static org.hamcrest.Matchers.anEmptyMap;
21+
import static org.hamcrest.Matchers.empty;
2122
import static org.hamcrest.Matchers.equalTo;
2223
import static org.hamcrest.Matchers.hasEntry;
24+
import static org.hamcrest.Matchers.hasSize;
2325
import static org.junit.jupiter.api.Assertions.assertEquals;
2426

2527
import java.util.Collections;
@@ -182,12 +184,29 @@ public void suppliesEntry() {
182184
}
183185

184186
@Test
185-
public void buildsFromEntries() {
187+
public void buildsFromTwoEntries() {
186188
Map<String, String> map = MapUtils.ofEntries(
187189
MapUtils.entry("1", "A"),
188190
MapUtils.entry("2", "B"));
191+
189192
assertThat(map, hasEntry("1", "A"));
190193
assertThat(map, hasEntry("2", "B"));
194+
assertThat(map.entrySet(), hasSize(2));
195+
}
196+
197+
@Test
198+
public void buildsFromEntriesSingleEntry() {
199+
Map<String, String> map = MapUtils.ofEntries(MapUtils.entry("1", "A"));
200+
201+
assertThat(map, hasEntry("1", "A"));
202+
assertThat(map.entrySet(), hasSize(1));
203+
}
204+
205+
@Test
206+
public void buildsFromEntriesEmpty() {
207+
Map<String, String> map = MapUtils.ofEntries();
208+
209+
assertThat(map.entrySet(), empty());
191210
}
192211

193212
@Test

0 commit comments

Comments
 (0)