-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringDecorator.java
367 lines (278 loc) · 8.89 KB
/
StringDecorator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// String Decorator - add functionality to String class
package structural.decorator;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class MagicString
{
private String string;
public MagicString(String string) {
this.string = string;
}
public long getNumberOfVowels()
{
return string.chars()
.mapToObj(c -> (char)c)
.filter(c -> "aeiou".contains(c.toString()))
.count();
}
@Override
public String toString() {
return string;
}
///////////////////////////////
public int length() {
return string.length();
}
public boolean isEmpty() {
return string.isEmpty();
}
public char charAt(int index) {
return string.charAt(index);
}
public int codePointAt(int index) {
return string.codePointAt(index);
}
public int codePointBefore(int index) {
return string.codePointBefore(index);
}
public int codePointCount(int beginIndex, int endIndex) {
return string.codePointCount(beginIndex, endIndex);
}
public int offsetByCodePoints(int index, int codePointOffset) {
return string.offsetByCodePoints(index, codePointOffset);
}
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
string.getChars(srcBegin, srcEnd, dst, dstBegin);
}
@Deprecated(since = "1.1")
public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) {
string.getBytes(srcBegin, srcEnd, dst, dstBegin);
}
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException {
return string.getBytes(charsetName);
}
public byte[] getBytes(Charset charset) {
return string.getBytes(charset);
}
public byte[] getBytes() {
return string.getBytes();
}
public boolean contentEquals(StringBuffer sb) {
return string.contentEquals(sb);
}
public boolean contentEquals(CharSequence cs) {
return string.contentEquals(cs);
}
public boolean equalsIgnoreCase(String anotherString) {
return string.equalsIgnoreCase(anotherString);
}
public int compareTo(String anotherString) {
return string.compareTo(anotherString);
}
public int compareToIgnoreCase(String str) {
return string.compareToIgnoreCase(str);
}
public boolean regionMatches(int toffset, String other, int ooffset, int len) {
return string.regionMatches(toffset, other, ooffset, len);
}
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) {
return string.regionMatches(ignoreCase, toffset, other, ooffset, len);
}
public boolean startsWith(String prefix, int toffset) {
return string.startsWith(prefix, toffset);
}
public boolean startsWith(String prefix) {
return string.startsWith(prefix);
}
public boolean endsWith(String suffix) {
return string.endsWith(suffix);
}
public int indexOf(int ch) {
return string.indexOf(ch);
}
public int indexOf(int ch, int fromIndex) {
return string.indexOf(ch, fromIndex);
}
public int lastIndexOf(int ch) {
return string.lastIndexOf(ch);
}
public int lastIndexOf(int ch, int fromIndex) {
return string.lastIndexOf(ch, fromIndex);
}
public int indexOf(String str) {
return string.indexOf(str);
}
public int indexOf(String str, int fromIndex) {
return string.indexOf(str, fromIndex);
}
public int lastIndexOf(String str) {
return string.lastIndexOf(str);
}
public int lastIndexOf(String str, int fromIndex) {
return string.lastIndexOf(str, fromIndex);
}
public String substring(int beginIndex) {
return string.substring(beginIndex);
}
public String substring(int beginIndex, int endIndex) {
return string.substring(beginIndex, endIndex);
}
public CharSequence subSequence(int beginIndex, int endIndex) {
return string.subSequence(beginIndex, endIndex);
}
public String concat(String str) {
return string.concat(str);
}
public String replace(char oldChar, char newChar) {
return string.replace(oldChar, newChar);
}
public boolean matches(String regex) {
return string.matches(regex);
}
public boolean contains(CharSequence s) {
return string.contains(s);
}
public String replaceFirst(String regex, String replacement) {
return string.replaceFirst(regex, replacement);
}
public String replaceAll(String regex, String replacement) {
return string.replaceAll(regex, replacement);
}
public String replace(CharSequence target, CharSequence replacement) {
return string.replace(target, replacement);
}
public String[] split(String regex, int limit) {
return string.split(regex, limit);
}
public String[] split(String regex) {
return string.split(regex);
}
public static String join(CharSequence delimiter, CharSequence... elements) {
return String.join(delimiter, elements);
}
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
return String.join(delimiter, elements);
}
public String toLowerCase(Locale locale) {
return string.toLowerCase(locale);
}
public String toLowerCase() {
return string.toLowerCase();
}
public String toUpperCase(Locale locale) {
return string.toUpperCase(locale);
}
public String toUpperCase() {
return string.toUpperCase();
}
public String trim() {
return string.trim();
}
public String strip() {
return string.strip();
}
public String stripLeading() {
return string.stripLeading();
}
public String stripTrailing() {
return string.stripTrailing();
}
public boolean isBlank() {
return string.isBlank();
}
public Stream<String> lines() {
return string.lines();
}
public String indent(int n) {
return string.indent(n);
}
public String stripIndent() {
return string.stripIndent();
}
public String translateEscapes() {
return string.translateEscapes();
}
public <R> R transform(Function<? super String, ? extends R> f) {
return string.transform(f);
}
public IntStream chars() {
return string.chars();
}
public IntStream codePoints() {
return string.codePoints();
}
public char[] toCharArray() {
return string.toCharArray();
}
public static String format(String format, Object... args) {
return String.format(format, args);
}
public static String format(Locale l, String format, Object... args) {
return String.format(l, format, args);
}
public String formatted(Object... args) {
return string.formatted(args);
}
public static String valueOf(Object obj) {
return String.valueOf(obj);
}
public static String valueOf(char[] data) {
return String.valueOf(data);
}
public static String valueOf(char[] data, int offset, int count) {
return String.valueOf(data, offset, count);
}
public static String copyValueOf(char[] data, int offset, int count) {
return String.copyValueOf(data, offset, count);
}
public static String copyValueOf(char[] data) {
return String.copyValueOf(data);
}
public static String valueOf(boolean b) {
return String.valueOf(b);
}
public static String valueOf(char c) {
return String.valueOf(c);
}
public static String valueOf(int i) {
return String.valueOf(i);
}
public static String valueOf(long l) {
return String.valueOf(l);
}
public static String valueOf(float f) {
return String.valueOf(f);
}
public static String valueOf(double d) {
return String.valueOf(d);
}
public String intern() {
return string.intern();
}
public String repeat(int count) {
return string.repeat(count);
}
public Optional<String> describeConstable() {
return string.describeConstable();
}
public String resolveConstantDesc(MethodHandles.Lookup lookup) {
return string.resolveConstantDesc(lookup);
}
public static int compare(CharSequence cs1, CharSequence cs2) {
return CharSequence.compare(cs1, cs2);
}
}
class StringDecoratorDemo
{
public static void main(String[] args) {
MagicString s = new MagicString("hello");
System.out.println(s + " has "
+ s.getNumberOfVowels() + " vowels.");
}
}