From 60b6a3f5c23d7600f65e7b7b4ee645968bb48f7a Mon Sep 17 00:00:00 2001 From: Alekseeva Yana Date: Tue, 23 Jan 2024 23:27:39 +0300 Subject: [PATCH] fix(#2790):retuned first signature Optimization --- .../org/eolang/maven/OptimizationTask.java | 9 ++-- .../eolang/maven/optimization/OptCached.java | 45 ++++++++++--------- .../org/eolang/maven/optimization/OptSpy.java | 7 ++- .../eolang/maven/optimization/OptTrain.java | 8 ++-- .../maven/optimization/Optimization.java | 5 +-- .../maven/optimization/OptCachedTest.java | 25 +++++++---- 6 files changed, 54 insertions(+), 45 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizationTask.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizationTask.java index 2141d58cb1..dce33e0e8a 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizationTask.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizationTask.java @@ -105,7 +105,7 @@ public Scalar value( this.update.apply( tojo, this.make( - this.optimization(tojo, common).apply(src), src + this.optimization(tojo, common).apply(new XMLDocument(src)), src ).toAbsolutePath() ); return 1; @@ -144,15 +144,18 @@ private Path make(final XML xml, final Path file) throws IOException { * @param tojo Tojo * @param common Optimization * @return Optimization for specific Tojo + * @throws Exception If fails */ - private Optimization optimization(final ForeignTojo tojo, final Optimization common) { + private Optimization optimization(final ForeignTojo tojo, final Optimization common) + throws Exception { final Optimization res; if (tojo.hasHash()) { res = new OptCached( common, this.paths.get( OptimizationFolder.CACHE.key() - ).resolve(this.dirs.get(OptimizationFolder.CACHE.key())).resolve(tojo.hash()) + ).resolve(this.dirs.get(OptimizationFolder.CACHE.key())).resolve(tojo.hash()), + this.source.apply(tojo) ); } else { res = common; diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java index 9b115c7fba..88b20135e8 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java @@ -42,9 +42,6 @@ * In ParseMojo we have condition {@code if (tojo.hasHash())}, in OptimizeMojo or ShakeMojo we * compare creation time of files. * Don't forget to enable the tests. - * @todo #2790:30min Get the XML name from its path, - * but doesn't use {@code xml.xpath("/program/@name").get(0)}, - * in classes {@link OptCached}, {@link OptTrain}, {@link OptSpy}. */ public final class OptCached implements Optimization { @@ -58,38 +55,47 @@ public final class OptCached implements Optimization { */ private final Path folder; + /** + * Absolute path of program xml. + */ + private final Path path; + /** * The main constructor. * * @param delegate Real optimization. * @param folder Cache folder. + * @param path XML file path. */ public OptCached( final Optimization delegate, - final Path folder + final Path folder, + final Path path ) { this.delegate = delegate; this.folder = folder; + this.path = path; } @Override - public XML apply(final Path path) throws Exception { + public XML apply(final XML xml) { + final String name = xml.xpath("/program/@name").get(0); try { final XML optimized; - if (this.contains(path)) { - optimized = new XMLDocument(this.cached(path)); + if (this.contains(name)) { + optimized = new XMLDocument(this.cached(name)); } else { - optimized = this.delegate.apply(path); + optimized = this.delegate.apply(xml); new FtDefault(this.folder).save( - new XMLDocument(path).xpath("/program/@name").get(0), + name, AssembleMojo.IR_EXTENSION, optimized::toString ); } return optimized; } catch (final IOException ex) { - throw new IOException( - String.format("Can't optimize '%s'", path), + throw new IllegalStateException( + String.format("Can't optimize '%s'", xml), ex ); } @@ -98,33 +104,30 @@ public XML apply(final Path path) throws Exception { /** * Returns the path to the cached program. * Pay attention that the path is not checked for existence. - * @param path Path eo program. + * @param name Name XML program. * @return Path to the cached program. * @throws IOException If fails. */ - private Path cached(final Path path) throws IOException { - return new Place( - new XMLDocument(path) - .xpath("/program/@name").get(0) - ) + private Path cached(final String name) throws IOException { + return new Place(name) .make(this.folder, AssembleMojo.IR_EXTENSION); } /** * Checks if the cache contains the program. - * @param path Path eo program. + * @param name Name XML program. * @return True if the cache contains the program. * @throws IOException If fails. */ - private boolean contains(final Path path) throws IOException { - final Path cache = this.cached(path); + private boolean contains(final String name) throws IOException { + final Path cache = this.cached(name); final boolean res; if (Files.exists(cache)) { res = !Files .getLastModifiedTime(cache) .toInstant() .isBefore( - Files.getLastModifiedTime(path) + Files.getLastModifiedTime(this.path) .toInstant() ); } else { diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java index a44988274d..c9b036cb63 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptSpy.java @@ -25,7 +25,6 @@ import com.jcabi.log.Logger; import com.jcabi.xml.XML; -import com.jcabi.xml.XMLDocument; import com.yegor256.xsline.Shift; import com.yegor256.xsline.Train; import java.nio.file.Path; @@ -67,9 +66,9 @@ public OptSpy(final Train trn, final Path target) { } @Override - public XML apply(final Path path) throws Exception { + public XML apply(final XML xml) { final Path dir = new Place( - new XMLDocument(path) + xml .xpath("/program/@name") .get(0) ) @@ -78,6 +77,6 @@ public XML apply(final Path path) throws Exception { this, "Optimization steps will be tracked to %s", new Rel(dir) ); - return new OptTrain(new SpyTrain(this.train, dir)).apply(path); + return new OptTrain(new SpyTrain(this.train, dir)).apply(xml); } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java index fc51d8f1c1..0dfb18ac17 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptTrain.java @@ -24,7 +24,6 @@ package org.eolang.maven.optimization; import com.jcabi.xml.XML; -import com.jcabi.xml.XMLDocument; import com.yegor256.xsline.Shift; import com.yegor256.xsline.StClasspath; import com.yegor256.xsline.TrClasspath; @@ -32,7 +31,6 @@ import com.yegor256.xsline.TrFast; import com.yegor256.xsline.Train; import com.yegor256.xsline.Xsline; -import java.nio.file.Path; /** * Optimisation train of XLS`s. @@ -89,7 +87,7 @@ public OptTrain() { * @param shifts XLS shifts. */ public OptTrain(final Train shifts) { - this(path -> new XMLDocument(path), shifts); + this(xml -> xml, shifts); } /** @@ -119,7 +117,7 @@ public OptTrain( } @Override - public XML apply(final Path path) throws Exception { - return new Xsline(this.shifts).pass(this.delegate.apply(path)); + public XML apply(final XML xml) { + return new Xsline(this.shifts).pass(this.delegate.apply(xml)); } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java index 79f3398c11..78efed87ba 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.java @@ -24,8 +24,7 @@ package org.eolang.maven.optimization; import com.jcabi.xml.XML; -import java.nio.file.Path; -import org.cactoos.Func; +import java.util.function.Function; /** * Abstraction for XML optimizations. @@ -33,5 +32,5 @@ * @since 0.28.11 */ @FunctionalInterface -public interface Optimization extends Func { +public interface Optimization extends Function { } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java index 08621dec1b..fe1431816d 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java @@ -72,8 +72,9 @@ void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path cache, @TempDir fin path -> { throw new IllegalStateException("This code shouldn't be executed"); }, - cache - ).apply(program), + cache, + program + ).apply(xml), Matchers.equalTo(xml) ); } @@ -101,8 +102,9 @@ void returnsFromCacheButTimesSaveAndExecuteDifferent( path -> { throw new IllegalStateException("This code shouldn't be executed"); }, - cache - ).apply(program), + cache, + program + ).apply(xml), Matchers.equalTo(xml) ); } @@ -126,8 +128,9 @@ void returnsFromCacheCorrectProgram(@TempDir final Path cache, @TempDir final Pa "Expecting current program to be compiled, but prev program was returned from cache.", new OptCached( path -> OptCachedTest.program("second program"), - cache - ).apply(current), + cache, + current + ).apply(OptCachedTest.program("second program")), Matchers.equalTo(OptCachedTest.program("second program")) ); } @@ -138,7 +141,10 @@ void optimizesIfXmlIsAbsentInCache(@TempDir final Path cache, @TempDir final Pat final Path program = OptCachedTest.save(dir, OptCachedTest.program()); MatcherAssert.assertThat( "We expect that the program will be created and returned as is (same instance)", - new OptCached(path -> OptCachedTest.program(), cache).apply(program), + new OptCached( + path -> OptCachedTest.program(), cache, program + ) + .apply(OptCachedTest.program()), Matchers.equalTo(OptCachedTest.program()) ); MatcherAssert.assertThat( @@ -169,9 +175,10 @@ void optimizesBecauseCacheIsExpired( "We expected that the program will be optimized because the cache is expired", new OptCached( path -> OptCachedTest.program("new program"), - cache + cache, + program ) - .apply(program), + .apply(OptCachedTest.program("new program")), Matchers.equalTo(OptCachedTest.program("new program")) ); }