From e993aba5617a699099bf1206a3856a7ba8310508 Mon Sep 17 00:00:00 2001 From: Beevood Date: Mon, 31 Jul 2023 19:41:12 +0300 Subject: [PATCH 1/2] CompletableFuture --- .gitignore | 38 ++++++++++++ .idea/encodings.xml | 7 +++ .idea/misc.xml | 14 +++++ .idea/uiDesigner.xml | 124 ++++++++++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 46 +++++++++++++++ pom.xml | 25 ++++++++ src/main/java/Main.java | 122 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 382 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 pom.xml create mode 100644 src/main/java/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f8e994c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..5570d92 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + 1690470337263 + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..01167d6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + org.example + AsyncCourse + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.fasterxml.jackson.core + jackson-databind + 2.15.2 + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..6cab1ac --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,122 @@ +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class Main { + private final static HttpClient client = HttpClient.newHttpClient(); + private final static ObjectMapper mapper = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + private final static HttpResponse.BodyHandler BODY_HANDLER = HttpResponse.BodyHandlers.ofString(); + + public static void main(String[] args) { + long time = System.currentTimeMillis(); + User user = getUser(); + System.out.println(user); + System.out.println(System.currentTimeMillis() - time); + } + + public record User(String name, String email, List repos) { + public User(@JsonProperty("login") String name, String email, List repos) { + this.name = name; + this.email = email; + this.repos = repos == null ? new ArrayList<>() : repos; + } + + public void addAllRepos(List repos) { + this.repos.addAll(repos); + } + } + + public record Repo(String name, String contributeUrl, List contributeNames) { + public Repo(String name, @JsonProperty("contributors_url") String contributeUrl, List contributeNames) { + this.name = name; + this.contributeUrl = contributeUrl; + this.contributeNames = contributeNames == null ? new ArrayList<>() : contributeNames; + } + + public void addContributeNames(List ctr) { + this.contributeNames.addAll(ctr); + } + } + + record Contributor(@JsonProperty("login") String name) { + } + + public static User getUser() { + HttpRequest requestClient = httpRequestGET("https://api.github.com/users/pivotal"); + HttpRequest requestRepo = httpRequestGET("https://api.github.com/users/pivotal/repos"); + + CompletableFuture> reposFuture = client.sendAsync(requestRepo, BODY_HANDLER) + .thenApply(HttpResponse::body) + .thenApply(Main::parseRepos) + .thenApply(repos -> { + List> allFutures = new ArrayList<>(); + List resultRepos = repos.stream().limit(10).toList(); + resultRepos.forEach(repo -> + allFutures.add(client.sendAsync(httpRequestGET(repo.contributeUrl), BODY_HANDLER) + .thenApply(HttpResponse::body) + .thenApply(Main::parseContributors) + .thenAccept(repo::addContributeNames) + .thenAccept(nothing -> System.out.println("contribute")) + )); + CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[0])).join(); + return resultRepos; + } + ); + + return client.sendAsync(requestClient, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .thenApply(Main::parseUser) + .thenCombine(reposFuture, (userF, reposF) -> { + userF.addAllRepos(reposF); + return userF; + }) + .thenApply(f -> { + System.out.println("user"); + return f; + }).join(); + } + + private static HttpRequest httpRequestGET(String url) { + return HttpRequest.newBuilder() + .uri(URI.create(url)) + .GET() + .build(); + } + + private static List parseRepos(String jsonString) { + try { + return Arrays.stream(mapper.readValue(jsonString, Repo[].class)).toList(); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + private static List parseContributors(String jsonString) { + try { + return Arrays.stream(mapper.readValue(jsonString, Contributor[].class)).map(Contributor::name).toList(); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + + private static User parseUser(String jsonString) { + try { + return mapper.readValue(jsonString, User.class); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + +} From f73d9ac9b19cd164ffef9a6f78cb308da808883d Mon Sep 17 00:00:00 2001 From: Beevood <45046335+Beevood@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:15:20 +0300 Subject: [PATCH 2/2] Delete .idea directory --- .idea/encodings.xml | 7 --- .idea/misc.xml | 14 ----- .idea/uiDesigner.xml | 124 ------------------------------------------- .idea/vcs.xml | 6 --- .idea/workspace.xml | 46 ---------------- 5 files changed, 197 deletions(-) delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/uiDesigner.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index aa00ffa..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index f8e994c..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml deleted file mode 100644 index 2b63946..0000000 --- a/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 5570d92..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - 1690470337263 - - - - \ No newline at end of file