Skip to content

Commit

Permalink
objectionary#2863 make Home abstract class to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed Mar 4, 2024
1 parent 72632f2 commit 4e872b4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 58 deletions.
25 changes: 1 addition & 24 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 @@ -26,14 +26,11 @@
import com.jcabi.log.Logger;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.Bytes;
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 @@ -45,7 +42,7 @@
* @since 0.27
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class HmBase implements Home {
public final class HmBase extends Home {
/**
* Current working directory.
*/
Expand All @@ -69,26 +66,6 @@ public HmBase(final Path path) {
this.cwd = path;
}

@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 {
final Path target = this.absolute(this.onlyRelative(path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@

import com.jcabi.log.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;

/**
* Location for files that saves optionally.
* @since 0.32.0
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class HmOptional implements Home {
public final class HmOptional extends Home {
/**
* Original home.
*/
Expand All @@ -59,26 +56,6 @@ public HmOptional(final Home home, final boolean rwte) {
this.rewrite = rwte;
}

@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 {
final Path target = this.absolute(this.onlyRelative(path));
Expand Down
29 changes: 19 additions & 10 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;

/**
* Location for the files.
* @since 0.32.0
*/
public interface Home {
public abstract class Home {
/**
* Saving string.
*
* @param str String
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(String str, Path path) throws IOException;
public void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
}

/**
* Saving text.
Expand All @@ -51,7 +54,9 @@ public interface Home {
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(Text txt, Path path) throws IOException;
public void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
}

/**
* Saving stream.
Expand All @@ -60,7 +65,9 @@ public interface Home {
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(InputStream stream, Path path) throws IOException;
public void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
}

/**
* Saving bytes.
Expand All @@ -69,7 +76,9 @@ public interface Home {
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(byte[] bytes, Path path) throws IOException;
public void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
}

/**
* Saving input.
Expand All @@ -79,7 +88,7 @@ public interface Home {
* @throws IOException If fails
* @throws IllegalArgumentException If given path is absolute
*/
void save(Input input, Path path) throws IOException;
public abstract void save(Input input, Path path) throws IOException;

/**
* Check if exists.
Expand All @@ -88,7 +97,7 @@ public interface Home {
* @return True if exists
* @throws IllegalArgumentException If given path is absolute
*/
boolean exists(Path path);
public abstract boolean exists(Path path);

/**
* Load bytes from file by path.
Expand All @@ -99,21 +108,21 @@ public interface Home {
* if some exception happens during reading the file
* @throws IllegalArgumentException If given path is absolute
*/
Bytes load(Path path) throws IOException;
public abstract Bytes load(Path path) throws IOException;

/**
* Absolute path to a file.
*
* @param path Cwd-relative path to file
* @return Absolute path
*/
Path absolute(Path path);
public abstract Path absolute(Path path);

/**
* Verifies that given path is relative and throws exception.
* @param path Path to be verified
* @return Given path if it's relative
* @throws IllegalArgumentException If given path is Absolute
*/
Path onlyRelative(Path path);
public abstract Path onlyRelative(Path path);
}

0 comments on commit 4e872b4

Please sign in to comment.