Skip to content

Commit

Permalink
submit java-advanced-ru/asynchrony
Browse files Browse the repository at this point in the history
  • Loading branch information
hexlet-cli committed Feb 21, 2024
1 parent 800cea6 commit a13d51b
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .current.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"assignment":"java-advanced-ru/sync-primitives"}
{"assignment":"java-advanced-ru/asynchrony"}
8 changes: 4 additions & 4 deletions java-advanced-ru/asynchrony/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.gradle/
build/
caches/
.idea/
.gradle/
build/
caches/
.idea/
4 changes: 2 additions & 2 deletions java-advanced-ru/asynchrony/resultFile.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Hello
World
Hello
World
130 changes: 65 additions & 65 deletions java-advanced-ru/asynchrony/src/main/java/exercise/App.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
package exercise;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutionException;

public class App {

// BEGIN
public static CompletableFuture<String> unionFiles(String file1path, String file2path, String resFilePath) {
var futureReadFile1 = CompletableFuture.supplyAsync(() -> {
try {
return Files.readString(Path.of(file1path).toAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
var futureReadFile2 = CompletableFuture.supplyAsync(() -> {
try {
return Files.readString(Path.of(file2path).toAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
});

return futureReadFile1.thenCombine(futureReadFile2, (line1, line2) -> {
var resultString = line1.concat(line2);
try {
Files.writeString(Path.of(resFilePath).toAbsolutePath(), resultString);
} catch (IOException e) {
throw new RuntimeException(e);
}
return resultString;
}).exceptionally(e -> {
System.out.println("NoSuchFileException");
return null;
});
}

public static CompletableFuture<Long> getDirectorySize(String directoryPath) {
return CompletableFuture.supplyAsync(() -> {
var dir = new File(Path.of(directoryPath).toAbsolutePath().normalize().toString());
var filesList = dir.listFiles();
if (filesList != null) {
return Arrays.stream(filesList).filter(File::isFile).count();
} else return 0L;
});
}
// END

public static void main(String[] args) throws ExecutionException, InterruptedException {
// BEGIN
unionFiles("src/main/resources/file1.txt",
"src/main/resources/file2.txt",
"resultFile.txt").get();
// END
}
}

package exercise;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutionException;

public class App {

// BEGIN
public static CompletableFuture<String> unionFiles(String file1path, String file2path, String resFilePath) {
var futureReadFile1 = CompletableFuture.supplyAsync(() -> {
try {
return Files.readString(Path.of(file1path).toAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
var futureReadFile2 = CompletableFuture.supplyAsync(() -> {
try {
return Files.readString(Path.of(file2path).toAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
});

return futureReadFile1.thenCombine(futureReadFile2, (line1, line2) -> {
var resultString = line1.concat(line2);
try {
Files.writeString(Path.of(resFilePath).toAbsolutePath(), resultString);
} catch (IOException e) {
throw new RuntimeException(e);
}
return resultString;
}).exceptionally(e -> {
System.out.println("NoSuchFileException");
return null;
});
}

public static CompletableFuture<Long> getDirectorySize(String directoryPath) {
return CompletableFuture.supplyAsync(() -> {
var dir = new File(Path.of(directoryPath).toAbsolutePath().normalize().toString());
var filesList = dir.listFiles();
if (filesList != null) {
return Arrays.stream(filesList).filter(File::isFile).count();
} else return 0L;
});
}
// END

public static void main(String[] args) throws ExecutionException, InterruptedException {
// BEGIN
unionFiles("src/main/resources/file1.txt",
"src/main/resources/file2.txt",
"resultFile.txt").get();
// END
}
}
112 changes: 56 additions & 56 deletions java-advanced-ru/asynchrony/src/test/java/exercise/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
package exercise;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
import java.util.concurrent.CompletableFuture;

import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;

class AppTest {
private String destPath;

private static Path getFullPath(String filePath) {
return Paths.get(filePath).toAbsolutePath().normalize();
}

@BeforeEach
void beforeEach() throws Exception {
destPath = Files.createTempFile("test", "tmp").toString();
}

@Test
void testUnion() throws Exception {
CompletableFuture<String> result = App.unionFiles(
"src/test/resources/file1.txt",
"src/test/resources/file2.txt",
destPath
);
result.get();

String actual = Files.readString(getFullPath(destPath));
assertThat(actual).contains("Test", "Message");
}

@Test
void testUnionWithNonExistedFile() throws Exception {

String result = tapSystemOut(() -> {
App.unionFiles("nonExistingFile", "file", destPath).get();
});

assertThat(result.trim()).contains("NoSuchFileException");
}

// BEGIN
@Test
void testGetDirectorySize() throws Exception {
var result = App.getDirectorySize("src/test/resources/dir");
var filesCount = result.get();
assertThat(filesCount).isEqualTo(3L);
}
// END
}
package exercise;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
import java.util.concurrent.CompletableFuture;

import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;

class AppTest {
private String destPath;

private static Path getFullPath(String filePath) {
return Paths.get(filePath).toAbsolutePath().normalize();
}

@BeforeEach
void beforeEach() throws Exception {
destPath = Files.createTempFile("test", "tmp").toString();
}

@Test
void testUnion() throws Exception {
CompletableFuture<String> result = App.unionFiles(
"src/test/resources/file1.txt",
"src/test/resources/file2.txt",
destPath
);
result.get();

String actual = Files.readString(getFullPath(destPath));
assertThat(actual).contains("Test", "Message");
}

@Test
void testUnionWithNonExistedFile() throws Exception {

String result = tapSystemOut(() -> {
App.unionFiles("nonExistingFile", "file", destPath).get();
});

assertThat(result.trim()).contains("NoSuchFileException");
}

// BEGIN
@Test
void testGetDirectorySize() throws Exception {
var result = App.getDirectorySize("src/test/resources/dir");
var filesCount = result.get();
assertThat(filesCount).isEqualTo(3L);
}
// END
}

0 comments on commit a13d51b

Please sign in to comment.