Closed snago closed 6 years ago
That's a nice approach. Maybe the other methods should be deprecated in favor of this. Could you type of a few examples so I can compare?
Yes, TagCreator.filter
, TagCreator.each(Collection, Function)
and TagCreator.each(Map, Function)
can probably be deprecated in favor of TagCreator.each(Stream)
.
I would keep TagCreator.each(Map, BiFunction)
though, as that can be easier to use than having to deal with a stream of entries.
Here is the filter example from https://j2html.com/examples.html, rewritten to use the new each
-method and with the employees sorted by name:
body(
div(attrs("#employees"),
p("Some sibling element"),
each(employees.stream()
.filter(Objects::nonNull)
.sorted(Comparator.comparing(Employee::getName))
.map(employee -> div(attrs(".employee"),
h2(employee.getName()),
img().withSrc(employee.getImgPath()),
p(employee.getTitle())
))
)
)
)
Thanks! I'll merge this now and think about deprecating the other methods.
The each-methods that takes a Collection or a Map and a Function or BiFunction are nice, but accepting Stream directly would be even more powerful.
would become
Sure, that's not shorter but it means other stream functionality such as limit, flatMap, etc. won't have to be duplicated as TagCreator methods. E.g.: