import foo.*; import java.util.Map; import java.util.HashMap; import java.util.function.Function; class MapUpdateInlining { void testKey(Map map) { System.out.println(map.computeIfAbsent("foo", k -> k.equals("blahblah") ? "bar" : "baz").trim()); } void testValue(Map map) { System.out.println(map.computeIfAbsent("foo", k -> null).trim()); System.out.println(map.computeIfAbsent("foo", k -> "bar").trim()); } void testNullable(Map map) { System.out.println(map.computeIfAbsent("foo", k -> null).trim()); System.out.println(map.computeIfAbsent("foo", k -> "bar").trim()); } void testPresent(Map map, String key) { System.out.println(map.computeIfPresent(key, (k, v) -> v+"xyz").trim()); String res1 = map.computeIfPresent("foo", (k, v) -> v == null ? "oops" : k); String res = map.computeIfPresent(key, (k, v) -> k.isEmpty() ? "foo" : "bar"); if(res != null && res.equals("x")) { System.out.println("impossible"); } } void testCompute(Map map, String key) { System.out.println(map.compute(key, (k, v) -> v+"xyz").trim()); String res = map.compute("foo", (k, v) -> v == null ? "foo" : "bar"); if(res == null) { System.out.println("impossible"); } if(res.equals("x")) { System.out.println("impossible"); } } void testMerge(Map map) { String res = map.merge("foo", "bar", (v1, v2) -> v1.equals("x") || v2.equals("y") ? "oops" : "argh"); if(res.equals("bar")) { System.out.println("possible"); } if(res.equals("oops")) { System.out.println("possible"); } if(res.equals("argh")) { System.out.println("possible"); } if(res.equals("qux")) { System.out.println("impossible"); } } void testMerge2(Map map, String key, @Nullable String val) { if(map.merge(key, val, String::concat) == null) { System.out.println("impossible"); } } void testBoxing(Map map, String key) { map.merge(key, 1, (i1, i2) -> i1+1); } // IDEA-196415 void testFlush() { Map aMap = new HashMap<>(); aMap.computeIfAbsent("a", key -> key); if (aMap.isEmpty()) { System.out.println("EMPTY"); } else { System.out.println("NOT EMPTY"); } int len = aMap.size(); doSmth(); if(aMap.size() != len) { System.out.println("Impossible: Map is not escaped yet"); } } native void doSmth(); // IDEA-204641 public static void testAnyLambda(@NotNull Function lambda) { Map map = new HashMap<>(); Throwable thrown = null; try { map.computeIfAbsent("a", lambda); } catch (Throwable t) { thrown = t; throw t; } finally { if (thrown != null) { // possible int x = 0; } } } }