diff --git a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java index 54a130a67e..6b2cd14252 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java +++ b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicApp.java @@ -4,11 +4,11 @@ import java.io.IOException; import java.util.List; public class GitHubBasicApp { - + public static void main(String[] args) throws IOException { String userName = "eugenp"; - List topContributors = new GitHubBasicService().getTopContributors(userName); - topContributors.stream().forEach(System.out::println); + List topContributors = new GitHubBasicService() + .getTopContributors(userName); + topContributors.forEach(System.out::println); } - } diff --git a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java index fbfdd96567..20256fb540 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java +++ b/libraries/src/main/java/com/baeldung/retrofit/basic/GitHubBasicService.java @@ -1,54 +1,60 @@ package com.baeldung.retrofit.basic; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - import com.baeldung.retrofit.models.Contributor; import com.baeldung.retrofit.models.Repository; - import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; -public class GitHubBasicService { - +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +class GitHubBasicService { + private GitHubBasicApi gitHubApi; - - public GitHubBasicService() { + + GitHubBasicService() { Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") - .addConverterFactory(GsonConverterFactory.create()) - .build(); - - gitHubApi = retrofit.create(GitHubBasicApi.class); + .baseUrl("https://api.github.com/") + .addConverterFactory(GsonConverterFactory.create()) + .build(); + + gitHubApi = retrofit.create(GitHubBasicApi.class); } - - public List getTopContributors(String userName) throws IOException { + + List getTopContributors(String userName) throws IOException { List repos = gitHubApi .listRepos(userName) .execute() .body(); - - List topContributors = new ArrayList<>(); - for(Repository repo : repos) { - List contributers = gitHubApi - .listRepoContributors(userName, repo.getName()) - .execute() - .body(); - - List repoTopContributors = contributers.stream() - .filter(c -> c.getContributions() > 100) - .collect(Collectors.toList()); - topContributors.addAll(repoTopContributors); - } - - Collections.sort(topContributors, (a, b) -> b.getContributions() - a.getContributions()); - return topContributors.stream() - .map(c -> c.getName()) + + repos = repos != null ? repos : Collections.emptyList(); + + return repos.stream() + .flatMap(repo -> getContributors(userName, repo)) + .sorted((a, b) -> b.getContributions() - a.getContributions()) + .map(Contributor::getName) .distinct() + .sorted() .collect(Collectors.toList()); } + private Stream getContributors(String userName, Repository repo) { + List contributors = null; + try { + contributors = gitHubApi + .listRepoContributors(userName, repo.getName()) + .execute() + .body(); + } catch (IOException e) { + e.printStackTrace(); + } + + contributors = contributors != null ? contributors : Collections.emptyList(); + + return contributors.stream() + .filter(c -> c.getContributions() > 100); + } } diff --git a/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java b/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java index b7b7869ff6..2f8697f603 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java +++ b/libraries/src/main/java/com/baeldung/retrofit/models/Contributor.java @@ -2,11 +2,6 @@ package com.baeldung.retrofit.models; import com.google.gson.annotations.SerializedName; -/** - * GitHub Contributer - * @author hany - * - */ public class Contributor { @SerializedName("login") diff --git a/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java b/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java index 61e4752ce7..f12fcdf8f2 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java +++ b/libraries/src/main/java/com/baeldung/retrofit/models/Repository.java @@ -1,10 +1,5 @@ package com.baeldung.retrofit.models; -/** - * GitHub Repository - * @author hany - * - */ public class Repository { private String name; diff --git a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java index 0e0d8b1a77..b136a1e40b 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java +++ b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxApp.java @@ -3,11 +3,10 @@ package com.baeldung.retrofit.rx; import java.io.IOException; public class GitHubRxApp { - + public static void main(String[] args) throws IOException { String userName = "eugenp"; new GitHubRxService().getTopContributors(userName) .subscribe(System.out::println); } - } diff --git a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java index 56d149d13e..2ad50a9f39 100644 --- a/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java +++ b/libraries/src/main/java/com/baeldung/retrofit/rx/GitHubRxService.java @@ -1,33 +1,33 @@ package com.baeldung.retrofit.rx; +import com.baeldung.retrofit.models.Contributor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import rx.Observable; -public class GitHubRxService { - +class GitHubRxService { + private GitHubRxApi gitHubApi; - - public GitHubRxService() { + + GitHubRxService() { Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://api.github.com/") - .addConverterFactory(GsonConverterFactory.create()) - .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) - .build(); - + .baseUrl("https://api.github.com/") + .addConverterFactory(GsonConverterFactory.create()) + .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) + .build(); + gitHubApi = retrofit.create(GitHubRxApi.class); } - - public Observable getTopContributors(String userName) { + + Observable getTopContributors(String userName) { return gitHubApi.listRepos(userName) - .flatMap( repos -> Observable.from(repos)) - .flatMap( repo -> gitHubApi.listRepoContributors(userName, repo.getName()) ) - .flatMap( contributers -> Observable.from(contributers)) - .filter( c -> c.getContributions() > 100) - .sorted( (a, b) -> b.getContributions() - a.getContributions() ) - .map( c -> c.getName()) + .flatMapIterable(x -> x) + .flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName())) + .flatMapIterable(x -> x) + .filter(c -> c.getContributions() > 100) + .sorted((a, b) -> b.getContributions() - a.getContributions()) + .map(Contributor::getName) .distinct(); } - }