From 4766cb24d1a67d7705dd230b2f414b8a330536d4 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 22 Dec 2024 12:14:28 +0000 Subject: [PATCH 1/3] Performance: StringBuilder is faster than StringWriter --- .../main/java/jakarta/ws/rs/core/Variant.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java b/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java index b48bc6b10..c0b5627e8 100644 --- a/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java +++ b/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java @@ -16,7 +16,6 @@ package jakarta.ws.rs.core; -import java.io.StringWriter; import java.util.List; import java.util.Locale; import java.util.Objects; @@ -228,15 +227,14 @@ public boolean equals(final Object obj) { @Override public String toString() { - StringWriter w = new StringWriter(); - w.append("Variant[mediaType="); - w.append(mediaType == null ? "null" : mediaType.toString()); - w.append(", language="); - w.append(language == null ? "null" : language.toString()); - w.append(", encoding="); - w.append(encoding == null ? "null" : encoding); - w.append("]"); - return w.toString(); + StringBuilder sb = new StringBuilder("Variant[mediaType="); + sb.append(mediaType); + sb.append(", language="); + sb.append(language); + sb.append(", encoding="); + sb.append(encoding); + sb.append(']'); + return sb.toString(); } /** From 831482242adbf045afff18b2848289a0e763f80d Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 22 Dec 2024 12:30:35 +0000 Subject: [PATCH 2/3] Code Style: StringBuilder has fluent API --- .../main/java/jakarta/ws/rs/core/Variant.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java b/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java index c0b5627e8..a5460bfe2 100644 --- a/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java +++ b/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java @@ -227,14 +227,14 @@ public boolean equals(final Object obj) { @Override public String toString() { - StringBuilder sb = new StringBuilder("Variant[mediaType="); - sb.append(mediaType); - sb.append(", language="); - sb.append(language); - sb.append(", encoding="); - sb.append(encoding); - sb.append(']'); - return sb.toString(); + return new StringBuilder("Variant[mediaType=") + .append(mediaType) + .append(", language=") + .append(language) + .append(", encoding=") + .append(encoding) + .append(']') + .toString(); } /** From 382eb1bc2d6fa01c6f6168f275eb34d72ae2abb0 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 22 Dec 2024 13:05:56 +0000 Subject: [PATCH 3/3] Performance: String+ operation is faster than explicit StringBuilder.append() method --- .../src/main/java/jakarta/ws/rs/core/Variant.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java b/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java index a5460bfe2..663d7e4a5 100644 --- a/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java +++ b/jaxrs-api/src/main/java/jakarta/ws/rs/core/Variant.java @@ -227,14 +227,13 @@ public boolean equals(final Object obj) { @Override public String toString() { - return new StringBuilder("Variant[mediaType=") - .append(mediaType) - .append(", language=") - .append(language) - .append(", encoding=") - .append(encoding) - .append(']') - .toString(); + return "Variant[mediaType=" + + mediaType + + ", language=" + + language + + ", encoding=" + + encoding + + ']'; } /**