From 08c1050f463baaf94f1a2d1de24b2fe54f8940f7 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 29 Nov 2024 16:47:17 +0300 Subject: [PATCH] feat(#889): speed up DirectivesSeq --- .../directives/DirectivesSeq.java | 15 ++---- .../jeo/representation/xmir/XmlNode.java | 1 - .../XmirRepresentationTest.java | 3 +- .../directives/DirectivesSeqTest.java | 49 +++++++++++++++++++ 4 files changed, 55 insertions(+), 13 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 acd00e918..09371752e 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesSeq.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesSeq.java @@ -82,21 +82,16 @@ private DirectivesSeq(final String name, final Iterable... elements) @Override public Iterator iterator() { + final List all = this.stream() + .map(Directives::new) + .collect(Collectors.toList()); return new DirectivesJeoObject( - String.format("seq.of%d", this.size()), + String.format("seq.of%d", all.size()), this.name, - this.stream().map(Directives::new).collect(Collectors.toList()) + all ).iterator(); } - /** - * Size of the sequence. - * @return Size. - */ - private long size() { - return this.stream().count(); - } - /** * Stream of directives. * @return Stream of directives. diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java index 6d78faba7..db3570ef0 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java @@ -23,7 +23,6 @@ */ package org.eolang.jeo.representation.xmir; -import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import java.util.ArrayList; import java.util.List; diff --git a/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java b/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java index 4440303d7..8bbc219ed 100644 --- a/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java +++ b/src/test/java/org/eolang/jeo/representation/XmirRepresentationTest.java @@ -185,12 +185,11 @@ void convertsToXmirAndBack() { ); } final long end = System.currentTimeMillis(); - final long l = end - start; Logger.info( this, "We made %d attempts to convert bytecode to xmir and back in %[ms]s", attempts, - l + end - start ); } diff --git a/src/test/java/org/eolang/jeo/representation/directives/DirectivesSeqTest.java b/src/test/java/org/eolang/jeo/representation/directives/DirectivesSeqTest.java index 7e30277eb..70f19a1bb 100644 --- a/src/test/java/org/eolang/jeo/representation/directives/DirectivesSeqTest.java +++ b/src/test/java/org/eolang/jeo/representation/directives/DirectivesSeqTest.java @@ -24,8 +24,13 @@ package org.eolang.jeo.representation.directives; import com.jcabi.matchers.XhtmlMatchers; +import java.util.stream.Stream; import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.xembly.Directives; import org.xembly.ImpossibleModificationException; import org.xembly.Xembler; @@ -52,4 +57,48 @@ void convertsToNumberedSeq() throws ImpossibleModificationException { ) ); } + + @ParameterizedTest + @MethodSource("sequences") + void correctlyComputesSize( + final DirectivesSeq actual, final int expected + ) throws ImpossibleModificationException { + MatcherAssert.assertThat( + "The size of the sequence is not as expected", + new Xembler(actual).xml(), + XhtmlMatchers.hasXPath( + String.format( + "/o[contains(@base,'seq.of%d') and @name='@']", + expected + ) + ) + ); + } + + /** + * Sequences to test. + * @return Stream of arguments. + */ + private static Stream sequences() { + return Stream.of( + Arguments.of( + new DirectivesSeq( + new DirectivesValue("1"), new DirectivesValue("2") + ), + 2 + ), + Arguments.of(new DirectivesSeq(), 0), + Arguments.of(new DirectivesSeq(new Directives(), new Directives()), 0), + Arguments.of( + new DirectivesSeq( + new DirectivesValue("1"), new Directives(), + new Directives(), new DirectivesValue("4") + ), + 2 + ), + Arguments.of( + new DirectivesSeq(), 0 + ) + ); + } }