/** wraps a call to {@link String#format(String, Object...)} in a toString, i.e. using %s syntax, * useful for places where we want deferred evaluation * (e.g. as message to {@link Preconditions} to skip concatenation when not needed) */ public static FormattedString format(String pattern, Object... args) { return new FormattedString(pattern, args); }
@Test public void testToStringSupplier() { ToStringCounter c = new ToStringCounter(true); Assert.assertEquals(Strings.toStringSupplier(c).get(), "world1"); FormattedString x = Strings.format("hello %s", c); Assert.assertEquals(x.toString(), "hello world2"); Assert.assertEquals(x.toString(), "hello world3"); }
@Test public void testDeferredFormat() { ToStringCounter c = new ToStringCounter(); FormattedString x = Strings.format("hello %s", c); Assert.assertEquals(c.count, 0); Assert.assertEquals(x.toString(), "hello world"); Assert.assertEquals(c.count, 1); }