|
14 | 14 | import java.util.Map.Entry;
|
15 | 15 | import java.util.Objects;
|
16 | 16 | import java.util.Optional;
|
| 17 | +import java.util.function.BiFunction; |
17 | 18 | import java.util.function.Function;
|
18 | 19 | import java.util.function.Predicate;
|
19 | 20 | import java.util.stream.Collectors;
|
@@ -99,6 +100,20 @@ public static <I, T> DomContent each(final Map<I, T> map, final Function<Entry<I
|
99 | 100 | return rawHtml(map.entrySet().stream().map(mapper.andThen(DomContent::render)).collect(Collectors.joining()));
|
100 | 101 | }
|
101 | 102 |
|
| 103 | + /** |
| 104 | + * Creates a DomContent object containing HTML using a mapping function on a map |
| 105 | + * Intended usage: {@literal each(idsToNames, (id, name) -> li(id + " " + name))} |
| 106 | + * |
| 107 | + * @param <I> The type of the keys |
| 108 | + * @param <T> The type of the values |
| 109 | + * @param map the map to iterate over, ex: a map of values { 1: "Tom", 2: "Dick", 3: "Harry" } |
| 110 | + * @param mapper the mapping function, ex: {@literal "(id, name) -> li(id + " " + name)"} |
| 111 | + * @return DomContent containing mapped data {@literal (ex. docs: [li(1 Tom), li(2 Dick), li(3 Harry)])} |
| 112 | + */ |
| 113 | + public static <I, T> DomContent each(final Map<I, T> map, final BiFunction<I, T, DomContent> mapper) { |
| 114 | + return rawHtml(map.entrySet().stream().map(entry -> mapper.andThen(DomContent::render).apply(entry.getKey(), entry.getValue())).collect(Collectors.joining())); |
| 115 | + } |
| 116 | + |
102 | 117 | /**
|
103 | 118 | * Filters a collection to a list, to be used with {@link j2html.TagCreator#each}
|
104 | 119 | * Intended usage: {@literal each(filter(numbers, n -> n % 2 == 0), n -> li(n.toString()))}
|
|
0 commit comments