Simplified String2 constructor.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / String2.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2017, 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.ArrayList;
13 import java.util.List;
14
15 public class String2 {
16
17     private final List<Character> chars = new ArrayList<>();
18
19     public String2(String value) {
20         addSuffix(value);
21     }
22
23     public String2 addPrefix(final String prefix) {
24         if (prefix == null)
25             return this;
26
27         int i = 0;
28         for (final char c : prefix.toCharArray())
29             chars.add(i++, c);
30
31         return this;
32     }
33
34     public String2 addSuffix(final String suffix) {
35         if (suffix == null)
36             return this;
37
38         for (final char c : suffix.toCharArray())
39             chars.add(c);
40
41         return this;
42     }
43
44     public String2 addSuffix(String separator, final String suffix) {
45         if (!isEmpty())
46             addSuffix(separator);
47
48         addSuffix(suffix);
49
50         return this;
51     }
52     /**
53      * Cut given amount of characters from the left of the string.
54      *
55      * @param cutAmount of characters to cut
56      * @return part that was cut.
57      */
58     public String2 trimPrefix(final int cutAmount) {
59
60         int actualCutAmount = cutAmount;
61
62         if (actualCutAmount > getLength())
63             actualCutAmount = getLength();
64
65         chars.subList(0, actualCutAmount).clear();
66
67         return this;
68     }
69
70     public String2 trimPrefixIfExists(String prefix){
71         if (prefix == null)
72             return this;
73
74         if (hasPrefix(prefix))
75             trimPrefix(prefix.length());
76
77         return this;
78     }
79
80     public String2 trimSuffixIfExists(String suffix){
81         if (hasSuffix(suffix))
82             trimSuffix(suffix.length());
83
84         return this;
85     }
86
87     public String2 trimSuffix(int charsToTrim) {
88
89         if (charsToTrim > chars.size()){
90             chars.clear();
91             return this;
92         }
93
94         for (int i = 0; i<charsToTrim; i++)
95             chars.remove(chars.size()-1);
96
97         return this;
98     }
99
100     public boolean hasSuffix(String suffix) {
101         return contains(suffix, getLength() - suffix.length());
102     }
103
104     public boolean hasPrefix(String prefix){
105         return contains(prefix, 0);
106     }
107
108     public boolean contains(String fragment, int index){
109         if (index + fragment.length() > chars.size())
110             return false;
111
112         for (int i = 0; i < fragment.length(); i++)
113             if (chars.get(index + i) != fragment.charAt(i))
114                 return false;
115
116         return true;
117     }
118
119     public String2 enforceLength(final int targetLength) {
120         if (getLength() > targetLength)
121             chars.subList(targetLength, getLength()).clear();
122         else if (getLength() < targetLength) {
123             final int charactersToAdd = targetLength - getLength();
124             for (int i = 0; i < charactersToAdd; i++)
125                 chars.add(' ');
126         }
127
128         return this;
129     }
130
131     public int getLength() {
132         return chars.size();
133     }
134
135     public String getSubString(final int startInclusive, final int endExclusive) {
136         final char[] charArray = new char[endExclusive - startInclusive];
137
138         int j = 0;
139         for (int i = startInclusive; i < endExclusive; i++) {
140             charArray[j] = chars.get(i);
141             j++;
142         }
143         return new String(charArray);
144     }
145
146     public boolean isEmpty() {
147         return chars.size() == 0;
148     }
149
150     @Override
151     public String toString() {
152         return getSubString(0, chars.size());
153     }
154 }