From 5cb739fecf46c9720fbdcb093364d55055d0d1f6 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 23 Aug 2020 06:39:03 +0300 Subject: [PATCH] Use append/prepend instead of addSuffix/addPrefix --- .../eu/svjatoslav/commons/string/String2.java | 30 ++++++++++++------- .../commons/string/String2Test.java | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index 07e3fdb..8d7273d 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -13,23 +13,31 @@ public class String2 { private final List chars = new ArrayList<>(); + public String2 clear(){ + chars.clear(); + return this; + } + public String2(String value) { - addSuffix(value); + append(value); } public String2() { } public String2 repeat(int count){ - String value = toString(); + if (count > 0) { + String value = toString(); - for (int i = 1; i < count; i++) { - addSuffix(value); + for (int i = 1; i < count; i++) + append(value); + } else { + clear(); } return this; } - public String2 addPrefix(final String prefix) { + public String2 prepend(final String prefix) { if (prefix == null) return this; @@ -40,7 +48,7 @@ public class String2 { return this; } - public String2 addSuffix(final String suffix) { + public String2 append(final String suffix) { if (suffix == null) return this; @@ -50,17 +58,17 @@ public class String2 { return this; } - public String2 addSuffix(String separator, final String suffix) { + public String2 appendWithSeparator(String separator, final String suffix) { if (!isEmpty()) - addSuffix(separator); + append(separator); - addSuffix(suffix); + append(suffix); return this; } - public String2 addSuffix(String s, int times) { - for (int i = 0; i < times; i++) addSuffix(s); + public String2 append(String s, int times) { + for (int i = 0; i < times; i++) append(s); return this; } diff --git a/src/test/java/eu/svjatoslav/commons/string/String2Test.java b/src/test/java/eu/svjatoslav/commons/string/String2Test.java index f254980..be1837f 100755 --- a/src/test/java/eu/svjatoslav/commons/string/String2Test.java +++ b/src/test/java/eu/svjatoslav/commons/string/String2Test.java @@ -46,7 +46,7 @@ public class String2Test { @Test public void testSuffixAndPrefix() { final String2 s = new String2("experiment"); - s.addPrefix("The ").addSuffix(" !"); + s.prepend("The ").append(" !"); assertEquals("The experiment !", s.toString()); } -- 2.20.1