From 5e153e801f42c95cd307d2e280e443442b35078b Mon Sep 17 00:00:00 2001 From: Jia-Sheng Chen Date: Thu, 18 May 2023 11:26:18 +0800 Subject: [PATCH 1/2] rewrite app and app.web by java 17 instanceof --- app/src/main/java/org/astraea/app/App.java | 4 ++-- app/src/main/java/org/astraea/app/web/RecordHandler.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/astraea/app/App.java b/app/src/main/java/org/astraea/app/App.java index 481501bfff..118b048af1 100644 --- a/app/src/main/java/org/astraea/app/App.java +++ b/app/src/main/java/org/astraea/app/App.java @@ -70,8 +70,8 @@ static void execute(Map> mains, List args) throws Throw method.invoke(null, (Object) args.subList(1, args.size()).toArray(String[]::new)); } catch (InvocationTargetException targetException) { // Print out ParameterException, don't throw. - if (targetException.getTargetException() instanceof ParameterException) { - System.out.println(targetException.getTargetException().getMessage()); + if (targetException.getTargetException() instanceof ParameterException exception) { + System.out.println(exception.getMessage()); } else { throw targetException.getTargetException(); } diff --git a/app/src/main/java/org/astraea/app/web/RecordHandler.java b/app/src/main/java/org/astraea/app/web/RecordHandler.java index 8f3a70178f..3be24e5b3a 100644 --- a/app/src/main/java/org/astraea/app/web/RecordHandler.java +++ b/app/src/main/java/org/astraea/app/web/RecordHandler.java @@ -410,8 +410,8 @@ public String json() { @Override public void onComplete(Throwable error) { try { - if (error == null && consumer instanceof SubscribedConsumer) { - ((SubscribedConsumer) consumer).commitOffsets(Duration.ofSeconds(5)); + if (error == null && consumer instanceof SubscribedConsumer subscribedConsumer) { + subscribedConsumer.commitOffsets(Duration.ofSeconds(5)); } } finally { consumer.close(); From b86375e5520b70ecd3653f33e5e24802030fc421 Mon Sep 17 00:00:00 2001 From: Jia-Sheng Chen Date: Thu, 18 May 2023 16:50:34 +0800 Subject: [PATCH 2/2] rewrite app and app.web by java 17 toList --- .../main/java/org/astraea/app/web/RecordHandler.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/astraea/app/web/RecordHandler.java b/app/src/main/java/org/astraea/app/web/RecordHandler.java index 3be24e5b3a..a2eda70c95 100644 --- a/app/src/main/java/org/astraea/app/web/RecordHandler.java +++ b/app/src/main/java/org/astraea/app/web/RecordHandler.java @@ -17,7 +17,6 @@ package org.astraea.app.web; import static java.util.Objects.requireNonNull; -import static java.util.stream.Collectors.toList; import java.time.Duration; import java.util.Base64; @@ -163,8 +162,7 @@ public CompletionStage get(Channel channel) { // visible for testing GetResponse get(Consumer consumer, int limit, Duration timeout) { try { - return new GetResponse( - consumer, consumer.poll(timeout).stream().map(Record::new).collect(toList())); + return new GetResponse(consumer, consumer.poll(timeout).stream().map(Record::new).toList()); } catch (Exception e) { consumer.close(); throw e; @@ -190,9 +188,7 @@ public CompletionStage post(Channel channel) { () -> { try { return producer.send( - records.stream() - .map(record -> createRecord(producer, record)) - .collect(toList())); + records.stream().map(record -> createRecord(producer, record)).toList()); } finally { if (producer.transactional()) { producer.close(); @@ -214,7 +210,7 @@ public CompletionStage post(Channel channel) { return Response.for404("missing result"); })) .map(CompletionStage::toCompletableFuture) - .collect(toList()))); + .toList())); if (postRequest.async()) return CompletableFuture.completedFuture(Response.ACCEPT); return CompletableFuture.completedFuture( @@ -438,7 +434,7 @@ static class Record { timestamp = record.timestamp(); serializedKeySize = record.serializedKeySize(); serializedValueSize = record.serializedValueSize(); - headers = record.headers().stream().map(Header::new).collect(toList()); + headers = record.headers().stream().map(Header::new).toList(); key = record.key(); value = record.value(); leaderEpoch = record.leaderEpoch().orElse(null);