Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication down to 15 lines per piece #2916

Merged
merged 23 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
72632f2
#2863 add extra object to reduce code duplication
c71n93 Mar 4, 2024
4e872b4
#2863 make Home abstract class to reduce code duplication
c71n93 Mar 4, 2024
67606b9
#2863 create extra function to reduce code duplication in UnplaceMojo…
c71n93 Mar 4, 2024
34f68ad
#2863 get rid of ugly method using parametrized test
c71n93 Mar 4, 2024
31572d1
#2863 supress UnusedPrivateMethod warning
c71n93 Mar 4, 2024
0a6fa6f
#2863 add PhDecorator abstract class to reduce code duplication
c71n93 Mar 8, 2024
adee12e
#2863 delete some unnecessary methods overriding
c71n93 Mar 8, 2024
3842823
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 Mar 11, 2024
74c589c
#2863 fix qulice warnings
c71n93 Mar 11, 2024
7c77831
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 Mar 26, 2024
03e7d15
#2863 remove unnecessary test
c71n93 Mar 27, 2024
be129c9
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 Apr 11, 2024
8b58618
#2863 remove duplicated test
c71n93 Apr 11, 2024
5be5468
#2863 remove sudden groovy libs
c71n93 Apr 11, 2024
cc49278
#2863 new line at the end of the file
c71n93 Apr 11, 2024
76f06cd
#2863 get rid of Home abstract class
c71n93 Apr 15, 2024
725d9b1
#2863 rollback to current master
c71n93 Apr 15, 2024
daaee28
#2863 remove PhDecorator
c71n93 Apr 15, 2024
c73be94
#2863 remove newline at the end of file
c71n93 Apr 15, 2024
9a959ae
#2863 try to get rid of default methods in Home interface
c71n93 May 2, 2024
bc09cd0
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 May 2, 2024
3258acd
#2863 fix namings and qulice violations
c71n93 May 2, 2024
e422a9c
#2863 fix threshold
c71n93 May 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/simian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
distribution: 'temurin'
java-version: 17
- run: wget --quiet http://public.yegor256.com/simian.jar -O /tmp/simian.jar
- run: java -jar /tmp/simian.jar -threshold=19 "-excludes=**/gen" "-excludes=**/it" "**/*.java"
- run: java -jar /tmp/simian.jar -threshold=15 "-excludes=**/gen" "-excludes=**/it" "**/*.java"
87 changes: 48 additions & 39 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/HmBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.bytes.BytesOf;
import org.cactoos.io.InputOf;
import org.cactoos.io.OutputTo;
import org.cactoos.io.TeeInput;
import org.cactoos.scalar.IoChecked;
Expand All @@ -51,6 +50,11 @@ public final class HmBase implements Home {
*/
private final Path cwd;

/**
* Home with "save" functionality.
*/
private final Home origin;

/**
* Ctor.
*
Expand All @@ -63,63 +67,68 @@ public HmBase(final File file) {
/**
* Ctor.
*
* @param path Path
* @param pth Path
*/
public HmBase(final Path path) {
this.cwd = path;
public HmBase(final Path pth) {
this.cwd = pth;
this.origin = new HmSave(
(input, path) -> {
final Path target = this.absolute(this.onlyRelative(path));
if (target.toFile().getParentFile().mkdirs()) {
Logger.debug(
this, "Directory created: %s",
new Rel(target.getParent())
);
}
try {
final long bytes = new IoChecked<>(
new LengthOf(
new TeeInput(
input,
new OutputTo(target)
)
)
).value();
Logger.debug(
HmBase.class, "File %s saved (%d bytes)",
target, bytes
);
} catch (final IOException ex) {
throw new IOException(
String.format(
"Failed while trying to save to %s",
target
),
ex
);
}
}
);
}

@Override
public void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
this.origin.save(str, path);
}

@Override
public void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
this.origin.save(txt, path);
}

@Override
public void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
public void save(final InputStream stream, final Path path) throws IOException {
this.origin.save(stream, path);
}

@Override
public void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
public void save(final byte[] bytes, final Path path) throws IOException {
this.origin.save(bytes, path);
}

@Override
public void save(final Input input, final Path path) throws IOException {
final Path target = this.absolute(this.onlyRelative(path));
if (target.toFile().getParentFile().mkdirs()) {
Logger.debug(
this, "Directory created: %s",
new Rel(target.getParent())
);
}
try {
final long bytes = new IoChecked<>(
new LengthOf(
new TeeInput(
input,
new OutputTo(target)
)
)
).value();
Logger.debug(
HmBase.class, "File %s saved (%d bytes)",
target, bytes
);
} catch (final IOException ex) {
throw new IOException(
String.format(
"Failed while trying to save to %s",
target
),
ex
);
}
this.origin.save(input, path);
}

@Override
Expand Down
35 changes: 22 additions & 13 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/HmOptional.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;

/**
* Location for files that saves optionally.
Expand All @@ -43,6 +42,11 @@ public final class HmOptional implements Home {
*/
private final Home origin;

/**
* Home with "save" functionality.
*/
private final Home sve;

/**
* Rewrite files or not.
*/
Expand All @@ -57,36 +61,41 @@ public final class HmOptional implements Home {
public HmOptional(final Home home, final boolean rwte) {
this.origin = home;
this.rewrite = rwte;
this.sve = new HmSave(
(input, path) -> {
final Path target = this.absolute(this.onlyRelative(path));
if (!target.toFile().exists() || this.rewrite) {
this.origin.save(input, path);
} else {
Logger.info(this, "Rewriting of the %s file was skipped", target);
}
}
);
}

@Override
public void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
this.sve.save(str, path);
}

@Override
public void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
this.sve.save(txt, path);
}

@Override
public void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
public void save(final InputStream stream, final Path path) throws IOException {
this.sve.save(stream, path);
}

@Override
public void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
public void save(final byte[] bytes, final Path path) throws IOException {
this.sve.save(bytes, path);
}

@Override
public void save(final Input input, final Path path) throws IOException {
final Path target = this.absolute(this.onlyRelative(path));
if (!target.toFile().exists() || this.rewrite) {
this.origin.save(input, path);
} else {
Logger.info(this, "Rewriting of the %s file was skipped", target);
}
this.sve.save(input, path);
}

@Override
Expand Down
100 changes: 100 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/HmSave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* 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.eolang.maven.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import org.cactoos.BiProc;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;
import org.cactoos.proc.IoCheckedBiProc;

/**
* Home that defines the logic of saving different types of data to files.
*
* @since 0.37.0
*/
public final class HmSave implements Home {
/**
* BiProc with two arguments for saving {@link Input} from first argument to file from second.
*/
private final IoCheckedBiProc<Input, Path> sve;

/**
* Ctor.
*
* @param save BiProc for saving {@link Input} to file.
*/
public HmSave(final BiProc<Input, Path> save) {
this.sve = new IoCheckedBiProc<>(save);
}

@Override
public void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
}

@Override
public void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
}

@Override
public void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
}

@Override
public void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
}

@Override
public void save(final Input input, final Path path) throws IOException {
this.sve.exec(input, path);
}

@Override
public boolean exists(final Path path) {
throw new IllegalStateException("Should never happen");
}

@Override
public Bytes load(final Path path) throws IOException {
throw new IllegalStateException("Should never happen");
}

@Override
public Path absolute(final Path path) {
throw new IllegalStateException("Should never happen");
}

@Override
public Path onlyRelative(final Path path) {
throw new IllegalStateException("Should never happen");
}
}
Loading
Loading