Convenience method for splitting string into groups based on regular expression
[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 repeat(int count){
21         String value = toString();
22
23         for (int i = 1; i < count; i++) {
24             addSuffix(value);
25         }
26         return this;
27     }
28
29     public String2 addPrefix(final String prefix) {
30         if (prefix == null)
31             return this;
32
33         int i = 0;
34         for (final char c : prefix.toCharArray())
35             chars.add(i++, c);
36
37         return this;
38     }
39
40     public String2 addSuffix(final String suffix) {
41         if (suffix == null)
42             return this;
43
44         for (final char c : suffix.toCharArray())
45             chars.add(c);
46
47         return this;
48     }
49
50     public String2 addSuffix(String separator, final String suffix) {
51         if (!isEmpty())
52             addSuffix(separator);
53
54         addSuffix(suffix);
55
56         return this;
57     }
58
59     /**
60      * Cut given amount of characters from the left of the string.
61      *
62      * @param cutAmount of characters to cut
63      * @return part that was cut.
64      */
65     public String2 trimPrefix(final int cutAmount) {
66
67         int actualCutAmount = cutAmount;
68
69         if (actualCutAmount > getLength())
70             actualCutAmount = getLength();
71
72         chars.subList(0, actualCutAmount).clear();
73
74         return this;
75     }
76
77     public String2 trimPrefixIfExists(String prefix) {
78         if (prefix == null)
79             return this;
80
81         if (hasPrefix(prefix))
82             trimPrefix(prefix.length());
83
84         return this;
85     }
86
87     public String2 trimSuffixIfExists(String suffix) {
88         if (hasSuffix(suffix))
89             trimSuffix(suffix.length());
90
91         return this;
92     }
93
94     public String2 trimSuffix(int charsToTrim) {
95
96         if (charsToTrim > chars.size()) {
97             chars.clear();
98             return this;
99         }
100
101         for (int i = 0; i < charsToTrim; i++)
102             chars.remove(chars.size() - 1);
103
104         return this;
105     }
106
107     public boolean hasSuffix(String suffix) {
108         return contains(suffix, getLength() - suffix.length());
109     }
110
111     public boolean hasPrefix(String prefix) {
112         return contains(prefix, 0);
113     }
114
115     public boolean contains(String fragment, int index) {
116         if (index + fragment.length() > chars.size())
117             return false;
118
119         for (int i = 0; i < fragment.length(); i++)
120             if (chars.get(index + i) != fragment.charAt(i))
121                 return false;
122
123         return true;
124     }
125
126     public String2 enforceLength(final int targetLength) {
127         if (getLength() > targetLength)
128             chars.subList(targetLength, getLength()).clear();
129         else if (getLength() < targetLength) {
130             final int charactersToAdd = targetLength - getLength();
131             for (int i = 0; i < charactersToAdd; i++)
132                 chars.add(' ');
133         }
134
135         return this;
136     }
137
138     public int getLength() {
139         return chars.size();
140     }
141
142     public String getSubString(final int startInclusive, final int endExclusive) {
143         final char[] charArray = new char[endExclusive - startInclusive];
144
145         int j = 0;
146         for (int i = startInclusive; i < endExclusive; i++) {
147             charArray[j] = chars.get(i);
148             j++;
149         }
150         return new String(charArray);
151     }
152
153     public boolean isEmpty() {
154         return chars.size() == 0;
155     }
156
157     @Override
158     public String toString() {
159         return getSubString(0, chars.size());
160     }
161
162
163     public static String[] getGroups(String s, String regexp){
164         Pattern pattern = Pattern.compile(regexp);
165         Matcher matcher = pattern.matcher(s);
166
167         matcher.find();
168         String[] result = new String[matcher.groupCount()];
169
170         for (int i = 0; i< result.length; i++){
171             result[i] = matcher.group(i+1);
172         }
173
174         return result;
175     }
176 }