-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
800cea6
commit a13d51b
Showing
5 changed files
with
128 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"assignment":"java-advanced-ru/sync-primitives"} | ||
{"assignment":"java-advanced-ru/asynchrony"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.gradle/ | ||
build/ | ||
caches/ | ||
.idea/ | ||
.gradle/ | ||
build/ | ||
caches/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Hello | ||
World | ||
Hello | ||
World |
130 changes: 65 additions & 65 deletions
130
java-advanced-ru/asynchrony/src/main/java/exercise/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
112
java-advanced-ru/asynchrony/src/test/java/exercise/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |