import org.jetbrains.annotations.Nullable; import java.util.Optional; public class OptionalInlining { void testOrElse() { String s = Optional.ofNullable("foo").orElse("bar"); if (s.equals("bar")) { System.out.println("Never"); } String s2 = Optional.ofNullable(null).orElse("bar"); if (s2.equals("bar")) { System.out.println("Always"); } String s3 = Optional.of(Math.random() > 0.5 ? "foo" : "baz").orElse("bar"); if (s3.equals("foo") || s3.equals("baz")) { System.out.println("Always"); } if (s3.equals("bar")) { System.out.println("Never"); } } void testGuavaOr() { String res = com.google.common.base.Optional.of("xyz").or("foo"); if(res.equals("xyz")) { System.out.println("Always"); } if(res.equals("foo")) { System.out.println("Never"); } String res2 = com.google.common.base.Optional.absent().or(() -> "foo"); if(res2.equals("xyz")) { System.out.println("Never"); } if(res2.equals("foo")) { System.out.println("Always"); } } void testGuavaOrNull() { String s = com.google.common.base.Optional.fromNullable("foo").orNull(); if (s == null) { System.out.println("Never"); } if (s.equals("foo")) { System.out.println("Always"); } } void testIsPresent(Optional opt) { if (!opt.isPresent() && opt.orElse("foo").equals("bar")) { } } void testDeref(Optional opt) { if (opt == null) { System.out.println(opt.orElse("qq")); } } void testOrElseGet(Optional opt) { opt.orElseGet(null); String s = opt.orElseGet(() -> { if (Math.random() > 0.5) { return "foo"; } return "baz"; }); if (s.equals("bar") && !opt.isPresent()) { System.out.println("Impossible"); } } void testFilter(Optional opt, Optional intOpt) { opt.filter(null); Integer integer = intOpt.filter(x -> x > 5).filter(x -> x == 5).orElse(0); String s1 = opt.filter(s -> false).filter(s -> s.equals("barr")).orElse("baz"); if (s1.equals("xz")) { System.out.println("never"); } String abc = opt.filter(s -> s == "xyz").orElse("abc"); // s.equals("xyz") does not work yet :( if (abc.equals("123")) { System.out.println("never"); } if (abc.equals("xyz") && opt.isPresent()) { System.out.println("always"); } opt.filter(x -> x.length() > 5).filter(x -> x.isEmpty()).ifPresent(x -> System.out.println(x)); } @Nullable String nullableMethod() { if(Math.random() > 0.5) { return null; } return ""; } @Nullable Optional nullableOptionalMethod(String x) { return x.isEmpty() ? null : Optional.of(x); } @Nullable Object getObj(String s) { return new Object(); } void testMap(Optional opt) { opt.map(null); String res = opt.map(s -> null).orElse("abc"); if (!res.equals("abc")) { System.out.println("Never"); } String trimmed = Optional.ofNullable(nullableMethod()).map(xx -> xx.trim()).orElse(""); if(trimmed == null) { System.out.println("impossible"); } String xyz = nullableMethod(); Object n = Optional.ofNullable(xyz).map(String::trim).map(this::getObj).orElse(null); if(n instanceof Integer) { // n instanceof Integer -> n is not null -> xyz was not null -> safe to dereference System.out.println(xyz.trim()); } xyz.trim(); //opt.map(x -> x.isEmpty() ? "foo" : "bar").filter(x -> x.isEmpty()).ifPresent(x -> System.out.println(x)); } void testGuavaTransform(com.google.common.base.Optional opt) { String trimmed = com.google.common.base.Optional.fromNullable(nullableMethod()).transform(xx -> xx.trim()).or(""); if(trimmed == null) { System.out.println("impossible"); } if(opt.isPresent()) { if(opt.transform(x -> x.isEmpty() ? null : x).toJavaUtil().isPresent()) { System.out.println("Always"); } if(opt.toJavaUtil().map(x -> x.isEmpty() ? null : x).isPresent()) { System.out.println("Sometimes"); } } } void testToJavaUtil() { String xyz = nullableMethod(); Object n = com.google.common.base.Optional.fromNullable(xyz).transform(String::trim).toJavaUtil().map(this::getObj).orElse(null); if(n instanceof Integer) { // n instanceof Integer -> n is not null -> xyz was not null -> safe to dereference System.out.println(xyz.trim()); } xyz.trim(); } void testFlatMap(Optional opt) { opt.flatMap(null); opt.flatMap(x -> null); opt.flatMap(this::nullableOptionalMethod); opt.flatMap(x -> x.isEmpty() ? null : Optional.of(x)); opt.flatMap(x -> { if (x.isEmpty()) { return null; } else { return Optional.of(x); } }); String s = opt.flatMap(str -> Optional.of(str.length() > 10 ? "foo" : "bar")).orElse("baz"); if (s.equals("qux")) { System.out.println("Never"); } boolean res = opt.filter(x -> x.isEmpty()).flatMap(x -> x.length() <= 2 ? Optional.empty() : Optional.of("foo")) .isPresent(); } void testIfPresent(Optional opt) { opt.ifPresent(null); opt.map(s -> s.isEmpty() ? 5 : 6).ifPresent(val -> { if (val == 7) { System.out.println("oops"); } }); } void testIntermediate(Optional opt) { if ( x == \"bar\").isPresent()' is always 'false'">opt.filter(x -> x == "foo").filter(x -> x == "bar").isPresent()) { System.out.println("never"); } } // IDEA-174759 void testTwoOptionalInteraction(Optional a, Optional b) { if (a.isPresent() || b.isPresent()) { // prefer a over b Integer result = a.map(s -> s + "0").map(s -> Integer.parseInt(s)) .orElseGet(() -> Integer.parseInt(b.get())); // <-- no more warning for b.get() System.out.println(result); } } void testTwoOptionalInteractionMethodRef(Optional a, Optional b) { if (a.isPresent() || b.isPresent()) { // prefer a over b Integer result = a.map(s -> s + "0").map(Integer::parseInt) .orElseGet(() -> Integer.parseInt(b.get())); // <-- no more warning for b.get() System.out.println(result); } } static class Holder { int x; String s; } void testFilterChain(Optional opt) { boolean present = opt .filter(h -> h.x < 5) .filter(h -> h.x > 6) .map(h -> h.x).isPresent(); } void testFilterMap(Optional opt) { boolean present = opt .filter(h -> h.s == null) .map(h -> h.s) .isPresent(); opt.filter(h -> h.s == null).map(h -> h.s.trim()).ifPresent(System.out::println); } }