Skip to content

Instantly share code, notes, and snippets.

@PavloChechehov
Created June 17, 2022 05:33
Show Gist options
  • Save PavloChechehov/87b8a44e4865237a4cc8b0a2558f3aa5 to your computer and use it in GitHub Desktop.
Save PavloChechehov/87b8a44e4865237a4cc8b0a2558f3aa5 to your computer and use it in GitHub Desktop.
Friday Demo
package com.bobocode.lesson12;
import com.bobocode.lesson11.NasaResponse;
import com.bobocode.lesson11.Photo;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class FridayDemo {
/*HOMEWORK #11
Additional part:
Speed up your implementation using multiple threads
Make sure that it works faster than a single-thread impl
Post a screenshot of your solution
Implement the same logic using another HTTP client library
Make sure that it gives the same result
Post a screenshot of your solution*/
public static final String NASA_URL = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=15&api_key=DEMO_KEY";
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
long start = System.currentTimeMillis();
var response = restTemplate.getForObject(NASA_URL, NasaResponse.class);
CompletableFuture<Pair<String, Long>>[] array = restTemplate
.getForObject(NASA_URL, NasaResponse.class)
.getPhotos().stream()
// .parallel()
.map(Photo::getImgSrc)
.map(img -> {
return CompletableFuture.supplyAsync(() -> restTemplate.headForHeaders(URI.create(img)).getLocation())
.thenApply(uri -> Pair.of(img, restTemplate.headForHeaders(uri).getContentLength()));
})
.toArray(CompletableFuture[]::new);
CompletableFuture<Void> allOf = CompletableFuture.allOf(array);
List<Pair<String, Long>> longs = new ArrayList<>();
for (CompletableFuture<Pair<String, Long>> completableFuture : array) {
longs.add(completableFuture.join());
}
longs.stream().max(Comparator.comparing(Pair::getRight)).ifPresent(System.out::println);
long time = System.currentTimeMillis() - start;
System.out.printf("%s millis\n", time);
// Map<String, Long> map = Objects.requireNonNull(response).getPhotos()
// .stream()
// .map(Photo::getImgSrc)
//
// .collect(toMap(s -> s, img -> {
//
//// return CompletableFuture.supplyAsync(() -> restTemplate.headForHeaders(img).getLocation())
//// .thenApply(uri -> restTemplate.headForHeaders(uri).getContentLength())
//// .join();
// URI uri = restTemplate.headForHeaders(img).getLocation();
// return restTemplate.headForHeaders(uri).getContentLength();
// }));
// long time = System.currentTimeMillis() - start;
// Optional<Map.Entry<String, Long>> max = map.entrySet()
// .stream()
// .max(Map.Entry.comparingByValue());
//
// max.ifPresent(x -> {
// System.out.println("img_src:" + x.getKey());
// System.out.println("size:" + x.getValue());
// });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment