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