Skip to content

Commit

Permalink
Merge pull request #41679 from DaHoC/feature/6963_qute_iterate_long
Browse files Browse the repository at this point in the history
Qute: add support to iterate long numbers
  • Loading branch information
geoand authored Jul 4, 2024
2 parents a4b91ee + e0cabae commit 6c515be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ A section helper that defines the logic of a section can "execute" any of the bl
[[loop_section]]
==== Loop Section

The loop section makes it possible to iterate over an instance of `Iterable`, `Iterator`, array, `Map` (element is a `Map.Entry`), `Stream`, `Integer` and `int` (primitive value).
The loop section makes it possible to iterate over an instance of `Iterable`, `Iterator`, array, `Map` (element is a `Map.Entry`), `Stream`, `Integer`, `Long`, `int` and `long` (primitive value).
A `null` parameter value results in a no-op.

This section has two flavors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import io.quarkus.qute.SectionHelperFactory.SectionInitContext;
Expand Down Expand Up @@ -84,6 +85,8 @@ private static int extractSize(Object it) {
return Array.getLength(it);
} else if (it instanceof Integer) {
return ((Integer) it);
} else if (it instanceof Long) {
return (((Long) it).intValue());
} else if (it instanceof Collection) {
return ((Collection<?>) it).size();
} else if (it instanceof Map) {
Expand All @@ -101,6 +104,8 @@ private Iterator<?> extractIterator(Object it) {
return ((AbstractMap<?, ?>) it).entrySet().iterator();
} else if (it instanceof Integer) {
return IntStream.rangeClosed(1, (Integer) it).iterator();
} else if (it instanceof Long) {
return LongStream.rangeClosed(1, (Long) it).iterator();
} else if (it.getClass().isArray()) {
int length = Array.getLength(it);
List<Object> elements = new ArrayList<>(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ public void testIntegerStream() {
engine.parse("{#for i in total}{i}:{/for}").data("total", 3).render());
}

@Test
public void testLongStream() {
Engine engine = Engine.builder().addDefaults().build();

assertEquals("1:2:3:",
engine.parse("{#for i in total}{i}:{/for}").data("total", 3L).render());
}

@Test
public void testIterator() {
Engine engine = Engine.builder().addDefaults().build();
Expand Down

0 comments on commit 6c515be

Please sign in to comment.