Helper function to split string into groups based on regexp. Possibility to retrieve...
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / String2.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.string;
6
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11
12 public class String2 {
13
14     private final List<Character> chars = new ArrayList<>();
15
16     public String2(String value) {
17         addSuffix(value);
18     }
19
20     public String2() {
21     }
22
23     public String2 repeat(int count){
24         String value = toString();
25
26         for (int i = 1; i < count; i++) {
27             addSuffix(value);
28         }
29         return this;
30     }
31
32     public String2 addPrefix(final String prefix) {
33         if (prefix == null)
34             return this;
35
36         int i = 0;
37         for (final char c : prefix.toCharArray())
38             chars.add(i++, c);
39
40         return this;
41     }
42
43     public String2 addSuffix(final String suffix) {
44         if (suffix == null)
45             return this;
46
47         for (final char c : suffix.toCharArray())
48             chars.add(c);
49
50         return this;
51     }
52
53     public String2 addSuffix(String separator, final String suffix) {
54         if (!isEmpty())
55             addSuffix(separator);
56
57         addSuffix(suffix);
58
59         return this;
60     }
61
62     public String2 addSuffix(String s, int times) {
63         for (int i = 0; i < times; i++) addSuffix(s);
64         return this;
65     }
66
67
68     /**
69      * Cut given amount of characters from the left of the string.
70      *
71      * @param cutAmount of characters to cut
72      * @return part that was cut.
73      */
74     public String2 trimPrefix(final int cutAmount) {
75
76         int actualCutAmount = cutAmount;
77
78         if (actualCutAmount > getLength())
79             actualCutAmount = getLength();
80
81         chars.subList(0, actualCutAmount).clear();
82
83         return this;
84     }
85
86     public String2 trimPrefixIfExists(String prefix) {
87         if (prefix == null)
88             return this;
89
90         if (hasPrefix(prefix))
91             trimPrefix(prefix.length());
92
93         return this;
94     }
95
96     public String2 trimSuffixIfExists(String suffix) {
97         if (hasSuffix(suffix))
98             trimSuffix(suffix.length());
99
100         return this;
101     }
102
103     public String2 trimSuffix(int charsToTrim) {
104
105         if (charsToTrim > chars.size()) {
106             chars.clear();
107             return this;
108         }
109
110         for (int i = 0; i < charsToTrim; i++)
111             chars.remove(chars.size() - 1);
112
113         return this;
114     }
115
116     public boolean hasSuffix(String suffix) {
117         return contains(suffix, getLength() - suffix.length());
118     }
119
120     public boolean hasPrefix(String prefix) {
121         return contains(prefix, 0);
122     }
123
124     public boolean contains(String fragment, int index) {
125         if (index + fragment.length() > chars.size())
126             return false;
127
128         for (int i = 0; i < fragment.length(); i++)
129             if (chars.get(index + i) != fragment.charAt(i))
130                 return false;
131
132         return true;
133     }
134
135     public String2 enforceLength(final int targetLength) {
136         if (getLength() > targetLength)
137             chars.subList(targetLength, getLength()).clear();
138         else if (getLength() < targetLength) {
139             final int charactersToAdd = targetLength - getLength();
140             for (int i = 0; i < charactersToAdd; i++)
141                 chars.add(' ');
142         }
143
144         return this;
145     }
146
147     public int getLength() {
148         return chars.size();
149     }
150
151     public String getSubString(final int startInclusive, final int endExclusive) {
152         final char[] charArray = new char[endExclusive - startInclusive];
153
154         int j = 0;
155         for (int i = startInclusive; i < endExclusive; i++) {
156             charArray[j] = chars.get(i);
157             j++;
158         }
159         return new String(charArray);
160     }
161
162     public boolean isEmpty() {
163         return chars.size() == 0;
164     }
165
166     @Override
167     public String toString() {
168         return getSubString(0, chars.size());
169     }
170
171
172     public static String[] getGroups(String s, String regexp){
173         Pattern pattern = Pattern.compile(regexp);
174         Matcher matcher = pattern.matcher(s);
175
176         matcher.find();
177         String[] result = new String[matcher.groupCount()];
178
179         for (int i = 0; i< result.length; i++){
180             result[i] = matcher.group(i+1);
181         }
182
183         return result;
184     }
185 }