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