From 203ba478599ec291f8a084ac072feeaa92013ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=8E=E9=B8=A3?= Date: Fri, 26 Jun 2026 05:10:19 +0800 Subject: [PATCH 1/2] refactor: migrate String.format to String.formatted (Java 15+) Motivation: Java 15 introduced String.formatted() as an instance method counterpart to String.format(), providing cleaner and more readable string formatting. Modification: Replace String.format("literal", args) with "literal".formatted(args) across 14 files including elasticsearch, geode, google-cloud-bigquery, hbase, kudu, mongodb, pravega, solr, spring-web, sqs, and sse modules. Result: More concise and idiomatic Java code leveraging Java 15+ API. --- .../java/docs/javadsl/ElasticsearchTestBase.java | 12 ++++++------ .../test/java/docs/javadsl/GeodeBaseTestCase.java | 4 ++-- .../javadsl/GeodeContinuousSourceTestCase.java | 2 +- .../src/test/java/docs/javadsl/BigQueryDoc.java | 2 +- .../bigquery/e2e/javadsl/BigQueryEndToEndTest.java | 8 ++++---- .../src/test/java/docs/javadsl/HBaseStageTest.java | 14 +++++++------- kudu/src/test/java/docs/javadsl/KuduTableTest.java | 6 +++--- .../src/test/java/docs/javadsl/MongoSinkTest.java | 14 +++++++------- .../connectors/pravega/PravegaKVTableTestCase.java | 2 +- solr/src/test/java/docs/javadsl/SolrTest.java | 2 +- .../web/SpringWebPekkoStreamsConfiguration.java | 5 ++--- sqs/src/test/java/docs/javadsl/SqsPublishTest.java | 2 +- .../stream/connectors/sqs/javadsl/BaseSqsTest.java | 2 +- .../test/java/docs/javadsl/EventSourceTest.java | 2 +- 14 files changed, 38 insertions(+), 39 deletions(-) diff --git a/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java b/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java index afcef37925..0e6a5261d3 100644 --- a/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java +++ b/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java @@ -68,7 +68,7 @@ protected static void prepareIndex( int port, org.apache.pekko.stream.connectors.elasticsearch.ApiVersionBase version) throws IOException { connectionSettings = - ElasticsearchConnectionSettings.create(String.format("http://localhost:%d", port)); + ElasticsearchConnectionSettings.create("http://localhost:%d".formatted(port)); register("source", "Akka in Action"); register("source", "Programming in Scala"); @@ -82,24 +82,24 @@ protected static void prepareIndex( protected static void cleanIndex() throws IOException { HttpRequest request = - HttpRequest.DELETE(String.format("%s/_all", connectionSettings.baseUrl())); + HttpRequest.DELETE("%s/_all".formatted(connectionSettings.baseUrl())); http.singleRequest(request).toCompletableFuture().join(); } protected static void flushAndRefresh(String indexName) throws IOException { HttpRequest flushRequest = - HttpRequest.POST(String.format("%s/%s/_flush", connectionSettings.baseUrl(), indexName)); + HttpRequest.POST("%s/%s/_flush".formatted(connectionSettings.baseUrl(), indexName)); http.singleRequest(flushRequest).toCompletableFuture().join(); HttpRequest refreshRequest = - HttpRequest.POST(String.format("%s/%s/_refresh", connectionSettings.baseUrl(), indexName)); + HttpRequest.POST("%s/%s/_refresh".formatted(connectionSettings.baseUrl(), indexName)); http.singleRequest(refreshRequest).toCompletableFuture().join(); } protected static void register(String indexName, String title) { HttpRequest request = - HttpRequest.POST(String.format("%s/%s/_doc", connectionSettings.baseUrl(), indexName)) - .withEntity(ContentTypes.APPLICATION_JSON, String.format("{\"title\": \"%s\"}", title)); + HttpRequest.POST("%s/%s/_doc".formatted(connectionSettings.baseUrl(), indexName)) + .withEntity(ContentTypes.APPLICATION_JSON, "{\"title\": \"%s\"}".formatted(title)); http.singleRequest(request).toCompletableFuture().join(); } diff --git a/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java b/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java index d4c35d6165..61314cbfd8 100644 --- a/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java +++ b/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java @@ -60,12 +60,12 @@ public static void setup() { static Source buildPersonsSource(Integer... ids) { return Source.from(List.of(ids)) - .map((i) -> new Person(i, String.format("Person Java %d", i), new Date())); + .map((i) -> new Person(i, "Person Java %d".formatted(i), new Date())); } static Source buildAnimalsSource(Integer... ids) { return Source.from(List.of(ids)) - .map((i) -> new Animal(i, String.format("Animal Java %d", i), 1)); + .map((i) -> new Animal(i, "Animal Java %d".formatted(i), 1)); } protected Geode createGeodeClient() { diff --git a/geode/src/test/java/docs/javadsl/GeodeContinuousSourceTestCase.java b/geode/src/test/java/docs/javadsl/GeodeContinuousSourceTestCase.java index 4043aecb02..fbf5dc14ad 100644 --- a/geode/src/test/java/docs/javadsl/GeodeContinuousSourceTestCase.java +++ b/geode/src/test/java/docs/javadsl/GeodeContinuousSourceTestCase.java @@ -58,7 +58,7 @@ public void continuousSourceTest() throws ExecutionException, InterruptedExcepti Pair>> run = Source.from(List.of(120)) - .map((i) -> new Person(i, String.format("Java flow %d", i), new Date())) + .map((i) -> new Person(i, "Java flow %d".formatted(i), new Date())) .via(flow) .toMat(Sink.seq(), Keep.both()) .run(system); diff --git a/google-cloud-bigquery/src/test/java/docs/javadsl/BigQueryDoc.java b/google-cloud-bigquery/src/test/java/docs/javadsl/BigQueryDoc.java index f16433864c..992bddda68 100644 --- a/google-cloud-bigquery/src/test/java/docs/javadsl/BigQueryDoc.java +++ b/google-cloud-bigquery/src/test/java/docs/javadsl/BigQueryDoc.java @@ -156,7 +156,7 @@ void docs() { // #run-query String sqlQuery = - String.format("SELECT name, addresses FROM %s.%s WHERE age >= 100", datasetId, tableId); + "SELECT name, addresses FROM %s.%s WHERE age >= 100".formatted(datasetId, tableId); Unmarshaller> queryResponseUnmarshaller = BigQueryMarshallers.queryResponseUnmarshaller(NameAddressesPair.class); Source>> centenarians = diff --git a/google-cloud-bigquery/src/test/java/org/apache/pekko/stream/connectors/googlecloud/bigquery/e2e/javadsl/BigQueryEndToEndTest.java b/google-cloud-bigquery/src/test/java/org/apache/pekko/stream/connectors/googlecloud/bigquery/e2e/javadsl/BigQueryEndToEndTest.java index 600ab873a0..41c3027527 100644 --- a/google-cloud-bigquery/src/test/java/org/apache/pekko/stream/connectors/googlecloud/bigquery/e2e/javadsl/BigQueryEndToEndTest.java +++ b/google-cloud-bigquery/src/test/java/org/apache/pekko/stream/connectors/googlecloud/bigquery/e2e/javadsl/BigQueryEndToEndTest.java @@ -256,8 +256,8 @@ public void retrieveRows() throws ExecutionException, InterruptedException { @Test public void runQuery() throws ExecutionException, InterruptedException { String query = - String.format( - "SELECT string, record, integer FROM %s.%s WHERE boolean;", datasetId(), tableId()); + "SELECT string, record, integer FROM %s.%s WHERE boolean;" + .formatted(datasetId(), tableId()); List> expectedResults = getRows().stream() .filter(A::getBoolean) @@ -281,8 +281,8 @@ public void runQuery() throws ExecutionException, InterruptedException { @Test public void dryRunQuery() throws ExecutionException, InterruptedException { String query = - String.format( - "SELECT string, record, integer FROM %s.%s WHERE boolean;", datasetId(), tableId()); + "SELECT string, record, integer FROM %s.%s WHERE boolean;" + .formatted(datasetId(), tableId()); QueryResponse response = BigQuery.query( query, true, false, BigQueryMarshallers.queryResponseUnmarshaller(JsonNode.class)) diff --git a/hbase/src/test/java/docs/javadsl/HBaseStageTest.java b/hbase/src/test/java/docs/javadsl/HBaseStageTest.java index 62ee0b7e74..566eec17e9 100644 --- a/hbase/src/test/java/docs/javadsl/HBaseStageTest.java +++ b/hbase/src/test/java/docs/javadsl/HBaseStageTest.java @@ -64,7 +64,7 @@ public static void teardown() { Function> hBaseConverter = person -> { try { - Put put = new Put(String.format("id_%d", person.id).getBytes("UTF-8")); + Put put = new Put("id_%d".formatted(person.id).getBytes("UTF-8")); put.addColumn( "info".getBytes("UTF-8"), "name".getBytes("UTF-8"), person.name.getBytes("UTF-8")); @@ -80,7 +80,7 @@ public static void teardown() { Function> appendHBaseConverter = person -> { try { - Append append = new Append(String.format("id_%d", person.id).getBytes("UTF-8")); + Append append = new Append("id_%d".formatted(person.id).getBytes("UTF-8")); append.add( "info".getBytes("UTF-8"), "aliases".getBytes("UTF-8"), person.name.getBytes("UTF-8")); @@ -96,7 +96,7 @@ public static void teardown() { Function> deleteHBaseConverter = person -> { try { - Delete delete = new Delete(String.format("id_%d", person.id).getBytes("UTF-8")); + Delete delete = new Delete("id_%d".formatted(person.id).getBytes("UTF-8")); return List.of(delete); } catch (UnsupportedEncodingException e) { @@ -110,7 +110,7 @@ public static void teardown() { Function> incrementHBaseConverter = person -> { try { - Increment increment = new Increment(String.format("id_%d", person.id).getBytes("UTF-8")); + Increment increment = new Increment("id_%d".formatted(person.id).getBytes("UTF-8")); increment.addColumn("info".getBytes("UTF-8"), "numberOfChanges".getBytes("UTF-8"), 1); return List.of(increment); @@ -125,7 +125,7 @@ public static void teardown() { Function> complexHBaseConverter = person -> { try { - byte[] id = String.format("id_%d", person.id).getBytes("UTF-8"); + byte[] id = "id_%d".formatted(person.id).getBytes("UTF-8"); byte[] infoFamily = "info".getBytes("UTF-8"); if (person.id != 0 && person.name.isEmpty()) { @@ -166,7 +166,7 @@ public void writeToSink() throws InterruptedException, TimeoutException, Executi final Sink> sink = HTableStage.sink(tableSettings); CompletionStage o = Source.from(List.of(100, 101, 102, 103, 104)) - .map((i) -> new Person(i, String.format("name %d", i))) + .map((i) -> new Person(i, "name %d".formatted(i))) .runWith(sink, system); // #sink @@ -187,7 +187,7 @@ public void writeThroughFlow() throws ExecutionException, InterruptedException { Flow flow = HTableStage.flow(tableSettings); Pair>> run = Source.from(List.of(200, 201, 202, 203, 204)) - .map((i) -> new Person(i, String.format("name_%d", i))) + .map((i) -> new Person(i, "name_%d".formatted(i))) .via(flow) .toMat(Sink.seq(), Keep.both()) .run(system); diff --git a/kudu/src/test/java/docs/javadsl/KuduTableTest.java b/kudu/src/test/java/docs/javadsl/KuduTableTest.java index d91ba6cc54..039f873153 100644 --- a/kudu/src/test/java/docs/javadsl/KuduTableTest.java +++ b/kudu/src/test/java/docs/javadsl/KuduTableTest.java @@ -98,7 +98,7 @@ public void sink() throws Exception { CompletionStage o = Source.from(List.of(100, 101, 102, 103, 104)) - .map((i) -> new Person(i, String.format("name %d", i))) + .map((i) -> new Person(i, "name %d".formatted(i))) .runWith(sink, system); // #sink assertEquals(Done.getInstance(), o.toCompletableFuture().get(5, TimeUnit.SECONDS)); @@ -111,7 +111,7 @@ public void flow() throws Exception { CompletionStage> run = Source.from(List.of(200, 201, 202, 203, 204)) - .map((i) -> new Person(i, String.format("name_%d", i))) + .map((i) -> new Person(i, "name_%d".formatted(i))) .via(flow) .toMat(Sink.seq(), Keep.right()) .run(system); @@ -140,7 +140,7 @@ public void customClient() throws Exception { CompletionStage> run = Source.from(List.of(200, 201, 202, 203, 204)) - .map((i) -> new Person(i, String.format("name_%d", i))) + .map((i) -> new Person(i, "name_%d".formatted(i))) .via(flow) .toMat(Sink.seq(), Keep.right()) .run(system); diff --git a/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java b/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java index 99abf3ce1c..0dad559091 100644 --- a/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java +++ b/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java @@ -97,9 +97,9 @@ private void insertDomainObjects() throws Exception { DomainObject domainObject = new DomainObject( i, - String.format("first-property-%s", i), - String.format("second-property-%s", i)); - System.out.println(String.format("%s inserting %s", i, domainObject)); + "first-property-%s".formatted(i), + "second-property-%s".formatted(i)); + System.out.println("%s inserting %s".formatted(i, domainObject)); return domainObject; }) .runWith(MongoSink.insertOne(domainObjectsColl), system) @@ -340,8 +340,8 @@ public void replaceWithReplaceOne() throws Exception { Filters.eq("_id", i), new DomainObject( i, - String.format("updated-first-property-%s", i), - String.format("updated-second-property-%s", i)))); + "updated-first-property-%s".formatted(i), + "updated-second-property-%s".formatted(i)))); final CompletionStage completion = source.runWith(MongoSink.replaceOne(domainObjectsColl), system); // #replace-one @@ -361,8 +361,8 @@ public void replaceWithReplaceOne() throws Exception { i -> new DomainObject( i, - String.format("updated-first-property-%s", i), - String.format("updated-second-property-%s", i))) + "updated-first-property-%s".formatted(i), + "updated-second-property-%s".formatted(i))) .toList(); assertEquals(expected, found); diff --git a/pravega/src/test/java/org/apache/pekko/stream/connectors/pravega/PravegaKVTableTestCase.java b/pravega/src/test/java/org/apache/pekko/stream/connectors/pravega/PravegaKVTableTestCase.java index ecfd81e414..768efde636 100644 --- a/pravega/src/test/java/org/apache/pekko/stream/connectors/pravega/PravegaKVTableTestCase.java +++ b/pravega/src/test/java/org/apache/pekko/stream/connectors/pravega/PravegaKVTableTestCase.java @@ -105,7 +105,7 @@ public void writeAndReadInKVTable() String result = readingDone.toCompletableFuture().get(timeoutSeconds, TimeUnit.SECONDS); Assertions.assertTrue( - result.equals("One, Two, Three, Four"), String.format("Read 2 elements [%s]", result)); + result.equals("One, Two, Three, Four"), "Read 2 elements [%s]".formatted(result)); Flow, NotUsed> readFlow = PravegaTable.readFlow(scope, tableName, tableReaderSettings); diff --git a/solr/src/test/java/docs/javadsl/SolrTest.java b/solr/src/test/java/docs/javadsl/SolrTest.java index 2dc1cdf6fd..cc5789d3fb 100644 --- a/solr/src/test/java/docs/javadsl/SolrTest.java +++ b/solr/src/test/java/docs/javadsl/SolrTest.java @@ -887,7 +887,7 @@ private TupleStream getTupleStream(String collection) throws IOException { streamContext.setSolrClientCache(solrClientCache); String expressionStr = - String.format("search(%s, q=*:*, fl=\"title,comment\", sort=\"title asc\")", collection); + "search(%s, q=*:*, fl=\"title,comment\", sort=\"title asc\")".formatted(collection); StreamExpression expression = StreamExpressionParser.parse(expressionStr); TupleStream stream = new CloudSolrStream(expression, factory); stream.setStreamContext(streamContext); diff --git a/spring-web/src/main/java/org/apache/pekko/stream/connectors/spring/web/SpringWebPekkoStreamsConfiguration.java b/spring-web/src/main/java/org/apache/pekko/stream/connectors/spring/web/SpringWebPekkoStreamsConfiguration.java index 6c46797be8..0812d49561 100644 --- a/spring-web/src/main/java/org/apache/pekko/stream/connectors/spring/web/SpringWebPekkoStreamsConfiguration.java +++ b/spring-web/src/main/java/org/apache/pekko/stream/connectors/spring/web/SpringWebPekkoStreamsConfiguration.java @@ -57,9 +57,8 @@ public SpringWebPekkoStreamsProperties getProperties() { private String getActorSystemName(final SpringWebPekkoStreamsProperties properties) { Objects.requireNonNull( properties, - String.format( - "%s is not present in application context", - SpringWebPekkoStreamsProperties.class.getSimpleName())); + "%s is not present in application context" + .formatted(SpringWebPekkoStreamsProperties.class.getSimpleName())); if (isBlank(properties.getActorSystemName())) { return DEFAULT_FACTORY_SYSTEM_NAME; diff --git a/sqs/src/test/java/docs/javadsl/SqsPublishTest.java b/sqs/src/test/java/docs/javadsl/SqsPublishTest.java index 75611f6318..eb32323664 100644 --- a/sqs/src/test/java/docs/javadsl/SqsPublishTest.java +++ b/sqs/src/test/java/docs/javadsl/SqsPublishTest.java @@ -378,6 +378,6 @@ public void ackViaFlow() throws Exception { private String toMd5(String s) throws Exception { MessageDigest m = MessageDigest.getInstance("MD5"); BigInteger bigInt = new BigInteger(1, m.digest(s.getBytes())); - return String.format("%032x", bigInt); + return "%032x".formatted(bigInt); } } diff --git a/sqs/src/test/java/org/apache/pekko/stream/connectors/sqs/javadsl/BaseSqsTest.java b/sqs/src/test/java/org/apache/pekko/stream/connectors/sqs/javadsl/BaseSqsTest.java index 9839f9ccef..aa8501c415 100644 --- a/sqs/src/test/java/org/apache/pekko/stream/connectors/sqs/javadsl/BaseSqsTest.java +++ b/sqs/src/test/java/org/apache/pekko/stream/connectors/sqs/javadsl/BaseSqsTest.java @@ -100,7 +100,7 @@ protected String randomQueueUrl() throws Exception { return sqsClient .createQueue( CreateQueueRequest.builder() - .queueName(String.format("queue-%s", new Random().nextInt())) + .queueName("queue-%s".formatted(new Random().nextInt())) .build()) .get(2, TimeUnit.SECONDS) .queueUrl(); diff --git a/sse/src/test/java/docs/javadsl/EventSourceTest.java b/sse/src/test/java/docs/javadsl/EventSourceTest.java index 801bc15784..cda6c978be 100644 --- a/sse/src/test/java/docs/javadsl/EventSourceTest.java +++ b/sse/src/test/java/docs/javadsl/EventSourceTest.java @@ -51,7 +51,7 @@ public static void compileTest() { Function> send = (request) -> http.singleRequest(request); - final Uri targetUri = Uri.create(String.format("http://%s:%d", host, port)); + final Uri targetUri = Uri.create("http://%s:%d".formatted(host, port)); final Optional lastEventId = Optional.of("2"); Source eventSource = EventSource.create(targetUri, send, lastEventId, system); From 7fbd3a733cfc4d9f447255b41126960082193c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=8E=E9=B8=A3?= Date: Fri, 26 Jun 2026 17:08:21 +0800 Subject: [PATCH 2/2] style: apply javafmt to fix code style check failures Apply javafmt to Java source files in elasticsearch and mongodb modules. --- .../src/test/java/docs/javadsl/ElasticsearchTestBase.java | 3 +-- geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java | 3 +-- mongodb/src/test/java/docs/javadsl/MongoSinkTest.java | 4 +--- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java b/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java index 0e6a5261d3..a64408c123 100644 --- a/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java +++ b/elasticsearch/src/test/java/docs/javadsl/ElasticsearchTestBase.java @@ -81,8 +81,7 @@ protected static void prepareIndex( } protected static void cleanIndex() throws IOException { - HttpRequest request = - HttpRequest.DELETE("%s/_all".formatted(connectionSettings.baseUrl())); + HttpRequest request = HttpRequest.DELETE("%s/_all".formatted(connectionSettings.baseUrl())); http.singleRequest(request).toCompletableFuture().join(); } diff --git a/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java b/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java index 61314cbfd8..39122b5a49 100644 --- a/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java +++ b/geode/src/test/java/docs/javadsl/GeodeBaseTestCase.java @@ -64,8 +64,7 @@ static Source buildPersonsSource(Integer... ids) { } static Source buildAnimalsSource(Integer... ids) { - return Source.from(List.of(ids)) - .map((i) -> new Animal(i, "Animal Java %d".formatted(i), 1)); + return Source.from(List.of(ids)).map((i) -> new Animal(i, "Animal Java %d".formatted(i), 1)); } protected Geode createGeodeClient() { diff --git a/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java b/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java index 0dad559091..e65c311e56 100644 --- a/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java +++ b/mongodb/src/test/java/docs/javadsl/MongoSinkTest.java @@ -96,9 +96,7 @@ private void insertDomainObjects() throws Exception { i -> { DomainObject domainObject = new DomainObject( - i, - "first-property-%s".formatted(i), - "second-property-%s".formatted(i)); + i, "first-property-%s".formatted(i), "second-property-%s".formatted(i)); System.out.println("%s inserting %s".formatted(i, domainObject)); return domainObject; })