@SuppressWarnings("unchecked") @Test public void testHelper() { final MustacheEngine engine = MustacheEngineBuilder.newBuilder() .registerHelper("decorateCat", decorate(Cat.class).put("const", "Cat") .compute("name", cat -> cat.getName().toUpperCase()) .compute("overweight", (cat) -> cat.getWeight() > 4500 ? cat.getWeight() - 4500 : 0).build()) .registerHelper("decorateIntList", decorate(List.class) .compute("sum", l -> l.stream().mapToInt(e -> Integer.valueOf(e.toString())).sum()).build()) .registerHelper("dummy", decorate(String.class).delegateKey("foo").compute("toUpperCase", s -> s.toLowerCase()).build()) .registerHelper("decorateStr", decorate(String.class) .compute("reverse", s -> new StringBuilder(s).reverse().toString()).build()) .build(); assertEquals("ooF", engine.compileMustache("{{#decorateStr}}{{reverse}}{{/decorateStr}}").render("Foo")); List<Cat> cats = new ArrayList<>(); cats.add(new Cat("Mikes", 3000)); cats.add(new Cat("Mourek", 5000)); assertEquals("Cat MIKES [3000g] has overweight 0g.Cat MOUREK [5000g] has overweight 500g.", engine.compileMustache( "{{#each}}{{#decorateCat}}{{const}} {{name}} [{{weight}}g] has overweight {{overweight}}g.{{/decorateCat}}{{/each}}") .render(cats)); try { engine.compileMustache("{{#decorateCat}}{{name}}{{/decorateCat}}").render("foo"); fail(); } catch (IllegalStateException expected) { } assertEquals("6:123", engine.compileMustache("{{#decorateIntList}}{{sum}}:{{#each}}{{.}}{{/each}}{{/decorateIntList}}") .render(Arrays.asList(1, 2, 3))); assertEquals("lu vs LU", engine.compileMustache("{{#dummy}}{{toUpperCase}} vs {{foo.toUpperCase}}{{/dummy}}").render("Lu")); }
public static <T> Builder<T> decorate(Class<T> delegateType) { return new Builder<>(o -> delegateType.isAssignableFrom(o.getClass())); }
DecoratorHelper extends BasicSectionHelper { public static <T> Builder<T> decorate(Class<T> delegateType) { return new Builder<>(o -> delegateType.isAssignableFrom(o.getClass())); } }
DecoratorHelper extends BasicSectionHelper { public static <T> Builder<T> decorate(Class<T> delegateType) { return new Builder<>(o -> delegateType.isAssignableFrom(o.getClass())); } private  DecoratorHelper(Predicate<Object> test, Map<String, Function<T, Object>> mappings, String delegateKey); }
DecoratorHelper extends BasicSectionHelper { public static <T> Builder<T> decorate(Class<T> delegateType) { return new Builder<>(o -> delegateType.isAssignableFrom(o.getClass())); } private  DecoratorHelper(Predicate<Object> test, Map<String, Function<T, Object>> mappings, String delegateKey); static Builder<T> decorate(Class<T> delegateType); static Builder<T> decorate(Predicate<Object> test); @SuppressWarnings("unchecked") @Override void execute(Options options); }
DecoratorHelper extends BasicSectionHelper { public static <T> Builder<T> decorate(Class<T> delegateType) { return new Builder<>(o -> delegateType.isAssignableFrom(o.getClass())); } private  DecoratorHelper(Predicate<Object> test, Map<String, Function<T, Object>> mappings, String delegateKey); static Builder<T> decorate(Class<T> delegateType); static Builder<T> decorate(Predicate<Object> test); @SuppressWarnings("unchecked") @Override void execute(Options options);  }