1010import java .util .Objects ;
1111
1212import static com .bobocode .HTTPGet .parseImageUrls ;
13+ import static java .lang .Long .parseLong ;
1314import static java .net .http .HttpRequest .BodyPublishers .noBody ;
1415import static java .util .Comparator .comparingLong ;
16+ import static java .util .Objects .nonNull ;
17+ import static java .util .Objects .requireNonNull ;
1518
1619public class HM12 {
1720
@@ -22,33 +25,29 @@ public static void main(String[] args) {
2225 parseImageUrls (get (link ).body ())
2326 .parallelStream ()
2427 .map (HM12 ::get )
25- .filter (res -> Objects . nonNull (getHeader (res , "Location" )))
28+ .filter (res -> nonNull (getHeader (res , "Location" )))
2629 .map (res -> getHeader (res , "Location" ))
2730 .map (HM12 ::head )
28- .filter (res -> Objects .nonNull (getHeader (res , "Content-Length" )))
29- .max (comparingLong (res -> Long .parseLong (getHeader (res , "Content-Length" ))))
31+ .max (comparingLong (res -> parseLong (requireNonNull (getHeader (res , "Content-Length" )))))
3032 .ifPresent (res -> System .out .printf ("%s - length: %s" , res .uri (), getHeader (res , "Content-Length" )));
3133 }
3234
3335 @ SneakyThrows
34- public static HttpResponse <String > get (String link ) {
35- return exchange (link , "GET" , noBody ());
36- }
37-
38- @ SneakyThrows
39- public static HttpResponse <String > head (String link ) {
40- return exchange (link , "HEAD" , noBody ());
41- }
42-
43- @ SneakyThrows
44- public static HttpResponse <String > exchange (String link , String method , HttpRequest .BodyPublisher bodyPublisher ) {
36+ private static HttpResponse <String > exchange (String link , String method , HttpRequest .BodyPublisher bodyPublisher ) {
4537 var request = HttpRequest .newBuilder (new URI (link )).method (method , bodyPublisher ).build ();
4638 return httpClient .send (request , HttpResponse .BodyHandlers .ofString ());
4739 }
4840
4941 private static String getHeader (HttpResponse <String > response , String header ) {
50- Objects .requireNonNull (response );
5142 List <String > values = response .headers ().map ().get (header );
5243 return values == null || values .isEmpty () ? null : values .get (0 );
5344 }
45+
46+ private static HttpResponse <String > get (String link ) {
47+ return exchange (link , "GET" , noBody ());
48+ }
49+
50+ private static HttpResponse <String > head (String link ) {
51+ return exchange (link , "HEAD" , noBody ());
52+ }
5453}
0 commit comments