/** * Puts all given context map entries into the current thread's * context map. * * <p>If the current thread does not have a context map it is * created as a side effect.</p> * @param m The map. * @since 2.7 */ public static void putAll(final Map<String, String> m) { if (contextMap instanceof ThreadContextMap2) { ((ThreadContextMap2) contextMap).putAll(m); } else if (contextMap instanceof DefaultThreadContextMap) { ((DefaultThreadContextMap) contextMap).putAll(m); } else { for (final Map.Entry<String, String> entry: m.entrySet()) { contextMap.put(entry.getKey(), entry.getValue()); } } }
@Test public void testPutAll() { final DefaultThreadContextMap map = new DefaultThreadContextMap(true); assertTrue(map.isEmpty()); assertFalse(map.containsKey("key")); final int mapSize = 10; final Map<String, String> newMap = new HashMap<>(mapSize); for (int i = 1; i <= mapSize; i++) { newMap.put("key" + i, "value" + i); } map.putAll(newMap); assertFalse(map.isEmpty()); for (int i = 1; i <= mapSize; i++) { assertTrue(map.containsKey("key" + i)); assertEquals("value" + i, map.get("key" + i)); } }
/** * Puts all given context map entries into the current thread's * context map. * * <p>If the current thread does not have a context map it is * created as a side effect.</p> * @param m The map. * @since 2.7 */ public static void putAll(final Map<String, String> m) { if (contextMap instanceof ThreadContextMap2) { ((ThreadContextMap2) contextMap).putAll(m); } else if (contextMap instanceof DefaultThreadContextMap) { ((DefaultThreadContextMap) contextMap).putAll(m); } else { for (final Map.Entry<String, String> entry: m.entrySet()) { contextMap.put(entry.getKey(), entry.getValue()); } } }