summaryrefslogtreecommitdiffstats
path: root/node-admin
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2022-01-28 17:48:47 +0100
committerMartin Polden <mpolden@mpolden.no>2022-01-28 17:48:47 +0100
commiteeed2728e76617b1b7503cc4070b9df2f4971a74 (patch)
treec05ad037b40e94317020dfeb9650626feb1c7f00 /node-admin
parentc58fe9e096554ec64a64060db4fc127f9d89f614 (diff)
Move SnippetGenerator to vespajlib
Diffstat (limited to 'node-admin')
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcessException.java2
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGenerator.java32
-rw-r--r--node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGeneratorTest.java59
3 files changed, 1 insertions, 92 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcessException.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcessException.java
index 8663aba4244..defaff78577 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcessException.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/ChildProcessException.java
@@ -2,7 +2,7 @@
package com.yahoo.vespa.hosted.node.admin.task.util.process;
-import com.yahoo.vespa.hosted.node.admin.task.util.text.SnippetGenerator;
+import com.yahoo.text.SnippetGenerator;
/**
* Base class for child process related exceptions, with a util to build an error message
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGenerator.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGenerator.java
deleted file mode 100644
index 61a7be25ec8..00000000000
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGenerator.java
+++ /dev/null
@@ -1,32 +0,0 @@
-// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.vespa.hosted.node.admin.task.util.text;
-
-/**
- * @author hakon
- */
-public class SnippetGenerator {
-
- private static final String OMIT_PREFIX = "[...";
- private static final String OMIT_SUFFIX = " chars omitted]";
- private static final int ASSUMED_OMIT_TEXT_LENGTH = OMIT_PREFIX.length() + 4 + OMIT_SUFFIX.length();
-
- /** Returns a snippet of approximate size. */
- public String makeSnippet(String text, int sizeHint) {
- if (text.length() <= Math.max(sizeHint, ASSUMED_OMIT_TEXT_LENGTH)) return text;
-
- int maxSuffixLength = Math.max(0, (sizeHint - ASSUMED_OMIT_TEXT_LENGTH) / 2);
- int maxPrefixLength = Math.max(0, sizeHint - ASSUMED_OMIT_TEXT_LENGTH - maxSuffixLength);
- String sizeString = Integer.toString(text.length() - maxPrefixLength - maxSuffixLength);
-
- // It would be silly to return a snippet when the full text is barely longer.
- // Note: Say ASSUMED_OMIT_TEXT_LENGTH=23: text will be returned whenever sizeHint<23 and text.length()<28.
- int snippetLength = maxPrefixLength + OMIT_PREFIX.length() + sizeString.length() + OMIT_SUFFIX.length() + maxSuffixLength;
- if (text.length() <= 1.05 * snippetLength + 5) return text;
-
- return text.substring(0, maxPrefixLength) +
- OMIT_PREFIX +
- sizeString +
- OMIT_SUFFIX +
- text.substring(text.length() - maxSuffixLength);
- }
-}
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGeneratorTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGeneratorTest.java
deleted file mode 100644
index a0b2df8783f..00000000000
--- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/text/SnippetGeneratorTest.java
+++ /dev/null
@@ -1,59 +0,0 @@
-// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.vespa.hosted.node.admin.task.util.text;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-/**
- * @author hakon
- */
-public class SnippetGeneratorTest {
- private final SnippetGenerator generator = new SnippetGenerator();
-
- private void assertSnippet(String text, int sizeHint, String expectedSnippet) {
- assertEquals(expectedSnippet, generator.makeSnippet(text, sizeHint));
- }
-
- @Test
- public void prefixSnippetForReallySmallSizeHint() {
- assertSnippet(
- "This is a long text that should be snippeted", 0,
- "[...44 chars omitted]");
-
- assertSnippet(
- "This is a long text that should be snippeted", 1,
- "[...44 chars omitted]");
- }
-
- @Test
- public void snippet() {
- assertSnippet(
- "This is a long text that should be snippeted", 23,
- "[...44 chars omitted]");
-
- assertSnippet(
- "This is a long text that should be snippeted", 24,
- "T[...43 chars omitted]");
-
- assertSnippet(
- "This is a long text that should be snippeted", 30,
- "This[...37 chars omitted]ted");
-
- }
-
- @Test
- public void noShorteningNeeded() {
- assertSnippet(
- "This is a long text that should be snippeted", 39,
- "This is [...28 chars omitted]nippeted");
-
- assertSnippet(
- "This is a long text that should be snippeted", 40,
- "This is a long text that should be snippeted");
-
- assertSnippet(
- "This is a long text that should be snippeted", 50,
- "This is a long text that should be snippeted");
- }
-} \ No newline at end of file