/*
* Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright ©2012-2017, 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
public class String2 {
- private final Vector<Character> chars = new Vector<Character>();
+ private final Vector<Character> chars = new Vector<>();
- public String2(final String value) {
- for (final Character c : value.toCharArray())
- chars.add(c);
- }
+ 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++);
+ public String2 addPrefix(final String prefix) {
+ int j = 0;
+ for (final char c : prefix.toCharArray())
+ chars.insertElementAt(c, j++);
- return this;
- }
+ return this;
+ }
- public String2 addSuffix(final String suffix) {
- for (final char c : suffix.toCharArray())
- chars.add(c);
+ public String2 addSuffix(final String suffix) {
+ for (final char c : suffix.toCharArray())
+ chars.add(c);
- return this;
- }
+ return this;
+ }
- /**
- * Cut given amount of characters from the left of the string.
- *
- * @param cutAmount
- * of characters to cut
- * @return cutted part.
- */
- public String cutLeft(final int cutAmount) {
+ /**
+ * Cut given amount of characters from the left of the string.
+ *
+ * @param cutAmount of characters to cut
+ * @return part that was cut.
+ */
+ public String trimLeft(final int cutAmount) {
- int actualCutAmount = cutAmount;
+ int actualCutAmount = cutAmount;
- if (actualCutAmount > getLength())
- actualCutAmount = getLength();
+ if (actualCutAmount > getLength())
+ actualCutAmount = getLength();
- final String result = getSubString(0, actualCutAmount);
+ final String result = getSubString(0, actualCutAmount);
- chars.subList(0, actualCutAmount).clear();
+ chars.subList(0, actualCutAmount).clear();
- return result;
- }
+ return result;
+ }
- public String2 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 String2 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(' ');
+ }
- return this;
- }
+ return this;
+ }
- public int getLength() {
- return chars.size();
- }
+ public int getLength() {
+ return chars.size();
+ }
- public String getSubString(final int startInclusive, final int endExclusive) {
- final char[] charArray = new char[endExclusive - startInclusive];
+ 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);
- }
+ 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;
- }
+ public boolean isEmpty() {
+ return chars.size() == 0;
+ }
- @Override
- public String toString() {
- return getSubString(0, chars.size());
- }
+ @Override
+ public String toString() {
+ return getSubString(0, chars.size());
+ }
}