From b4a05863f63664db023055e4874a703032de4859 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Thu, 19 Sep 2024 13:41:51 +0300 Subject: [PATCH] feat(#710): fix the problem with sequence object elements counting --- .../directives/DirectivesSeq.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesSeq.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesSeq.java index f3aeeb0d4..24f64f7dd 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesSeq.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesSeq.java @@ -28,6 +28,9 @@ import java.util.List; import java.util.Objects; import java.util.Random; +import java.util.stream.Stream; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; import org.xembly.Directive; import org.xembly.Directives; @@ -85,19 +88,25 @@ public Iterator iterator() { .attr("base", new EoFqn(String.format("seq%d", this.size())).fqn()) .attr("line", new Random().nextInt(Integer.MAX_VALUE)) .attr("name", this.name) - .append( - this.directives.stream() - .filter(Objects::nonNull) - .map(Directives::new) - .reduce(new Directives(), Directives::append) - ).up().iterator(); + .append(this.stream().reduce(new Directives(), Directives::append)).up().iterator(); } /** * Size of the sequence. * @return Size. */ - private int size() { - return this.directives.size(); + private long size() { + return this.stream().count(); + } + + /** + * Stream of directives. + * @return Stream of directives. + */ + private Stream stream() { + return this.directives.stream() + .filter(Objects::nonNull) + .filter(dirs -> new Unchecked<>(new LengthOf(dirs)).value() > 0) + .map(Directives::new); } }