Skip to content

Commit a802e31

Browse files
committed
Log errors instead of throwing errors for duplicate keys in ImmutableMap
1 parent 2800f91 commit a802e31

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Log error instead of throwing error for duplicate keys in ImmutableMap"
6+
}

utils/src/main/java/software/amazon/awssdk/utils/ImmutableMap.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
@SdkProtectedApi
5959
public final class ImmutableMap<K, V> implements Map<K, V> {
6060

61+
private static final Logger log = Logger.loggerFor(ImmutableMap.class);
6162
private static final String UNMODIFIABLE_MESSAGE = "This is an immutable map.";
62-
private static final String DUPLICATED_KEY_MESSAGE = "Duplicate keys are provided.";
6363

6464
private final Map<K, V> map;
6565

@@ -199,7 +199,9 @@ public static <K, V> ImmutableMap<K, V> of(K k0, V v0, K k1, V v1,
199199
private static <K, V> void putAndWarnDuplicateKeys(Map<K, V> map, K key,
200200
V value) {
201201
if (map.containsKey(key)) {
202-
throw new IllegalArgumentException(DUPLICATED_KEY_MESSAGE);
202+
log.error(() -> String.format("Duplicate keys are provided for [%s]. The first value [%s] will be kept, and the "
203+
+ "second value [%s] will be ignored.", key, map.get(key), value));
204+
return;
203205
}
204206
map.put(key, value);
205207
}
@@ -295,9 +297,8 @@ public Builder() {
295297
}
296298

297299
/**
298-
* Add a key-value pair into the built map. The method will throw
299-
* IllegalArgumentException immediately when duplicate keys are
300-
* provided.
300+
* Add a key-value pair into the built map. If duplicate keys are provided, the first value will be kept and the second
301+
* value will be ignored, and the SDK will log an error.
301302
*
302303
* @return Returns a reference to this object so that method calls can
303304
* be chained together.

utils/src/test/java/software/amazon/awssdk/utils/ImmutableMapTest.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,13 @@ public void testOfBuilder() {
7171
}
7272

7373
@Test
74-
public void testErrorOnDuplicateKeys() {
75-
try {
76-
Map<Integer, String> builtMap = new ImmutableMap.Builder<Integer, String>()
77-
.put(1, "one")
78-
.put(1, "two")
79-
.build();
80-
fail("IllegalArgumentException expected.");
81-
} catch (IllegalArgumentException iae) {
82-
// Ignored or expected.
83-
} catch (Exception e) {
84-
fail("IllegalArgumentException expected.");
85-
}
74+
public void putDuplicateKeys_keepsFirstOneAndDoesNotThrowError() {
75+
Map<Integer, String> builtMap = new ImmutableMap.Builder<Integer, String>()
76+
.put(1, "one")
77+
.put(1, "two")
78+
.build();
79+
80+
assertEquals("one", builtMap.get(1));
8681
}
8782

8883
@Test

0 commit comments

Comments
 (0)