-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdapterDecorator.java
269 lines (207 loc) · 6.14 KB
/
AdapterDecorator.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
// Adapter decorator
package structural.decorator.adapter;
import java.util.stream.IntStream;
class MyStringBuilder
{
private StringBuilder sb;
public MyStringBuilder() {
sb = new StringBuilder();
}
public MyStringBuilder(String str)
{
sb = new StringBuilder(str);
}
/*
Fluent interface approach - extensible methods
Adapter: adapts concat method to our class
Decorator (below): aggregating StringBuilder class methods below
*/
public MyStringBuilder concat(String str)
{
return new MyStringBuilder(sb.toString().concat(str));
}
public MyStringBuilder appendLine(String str)
{
sb.append(str).append(System.lineSeparator());
return this;
}
public String toString()
{
return sb.toString();
}
// decorator part:
// delegated, but all signatures are wrong!
// vv
public MyStringBuilder append(Object obj) {
//return sb.append(obj);
sb.append(obj);
return this;
}
// vv
public MyStringBuilder append(String str) {
sb.append(str);
return this;
}
public StringBuilder append(StringBuffer sb) {
return this.sb.append(sb);
}
public StringBuilder append(CharSequence s) {
return sb.append(s);
}
public StringBuilder append(CharSequence s, int start, int end) {
return sb.append(s, start, end);
}
public StringBuilder append(char[] str) {
return sb.append(str);
}
public StringBuilder append(char[] str, int offset, int len) {
return sb.append(str, offset, len);
}
public StringBuilder append(boolean b) {
return sb.append(b);
}
public StringBuilder append(char c) {
return sb.append(c);
}
public StringBuilder append(int i) {
return sb.append(i);
}
public StringBuilder append(long lng) {
return sb.append(lng);
}
public StringBuilder append(float f) {
return sb.append(f);
}
public StringBuilder append(double d) {
return sb.append(d);
}
public StringBuilder appendCodePoint(int codePoint) {
return sb.appendCodePoint(codePoint);
}
public StringBuilder delete(int start, int end) {
return sb.delete(start, end);
}
public StringBuilder deleteCharAt(int index) {
return sb.deleteCharAt(index);
}
public StringBuilder replace(int start, int end, String str) {
return sb.replace(start, end, str);
}
public StringBuilder insert(int index, char[] str, int offset, int len) {
return sb.insert(index, str, offset, len);
}
public StringBuilder insert(int offset, Object obj) {
return sb.insert(offset, obj);
}
public StringBuilder insert(int offset, String str) {
return sb.insert(offset, str);
}
public StringBuilder insert(int offset, char[] str) {
return sb.insert(offset, str);
}
public StringBuilder insert(int dstOffset, CharSequence s) {
return sb.insert(dstOffset, s);
}
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end) {
return sb.insert(dstOffset, s, start, end);
}
public StringBuilder insert(int offset, boolean b) {
return sb.insert(offset, b);
}
public StringBuilder insert(int offset, char c) {
return sb.insert(offset, c);
}
public StringBuilder insert(int offset, int i) {
return sb.insert(offset, i);
}
public StringBuilder insert(int offset, long l) {
return sb.insert(offset, l);
}
public StringBuilder insert(int offset, float f) {
return sb.insert(offset, f);
}
public StringBuilder insert(int offset, double d) {
return sb.insert(offset, d);
}
public int indexOf(String str) {
return sb.indexOf(str);
}
public int indexOf(String str, int fromIndex) {
return sb.indexOf(str, fromIndex);
}
public int lastIndexOf(String str) {
return sb.lastIndexOf(str);
}
public int lastIndexOf(String str, int fromIndex) {
return sb.lastIndexOf(str, fromIndex);
}
public StringBuilder reverse() {
return sb.reverse();
}
public int length() {
return sb.length();
}
public int capacity() {
return sb.capacity();
}
public void ensureCapacity(int minimumCapacity) {
sb.ensureCapacity(minimumCapacity);
}
public void trimToSize() {
sb.trimToSize();
}
public void setLength(int newLength) {
sb.setLength(newLength);
}
public char charAt(int index) {
return sb.charAt(index);
}
public int codePointAt(int index) {
return sb.codePointAt(index);
}
public int codePointBefore(int index) {
return sb.codePointBefore(index);
}
public int codePointCount(int beginIndex, int endIndex) {
return sb.codePointCount(beginIndex, endIndex);
}
public int offsetByCodePoints(int index, int codePointOffset) {
return sb.offsetByCodePoints(index, codePointOffset);
}
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
sb.getChars(srcBegin, srcEnd, dst, dstBegin);
}
public void setCharAt(int index, char ch) {
sb.setCharAt(index, ch);
}
public String substring(int start) {
return sb.substring(start);
}
public CharSequence subSequence(int start, int end) {
return sb.subSequence(start, end);
}
public String substring(int start, int end) {
return sb.substring(start, end);
}
public IntStream chars() {
return sb.chars();
}
public IntStream codePoints() {
return sb.codePoints();
}
public boolean isEmpty() {
return sb.isEmpty();
}
public static int compare(CharSequence cs1, CharSequence cs2) {
return CharSequence.compare(cs1, cs2);
}
}
class AdapterDecoratorDemo
{
public static void main(String[] args)
{
MyStringBuilder msb = new MyStringBuilder();
msb.append("hello").appendLine(" world");
System.out.println(msb.concat("and this too"));
}
}