RequestScopedMdc { public static void remove(RequestContext ctx, String key) { requireNonNull(ctx, "ctx"); requireNonNull(key, "key"); synchronized (ctx) { final Object2ObjectMap<String, String> oldMap = getMap(ctx); if (!oldMap.containsKey(key)) { return; } final Object2ObjectMap<String, String> newMap; if (oldMap.size() == 1) { newMap = Object2ObjectMaps.emptyMap(); } else { final Object2ObjectOpenHashMap<String, String> tmp = new Object2ObjectOpenHashMap<>(oldMap); tmp.remove(key); newMap = Object2ObjectMaps.unmodifiable(tmp); } ctx.setAttr(MAP, 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 remove() { final ServiceRequestContext ctx = newContext(); RequestScopedMdc.remove(ctx, "foo"); assertThat(RequestScopedMdc.getAll(ctx)).isEmpty(); RequestScopedMdc.put(ctx, "foo", "1"); assertThat(RequestScopedMdc.getAll(ctx)).containsOnly(Maps.immutableEntry("foo", "1")); RequestScopedMdc.remove(ctx, "bar"); assertThat(RequestScopedMdc.getAll(ctx)).containsOnly(Maps.immutableEntry("foo", "1")); RequestScopedMdc.remove(ctx, "foo"); assertThat(RequestScopedMdc.getAll(ctx)).isEmpty(); RequestScopedMdc.put(ctx, "foo", "1"); RequestScopedMdc.put(ctx, "bar", "2"); RequestScopedMdc.remove(ctx, "foo"); assertThat(RequestScopedMdc.getAll(ctx)).containsOnly(Maps.immutableEntry("bar", "2")); }