From 07132e995da940da298dd23f12f19e3b79b74b85 Mon Sep 17 00:00:00 2001 From: Bill Collins Date: Wed, 3 Mar 2021 15:44:07 +0000 Subject: [PATCH] Review feedback --- .../plugins/checks/api/TruncatedString.java | 22 ++++++++++++++++--- .../checks/api/TruncatedStringTest.java | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/checks/api/TruncatedString.java b/src/main/java/io/jenkins/plugins/checks/api/TruncatedString.java index a1227aa9..c1b45d2a 100644 --- a/src/main/java/io/jenkins/plugins/checks/api/TruncatedString.java +++ b/src/main/java/io/jenkins/plugins/checks/api/TruncatedString.java @@ -68,20 +68,36 @@ private List getChunks() { * @param maxSize the maximum size of the resultant string. * @return A string comprising as many of the joined chunks that will fit in the given size, plus the truncation * string if truncation was necessary. + * @deprecated use the explicit {@link #buildByBytes(int)} or {@link #buildByChars(int)} method according ot your requirements. */ + @Deprecated public String build(final int maxSize) { return build(maxSize, false); } /** - * Builds the string such that it does not exceed maxSize in bytes or chars, including the truncation string. + * Builds the string such that it does not exceed maxSize in bytes, including the truncation string. + * + * @param maxSize the maximum size of the resultant string. + * @return A string comprising as many of the joined chunks that will fit in the given size, plus the truncation + * string if truncation was necessary. + */ + public String buildByBytes(final int maxSize) { + return build(maxSize, false); + } + + /** + * Builds the string such that it does not exceed maxSize in chars, including the truncation string. * * @param maxSize the maximum size of the resultant string. - * @param chunkOnChars calculate length of strings as chars instead of bytes * @return A string comprising as many of the joined chunks that will fit in the given size, plus the truncation * string if truncation was necessary. */ - public String build(final int maxSize, final boolean chunkOnChars) { + public String buildByChars(final int maxSize) { + return build(maxSize, true); + } + + String build(final int maxSize, final boolean chunkOnChars) { List parts = getChunks(); if (truncateStart) { Collections.reverse(parts); diff --git a/src/test/java/io/jenkins/plugins/checks/api/TruncatedStringTest.java b/src/test/java/io/jenkins/plugins/checks/api/TruncatedStringTest.java index 39c655c1..4ebaab5e 100644 --- a/src/test/java/io/jenkins/plugins/checks/api/TruncatedStringTest.java +++ b/src/test/java/io/jenkins/plugins/checks/api/TruncatedStringTest.java @@ -131,7 +131,7 @@ public void shouldChunkNewlinesDifferently() { } @Test - public void shouldTruncateToBytesNotChars() { + public void shouldTruncateByBytesOrChars() { TruncatedString.Builder builder = getBuilder(); builder.addText("☃☃☃\n"); // 3 + 1 assertThat(builder.build().toString().length()).isEqualTo(4); @@ -145,7 +145,7 @@ public void shouldTruncateToBytesNotChars() { } @Test - public void shouldHandleLongCharsInTruncationTest() { + public void shouldHandleLongCharsInTruncationText() { String truncationText = "E_TOO_MUCH_☃"; assertThat(truncationText.length()).isEqualTo(12); assertThat(truncationText.getBytes(StandardCharsets.UTF_8).length).isEqualTo(14);