improved javadoc
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / String2.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.string;
11
12 import java.util.Vector;
13
14 public class String2 {
15
16         private final Vector<Character> chars = new Vector<Character>();
17
18         public String2(final String value) {
19                 for (final Character c : value.toCharArray())
20                         chars.add(c);
21         }
22
23         public String2 addPrefix(final String prefix) {
24                 int j = 0;
25                 for (final char c : prefix.toCharArray())
26                         chars.insertElementAt(c, j++);
27
28                 return this;
29         }
30
31         public String2 addSuffix(final String suffix) {
32                 for (final char c : suffix.toCharArray())
33                         chars.add(c);
34
35                 return this;
36         }
37
38         /**
39          * Cut given amount of characters from the left of the string.
40          *
41          * @param cutAmount
42          *            of characters to cut
43          * @return cutted part.
44          */
45         public String cutLeft(final int cutAmount) {
46
47                 int actualCutAmount = cutAmount;
48
49                 if (actualCutAmount > getLength())
50                         actualCutAmount = getLength();
51
52                 final String result = getSubString(0, actualCutAmount);
53
54                 chars.subList(0, actualCutAmount).clear();
55
56                 return result;
57         }
58
59         public String2 enforceLength(final int targetLength) {
60                 if (getLength() > targetLength)
61                         chars.subList(targetLength, getLength()).clear();
62                 else if (getLength() < targetLength) {
63                         final int charactersToAdd = targetLength - getLength();
64                         for (int i = 0; i < charactersToAdd; i++)
65                                 chars.add(' ');
66                 }
67
68                 return this;
69         }
70
71         public int getLength() {
72                 return chars.size();
73         }
74
75         public String getSubString(final int startInclusive, final int endExclusive) {
76                 final char[] charArray = new char[endExclusive - startInclusive];
77
78                 int j = 0;
79                 for (int i = startInclusive; i < endExclusive; i++) {
80                         charArray[j] = chars.get(i);
81                         j++;
82                 }
83                 return new String(charArray);
84         }
85
86         public boolean isEmpty() {
87                 return chars.size() == 0;
88         }
89
90         @Override
91         public String toString() {
92                 return getSubString(0, chars.size());
93         }
94 }