Skip to content

Instantly share code, notes, and snippets.

@making
Created October 10, 2015 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save making/0fabbaa4f7bc4e7dcfba to your computer and use it in GitHub Desktop.
Save making/0fabbaa4f7bc4e7dcfba to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.client.AsyncRestTemplate;
import java.util.*;
import java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args) throws Exception {
Main main = new Main();
List<String> usernames = Arrays.asList("philwebb", "dsyer", "wilkinsona", "snicoll", "rwinch");
Optional<CompletableFuture<List<GitHubStar>>> ret = usernames.stream()
.map(main::findStars)
.reduce((f1, f2) -> f1.thenCombine(f2,
(l1, l2) -> {
l1.addAll(l2);
return l1;
}));
ret.get().thenAccept(stars -> stars.stream()
.distinct()
.sorted(Comparator.comparing(GitHubStar::getUpdatedAt).reversed())
.forEach(System.out::println)
).get();
}
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
public CompletableFuture<List<GitHubStar>> findStars(String username) {
CompletableFuture<List<GitHubStar>> future = new CompletableFuture<>();
ListenableFuture<ResponseEntity<List<GitHubStar>>> response = restTemplate.exchange(
"https://api.github.com/users/{username}/starred", HttpMethod.GET, null,
new ParameterizedTypeReference<List<GitHubStar>>() {
}, Collections.singletonMap("username", username));
response.addCallback(x -> future.complete(x.getBody()), future::completeExceptionally);
return future;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class GitHubStar {
@JsonProperty("full_name")
private String name;
@JsonProperty("html_url")
private String url;
private String description;
@JsonProperty("updated_at")
private Date updatedAt;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public String toString() {
return "GitHubStar{" +
"name='" + name + '\'' +
", url='" + url + '\'' +
", description='" + description + '\'' +
", updatedAt=" + updatedAt +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GitHubStar that = (GitHubStar) o;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (url != null ? !url.equals(that.url) : that.url != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
return !(updatedAt != null ? !updatedAt.equals(that.updatedAt) : that.updatedAt != null);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (url != null ? url.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (updatedAt != null ? updatedAt.hashCode() : 0);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment