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