diff --git a/src/it/lints-it/src/test/java/org/eolang/lints/it/LintsItTest.java b/src/it/lints-it/src/test/java/org/eolang/lints/it/LintsItTest.java index c4e99d19a..c816af1ae 100644 --- a/src/it/lints-it/src/test/java/org/eolang/lints/it/LintsItTest.java +++ b/src/it/lints-it/src/test/java/org/eolang/lints/it/LintsItTest.java @@ -22,7 +22,7 @@ final class LintsItTest { void lintsProgram() throws IOException { MatcherAssert.assertThat( "passes with no exceptions", - new Program(new XMLDocument("")).defects(), + new Project(new XMLDocument("")).singleDefects(), Matchers.notNullValue() ); } diff --git a/src/main/java/org/eolang/lints/Program.java b/src/main/java/org/eolang/lints/Program.java index 9f52b678c..8aaee2f99 100644 --- a/src/main/java/org/eolang/lints/Program.java +++ b/src/main/java/org/eolang/lints/Program.java @@ -22,7 +22,7 @@ * @see XMIR * @since 0.1.0 */ -public final class Program { +final class Program { /** * Collection of mono lints, preloaded on JVM start. @@ -48,7 +48,7 @@ public final class Program { * @param file The absolute path of the XMIR file * @throws FileNotFoundException If file isn't found */ - public Program(final Path file) throws FileNotFoundException { + Program(final Path file) throws FileNotFoundException { this(new XMLDocument(file)); } @@ -56,7 +56,7 @@ public Program(final Path file) throws FileNotFoundException { * Ctor. * @param xml The XMIR */ - public Program(final XML xml) { + Program(final XML xml) { this(xml, Program.MONO); } diff --git a/src/main/java/org/eolang/lints/Programs.java b/src/main/java/org/eolang/lints/Programs.java index 46a096212..c2bfd3183 100644 --- a/src/main/java/org/eolang/lints/Programs.java +++ b/src/main/java/org/eolang/lints/Programs.java @@ -37,7 +37,7 @@ * @see XMIR * @since 0.1.0 */ -public final class Programs { +final class Programs { /** * Collection of mono lints, preloaded on JVM start. @@ -65,7 +65,7 @@ public final class Programs { * @param dirs The directory * @throws IOException If fails */ - public Programs(final Path... dirs) throws IOException { + Programs(final Path... dirs) throws IOException { this(Arrays.asList(dirs)); } @@ -78,7 +78,7 @@ public Programs(final Path... dirs) throws IOException { * @param dirs The directory * @throws IOException If fails */ - public Programs(final Collection dirs) throws IOException { + Programs(final Collection dirs) throws IOException { this(Programs.discover(dirs)); } @@ -86,7 +86,7 @@ public Programs(final Collection dirs) throws IOException { * Ctor. * @param map The map with them */ - public Programs(final Map map) { + Programs(final Map map) { this(map, Programs.WPA); } diff --git a/src/main/java/org/eolang/lints/Project.java b/src/main/java/org/eolang/lints/Project.java new file mode 100644 index 000000000..d139e8ab0 --- /dev/null +++ b/src/main/java/org/eolang/lints/Project.java @@ -0,0 +1,27 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.lints; + +import java.util.Collection; + +/** + * Project, files of which can be analyzed separately or together. + * + * @since 0.0.9 + */ +public interface Project { + + /** + * Defects found by analyzing each file independently. + * @return Collection of defects from all files + */ + Collection singleDefects(); + + /** + * Defects found by analyzing all files together. + * @return Collection of defects from all files + */ + Collection wpaDefects(); +} diff --git a/src/main/java/org/eolang/lints/ProjectOf.java b/src/main/java/org/eolang/lints/ProjectOf.java new file mode 100644 index 000000000..2d6a95446 --- /dev/null +++ b/src/main/java/org/eolang/lints/ProjectOf.java @@ -0,0 +1,123 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.lints; + +import com.jcabi.xml.XML; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import org.cactoos.func.Chained; +import org.cactoos.iterable.Mapped; +import org.cactoos.list.Joined; +import org.cactoos.list.ListOf; + +/** + * Implementation of Project. Provides several ways to make Project object. + * + * @since 0.0.9 + */ +public final class ProjectOf implements Project { + + /** + * Collection of programs that should be analyzed independently. + */ + private final Iterable programs; + + /** + * Programs that should be analyzed together. + */ + private final Programs wpa; + + /** + * Ctor. + * @param xml XML source. + */ + public ProjectOf(final XML xml) { + this(xml, "single"); + } + + /** + * Ctor. + * @param xml XML source. + * @param name File name. + */ + public ProjectOf(final XML xml, final String name) { + this( + new Program(xml), + new Programs(Map.of(name, xml)) + ); + } + + /** + * Ctor. + * @param home Path to file. + * @throws IOException Throw IOException in case of problems working with files. + */ + public ProjectOf(final Path home) throws IOException { + this( + new Program(home), + new Programs(home) + ); + } + + /** + * Ctor. + * @param programs Set of programs XMLs with names. + * @param list List of lints for independent analyze. + * @param wpa List of lints for full project analyze. + */ + public ProjectOf( + final Map programs, + final Iterable> list, + final Iterable>> wpa + ) { + this( + new Mapped<>( + xml -> new Program(xml, list), + programs.values() + ), + new Programs(programs, wpa) + ); + } + + /** + * Ctor. + * @param program Single program. + * @param wpa Programs for whole program analysis. + */ + ProjectOf(final Program program, final Programs wpa) { + this( + new ListOf<>(program), + wpa + ); + } + + /** + * Ctor. + * @param programs List of single programs. + * @param wpa Programs for whole program analysis. + */ + ProjectOf(final Iterable programs, final Programs wpa) { + this.programs = programs; + this.wpa = wpa; + } + + @Override + public Collection singleDefects() { + return new Joined<>( + new Mapped>( + new Chained<>(Program::defects, ListOf::new), + this.programs + ) + ); + } + + @Override + public Collection wpaDefects() { + return this.wpa.defects(); + } +} diff --git a/src/test/java/benchmarks/ProgramBench.java b/src/test/java/benchmarks/ProgramBench.java index d5ee6ad81..65de7a63e 100644 --- a/src/test/java/benchmarks/ProgramBench.java +++ b/src/test/java/benchmarks/ProgramBench.java @@ -9,7 +9,8 @@ import fixtures.LargeXmir; import java.util.concurrent.TimeUnit; import org.cactoos.scalar.Unchecked; -import org.eolang.lints.Program; +import org.eolang.lints.Project; +import org.eolang.lints.ProjectOf; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -21,7 +22,7 @@ import org.openjdk.jmh.annotations.Warmup; /** - * Benchmark for {@link Program}. + * Benchmark for {@link Project}::singleDefects. * * @since 0.0.34 * @checkstyle DesignForExtensionCheck (10 lines) @@ -47,11 +48,11 @@ public class ProgramBench { @Benchmark public final void scansLargeXmir() { - new Program(ProgramBench.LARGE).defects(); + new ProjectOf(ProgramBench.LARGE).singleDefects(); } @Benchmark public final void scansSmallXmir() { - new Program(ProgramBench.SMALL).defects(); + new ProjectOf(ProgramBench.SMALL).singleDefects(); } } diff --git a/src/test/java/benchmarks/ProgramsBench.java b/src/test/java/benchmarks/ProgramsBench.java index e76cb7bc2..521cc588d 100644 --- a/src/test/java/benchmarks/ProgramsBench.java +++ b/src/test/java/benchmarks/ProgramsBench.java @@ -11,8 +11,8 @@ import java.nio.file.Path; import java.util.concurrent.TimeUnit; import org.cactoos.scalar.IoChecked; -import org.eolang.lints.Program; -import org.eolang.lints.Programs; +import org.eolang.lints.Project; +import org.eolang.lints.ProjectOf; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; @@ -24,7 +24,7 @@ import org.openjdk.jmh.annotations.Warmup; /** - * Benchmark for {@link Program}. + * Benchmark for {@link Project}::wpaDefects. * * @since 0.0.34 * @checkstyle DesignForExtensionCheck (10 lines) @@ -62,6 +62,6 @@ public ProgramsBench() { @Benchmark public final void scansLargeProgram() throws IOException { - new Programs(this.home).defects(); + new ProjectOf(this.home).wpaDefects(); } }