2e9446a5fc3edf67c153d0e68a9f5a6055f9a51a
[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     public String2 addSuffix(String separator, final String suffix) {
50         if (!isEmpty())
51             addSuffix(separator);
52
53         addSuffix(suffix);
54
55         return this;
56     }
57     /**
58      * Cut given amount of characters from the left of the string.
59      *
60      * @param cutAmount of characters to cut
61      * @return part that was cut.
62      */
63     public String2 trimPrefix(final int cutAmount) {
64
65         int actualCutAmount = cutAmount;
66
67         if (actualCutAmount > getLength())
68             actualCutAmount = getLength();
69
70         chars.subList(0, actualCutAmount).clear();
71
72         return this;
73     }
74
75     public String2 trimPrefixIfExists(String prefix){
76         if (prefix == null)
77             return this;
78
79         if (hasPrefix(prefix))
80             trimPrefix(prefix.length());
81
82         return this;
83     }
84
85     public String2 trimSuffixIfExists(String suffix){
86         if (hasSuffix(suffix))
87             trimSuffix(suffix.length());
88
89         return this;
90     }
91
92     public String2 trimSuffix(int charsToTrim) {
93
94         if (charsToTrim > chars.size()){
95             chars.clear();
96             return this;
97         }
98
99         for (int i = 0; i<charsToTrim; i++)
100             chars.remove(chars.size()-1);
101
102         return this;
103     }
104
105     public boolean hasSuffix(String suffix) {
106         return contains(suffix, getLength() - suffix.length());
107     }
108
109     public boolean hasPrefix(String prefix){
110         return contains(prefix, 0);
111     }
112
113     public boolean contains(String fragment, int index){
114         if (index + fragment.length() > chars.size())
115             return false;
116
117         for (int i = 0; i < fragment.length(); i++)
118             if (chars.get(index + i) != fragment.charAt(i))
119                 return false;
120
121         return true;
122     }
123
124     public String2 enforceLength(final int targetLength) {
125         if (getLength() > targetLength)
126             chars.subList(targetLength, getLength()).clear();
127         else if (getLength() < targetLength) {
128             final int charactersToAdd = targetLength - getLength();
129             for (int i = 0; i < charactersToAdd; i++)
130                 chars.add(' ');
131         }
132
133         return this;
134     }
135
136     public int getLength() {
137         return chars.size();
138     }
139
140     public String getSubString(final int startInclusive, final int endExclusive) {
141         final char[] charArray = new char[endExclusive - startInclusive];
142
143         int j = 0;
144         for (int i = startInclusive; i < endExclusive; i++) {
145             charArray[j] = chars.get(i);
146             j++;
147         }
148         return new String(charArray);
149     }
150
151     public boolean isEmpty() {
152         return chars.size() == 0;
153     }
154
155     @Override
156     public String toString() {
157         return getSubString(0, chars.size());
158     }
159 }