Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/tools/lib/builder/AbstractBuilder.java
41149 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
package builder;
24
25
import java.io.PrintWriter;
26
import java.io.StringWriter;
27
import java.util.Collections;
28
import java.util.List;
29
30
public abstract class AbstractBuilder {
31
32
final String name;
33
34
Modifiers modifiers;
35
Comment comments;
36
String cname;
37
38
/**
39
* Constructs the base builder.
40
* @param modifiers for the class
41
* @param name of the element
42
*/
43
public AbstractBuilder(Modifiers modifiers, String name) {
44
this.modifiers = modifiers;
45
this.name = name;
46
this.comments = new Comment(Comment.Kind.AUTO);
47
}
48
49
AbstractBuilder setModifiers(String... mods) {
50
this.modifiers = new Modifiers(mods);
51
return this;
52
}
53
54
/**
55
* Sets the enclosing type's name.
56
* @param className the enclosing type's name
57
*/
58
void setClassName(String className) {
59
this.cname = className;
60
}
61
62
/**
63
* Sets the comment type for the member.
64
* @param comment for the member.
65
* @return this builder.
66
*/
67
public AbstractBuilder setComments(Comment comment) {
68
this.comments = comment;
69
return this;
70
}
71
72
/**
73
* Sets the comments for the member.
74
* @param comments for the member.
75
* @return this builder.
76
*/
77
public AbstractBuilder setComments(String... comments) {
78
this.comments = new Comment(comments);
79
return this;
80
}
81
82
/**
83
* Sets a comment for the element. Typically used to set
84
* user's preferences whether an automatic comment is
85
* required or no API comment.
86
*
87
* @param kind of comment, automatic or no comment.
88
* @return this builder.
89
*/
90
public AbstractBuilder setComments(Comment.Kind kind) {
91
switch (kind) {
92
case NO_API_COMMENT: case AUTO: case INHERIT_DOC:
93
this.comments = new Comment(kind);
94
break;
95
default:
96
throw new IllegalArgumentException(kind + " not allowed");
97
}
98
return this;
99
}
100
101
/**
102
* The comment container.
103
*/
104
public static class Comment {
105
106
/**
107
* The kinds of a comment.
108
*/
109
public enum Kind {
110
/**
111
* user specified
112
*/
113
USER,
114
/**
115
* no API comments
116
*/
117
NO_API_COMMENT,
118
/**
119
* inserts the javadoc tag
120
*/
121
INHERIT_DOC,
122
/**
123
* auto generate one
124
*/
125
AUTO
126
}
127
128
final Kind kind;
129
final List<String> comments;
130
131
/**
132
* Construct an initial comment.
133
*
134
* @param kind
135
*/
136
public Comment(Kind kind) {
137
this.kind = kind;
138
comments = Collections.emptyList();
139
}
140
141
/**
142
* Specify a user comment.
143
*
144
* @param comments the string of API comments.
145
*/
146
public Comment(String... comments) {
147
kind = Kind.USER;
148
this.comments = comments == null
149
? Collections.emptyList()
150
: List.of(comments);
151
}
152
153
@Override
154
public String toString() {
155
ClassBuilder.OutputWriter ow = new ClassBuilder.OutputWriter();
156
switch (kind) {
157
case USER:
158
comments.forEach((s) -> ow.println(" " + s));
159
break;
160
case INHERIT_DOC:
161
ow.println("{@inheritDoc}");
162
break;
163
}
164
return ow.toString();
165
}
166
}
167
168
/**
169
* The modifier representation for an element.
170
*/
171
public static class Modifiers {
172
List<String> modifiers;
173
174
/**
175
* Constructs a modifier container.
176
* @param modifiers for an element.
177
*/
178
public Modifiers(String... modifiers) {
179
this.modifiers = List.of(modifiers);
180
}
181
182
/**
183
* Constructs a modifier container.
184
* @param modifiers a list of modifier strings.
185
*/
186
public Modifiers(List<String> modifiers) {
187
this.modifiers = modifiers;
188
}
189
190
/**
191
* Sets the modifiers for this element.
192
* @param modifiers
193
*/
194
public void setModifiers(String... modifiers) {
195
this.modifiers = List.of(modifiers);
196
}
197
198
/**
199
* Sets the modifiers for this element.
200
* @param modifiers
201
*/
202
public void setModifiers(List<String> modifiers) {
203
this.modifiers = modifiers;
204
}
205
206
@Override
207
public String toString() {
208
OutputWriter ow = new OutputWriter();
209
modifiers.forEach(i -> ow.print(i + " "));
210
return ow.toString();
211
}
212
}
213
214
/**
215
* The output writer.
216
*/
217
public static class OutputWriter {
218
private final StringWriter sw = new StringWriter();
219
private final PrintWriter pw = new PrintWriter(sw);
220
221
@Override
222
public String toString() {
223
return sw.getBuffer().toString();
224
}
225
226
/**
227
* Prints a string without NL.
228
* @param s the string to print.
229
*/
230
public void print(String s) {
231
pw.print(s);
232
}
233
234
/**
235
* Prints a string with a NL.
236
* @param s the string to print.
237
*/
238
public void println(String s) {
239
pw.println(s);
240
}
241
}
242
243
/**
244
* A container to encapsulate a pair of values.
245
*/
246
public static class Pair {
247
final String first;
248
final String second;
249
250
public Pair(String first, String second) {
251
this.first = first;
252
this.second = second;
253
}
254
}
255
}
256
257