RequestScopedMdc { public static void putAll(RequestContext ctx, Map<String, String> map) { requireNonNull(ctx, "ctx"); requireNonNull(map, "map"); if (map.isEmpty()) { return; } synchronized (ctx) { final Object2ObjectMap<String, String> oldMap = getMap(ctx); final Object2ObjectMap<String, String> newMap; if (oldMap.isEmpty()) { newMap = new Object2ObjectOpenHashMap<>(map); } else { newMap = new Object2ObjectOpenHashMap<>(oldMap.size() + map.size()); newMap.putAll(oldMap); newMap.putAll(map); } ctx.setAttr(MAP, Object2ObjectMaps.unmodifiable(newMap)); } } private  RequestScopedMdc(); @Nullable static String get(RequestContext ctx, String key); static Map<String, String> getAll(RequestContext ctx); static void put(RequestContext ctx, String key, @Nullable String value); static void putAll(RequestContext ctx, Map<String, String> map); static void copy(RequestContext ctx, String key); static void copyAll(RequestContext ctx); static void remove(RequestContext ctx, String key); static void clear(RequestContext ctx);  }
@Test void putAll() { final ServiceRequestContext ctx = newContext(); RequestScopedMdc.putAll(ctx, ImmutableMap.of()); assertThat(RequestScopedMdc.getAll(ctx)).isEmpty(); RequestScopedMdc.putAll(ctx, ImmutableMap.of("foo", "1", "bar", "2")); assertThat(RequestScopedMdc.getAll(ctx)).containsOnly( Maps.immutableEntry("foo", "1"), Maps.immutableEntry("bar", "2")); RequestScopedMdc.putAll(ctx, ImmutableMap.of("bar", "3", "baz", "4")); assertThat(RequestScopedMdc.getAll(ctx)).containsOnly( Maps.immutableEntry("foo", "1"), Maps.immutableEntry("bar", "3"), Maps.immutableEntry("baz", "4")); }