Skip to content

Commit

Permalink
#2 simple metric
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 17, 2017
1 parent b32fd53 commit e1b398d
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 40 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ The MIT License (MIT)
<artifactId>cactoos</artifactId>
<version>0.20.1</version>
</dependency>
<dependency>
<groupId>com.jcabi.incubator</groupId>
<artifactId>xembly</artifactId>
<version>0.22</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/org/jpeek/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek;

import java.io.IOException;
import java.nio.file.Path;
import org.cactoos.io.LengthOf;
import org.cactoos.io.TeeInput;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.And;
import org.cactoos.scalar.IoCheckedScalar;
import org.jpeek.metrics.basic.TotalFiles;
import org.xembly.Xembler;

/**
* Application.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.1
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class App {

/**
* Location of the project to analyze.
*/
private final Path input;

/**
* Directory to save reports to.
*/
private final Path output;

/**
* Ctor.
* @param source Source directory
* @param target Target dir
*/
public App(final Path source, final Path target) {
this.input = source;
this.output = target;
}

/**
* Analyze sources.
* @throws IOException If fails
*/
public void analyze() throws IOException {
final Base base = new DefaultBase(this.input);
final Iterable<Metric> metrics = new ListOf<>(
new TotalFiles(base)
);
new IoCheckedScalar<>(
new And(
metrics,
metric -> {
new LengthOf(
new TeeInput(
new Xembler(metric.xembly()).xmlQuietly(),
this.output.resolve(
String.format(
"%s.xml",
metric.getClass().getSimpleName()
)
)
)
).value();
}
)
).value();
}

}
9 changes: 7 additions & 2 deletions src/main/java/org/jpeek/DefaultBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @version $Id$
* @since 0.1
*/
final class DefaultBase implements Base {
public final class DefaultBase implements Base {

/**
* Directory.
Expand All @@ -48,10 +48,15 @@ final class DefaultBase implements Base {
* Ctor.
* @param path Path of the directory with files
*/
DefaultBase(final Path path) {
public DefaultBase(final Path path) {
this.dir = path;
}

@Override
public String toString() {
return this.dir.toAbsolutePath().toString();
}

@Override
public Iterable<Path> files() throws IOException {
return Files.walk(this.dir).collect(Collectors.toList());
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/org/jpeek/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek;

import java.io.IOException;
import java.nio.file.Paths;

/**
* Main entry point.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.1
*/
public final class Main {

/**
* Ctor.
*/
private Main() {
// intentionally
}

/**
* Main Java entry point.
* @param args Command line args
* @throws IOException If fails
*/
public static void main(final String... args) throws IOException {
if (args.length != 2) {
throw new IllegalArgumentException(
"Exactly two arguments required: source and output directory"
);
}
new App(
Paths.get(args[0]),
Paths.get(args[1])
).analyze();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,24 @@
package org.jpeek;

import java.io.IOException;
import java.nio.file.Path;
import org.cactoos.iterable.Filtered;
import org.xembly.Directive;

/**
* Base with files only.
* Single metric.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.1
*/
final class FilesOnlyBase implements Base {
public interface Metric {

/**
* Base.
* Present it in XML format.
* @return XML report
* @throws IOException If fails
*/
private final Base base;

/**
* Ctor.
* @param bse Original base
*/
FilesOnlyBase(final Base bse) {
this.base = bse;
}

@Override
public Iterable<Path> files() throws IOException {
return new Filtered<>(
this.base.files(),
path -> path.toFile().isFile()
);
}
Iterable<Directive> xembly() throws IOException;

}
74 changes: 74 additions & 0 deletions src/main/java/org/jpeek/metrics/basic/TotalFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek.metrics.basic;

import java.io.IOException;
import java.nio.file.Files;
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.LengthOf;
import org.jpeek.Base;
import org.jpeek.Metric;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Total files.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.1
*/
public final class TotalFiles implements Metric {

/**
* The base.
*/
private final Base base;

/**
* Ctor.
* @param bse The base
*/
public TotalFiles(final Base bse) {
this.base = bse;
}

@Override
public Iterable<Directive> xembly() throws IOException {
return new Directives()
.add("app")
.attr("id", this.base.toString())
.attr(
"value",
new LengthOf(
new Filtered<>(
this.base.files(),
path -> Files.isRegularFile(path)
)
).value()
);
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/jpeek/metrics/basic/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Basic metrics.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.1
*/
package org.jpeek.metrics.basic;
Loading

0 comments on commit e1b398d

Please sign in to comment.