Skip to content

Commit

Permalink
objectionary#2863 try to get rid of default methods in Home interface
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed May 2, 2024
1 parent c73be94 commit 9a959ae
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 130 deletions.
10 changes: 10 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/BiIoProc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.eolang.maven.util;

import org.cactoos.BiProc;

import java.io.IOException;

interface BiIoProc<T, U> extends BiProc<T, U> {
@Override
void exec(T t, U u) throws IOException;
}
91 changes: 0 additions & 91 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/DataSaved.java

This file was deleted.

122 changes: 90 additions & 32 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,11 +26,14 @@
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 @@ -48,6 +51,8 @@ public final class HmBase implements Home {
*/
private final Path cwd;

private final Home origin;

/**
* Ctor.
*
Expand All @@ -60,43 +65,96 @@ public HmBase(final File file) {
/**
* Ctor.
*
* @param path Path
* @param pth Path
*/
public HmBase(final Path pth) {
this.cwd = pth;
this.origin = new HmNew(
(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
);
}
}
);
}

/**
* Saving string.
*
* @param str String
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
@Override
public void save(final String str, final Path path) throws IOException {
this.origin.save(str, path);
}

/**
* Saving text.
*
* @param txt Text
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
public HmBase(final Path path) {
this.cwd = path;
@Override
public void save(final Text txt, final Path path) throws IOException {
this.origin.save(txt, path);
}

/**
* Saving stream.
*
* @param stream Input stream
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
@Override
public void save(final InputStream stream, final Path path) throws IOException {
this.origin.save(stream, path);
}

/**
* Saving bytes.
*
* @param bytes Byte array
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
@Override
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
63 changes: 63 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/HmNew.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.eolang.maven.util;

import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

public class HmNew implements Home {
private final BiIoProc<Input, Path> sve;

HmNew(BiIoProc<Input, Path> save) {
this.sve = save;
}

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

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

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

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

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

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

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

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

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

0 comments on commit 9a959ae

Please sign in to comment.