Renamed CuttableString to more generic String2.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 14 Mar 2014 18:34:55 +0000 (20:34 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 14 Mar 2014 18:34:55 +0000 (20:34 +0200)
Added prefix, suffix support and length enforcing possibility.

src/main/java/eu/svjatoslav/commons/string/CuttableString.java [deleted file]
src/main/java/eu/svjatoslav/commons/string/String2.java [new file with mode: 0755]
src/test/java/eu/svjatoslav/commons/string/CuttableStringTest.java [deleted file]
src/test/java/eu/svjatoslav/commons/string/String2Test.java [new file with mode: 0755]

diff --git a/src/main/java/eu/svjatoslav/commons/string/CuttableString.java b/src/main/java/eu/svjatoslav/commons/string/CuttableString.java
deleted file mode 100755 (executable)
index 278673b..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.string;
-
-public class CuttableString {
-
-       private String value;
-
-       public CuttableString(final String value) {
-               this.value = value;
-       }
-
-       /**
-        * Cut given amount of characters from the left of the string. Return cutted
-        * part.
-        */
-       public String cutLeft(final int cutAmount) {
-
-               int actualCutAmount = cutAmount;
-
-               if (actualCutAmount > value.length())
-                       actualCutAmount = value.length();
-
-               final String result = value.substring(0, actualCutAmount);
-
-               value = value.substring(actualCutAmount);
-
-               return result;
-       }
-
-       public int getLength() {
-               return value.length();
-       };
-
-       public String getValue() {
-               return value;
-       }
-
-       public boolean isEmpty() {
-               return value.length() == 0;
-       }
-}
diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java
new file mode 100755 (executable)
index 0000000..fe83ade
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.commons.string;
+
+import java.util.Vector;
+
+public class String2 {
+
+       private final Vector<Character> chars = new Vector<Character>();
+
+       public String2(final String value) {
+               for (final Character c : value.toCharArray())
+                       chars.add(c);
+       }
+
+       public String2 addPrefix(final String prefix) {
+               int j = 0;
+               for (final char c : prefix.toCharArray())
+                       chars.insertElementAt(c, j++);
+
+               return this;
+       }
+
+       public String2 addSuffix(final String suffix) {
+               for (final char c : suffix.toCharArray())
+                       chars.add(c);
+
+               return this;
+       }
+
+       /**
+        * Cut given amount of characters from the left of the string. Return cutted
+        * part.
+        */
+       public String cutLeft(final int cutAmount) {
+
+               int actualCutAmount = cutAmount;
+
+               if (actualCutAmount > getLength())
+                       actualCutAmount = getLength();
+
+               final String result = getSubString(0, actualCutAmount);
+
+               chars.subList(0, actualCutAmount).clear();
+
+               return result;
+       }
+
+       public void enforceLength(final int targetLength) {
+               if (getLength() > targetLength)
+                       chars.subList(targetLength, getLength()).clear();
+               else if (getLength() < targetLength) {
+                       final int charactersToAdd = targetLength - getLength();
+                       for (int i = 0; i < charactersToAdd; i++)
+                               chars.add(' ');
+               }
+       }
+
+       public int getLength() {
+               return chars.size();
+       }
+
+       public String getSubString(final int startInclusive, final int endExclusive) {
+               final char[] charArray = new char[endExclusive - startInclusive];
+
+               int j = 0;
+               for (int i = startInclusive; i < endExclusive; i++) {
+                       charArray[j] = chars.get(i);
+                       j++;
+               }
+               return new String(charArray);
+       }
+
+       public boolean isEmpty() {
+               return chars.size() == 0;
+       }
+
+       @Override
+       public String toString() {
+               return getSubString(0, chars.size());
+       }
+}
diff --git a/src/test/java/eu/svjatoslav/commons/string/CuttableStringTest.java b/src/test/java/eu/svjatoslav/commons/string/CuttableStringTest.java
deleted file mode 100755 (executable)
index 6e9444d..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.string;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
-
-public class CuttableStringTest {
-
-       @Test
-       public void testCutLeft() {
-
-               final CuttableString s = new CuttableString("this is a test");
-
-               assertEquals("this ", s.cutLeft(5));
-               assertEquals("is ", s.cutLeft(3));
-               assertEquals("a ", s.cutLeft(2));
-               assertEquals("test", s.cutLeft(25));
-               assertEquals("", s.cutLeft(5));
-
-       }
-}
diff --git a/src/test/java/eu/svjatoslav/commons/string/String2Test.java b/src/test/java/eu/svjatoslav/commons/string/String2Test.java
new file mode 100755 (executable)
index 0000000..f13d0d2
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.commons.string;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class String2Test {
+
+       @Test
+       public void testCutLeft() {
+
+               final String2 s = new String2("this is a test");
+
+               assertEquals("this ", s.cutLeft(5));
+               assertEquals("is ", s.cutLeft(3));
+               assertEquals("a ", s.cutLeft(2));
+               assertEquals("test", s.cutLeft(25));
+               assertEquals("", s.cutLeft(5));
+
+       }
+
+       @Test
+       public void testEnforceLength() {
+               final String2 s = new String2("12345678");
+               s.enforceLength(3);
+               assertEquals("123", s.toString());
+
+               s.enforceLength(5);
+               assertEquals("123  ", s.toString());
+       }
+
+       @Test
+       public void testSuffexAndPrefix() {
+               final String2 s = new String2("experiment");
+               s.addPrefix("The ").addSuffix(" !");
+
+               assertEquals("The experiment !", s.toString());
+       }
+
+}