forked from KyoriPowered/adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractComponentBuilder.java
314 lines (285 loc) · 10.2 KB
/
AbstractComponentBuilder.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
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static java.util.Objects.requireNonNull;
/**
* An abstract implementation of a component builder.
*
* @param <C> the component type
* @param <B> the builder type
*/
abstract class AbstractComponentBuilder<C extends BuildableComponent<C, B>, B extends ComponentBuilder<C, B>> implements ComponentBuilder<C, B> {
// We use an empty list by default to prevent unnecessary list creation for components with no children
protected List<Component> children = Collections.emptyList();
/*
* We maintain two separate fields here - a style, and style builder. If we're creating this component builder from
* another component, or someone provides a style via style(Style), then we don't need a builder - unless someone later
* calls one of the style modification methods in this builder, at which time we'll convert 'style' to a style builder.
*/
private @Nullable Style style;
private Style.@Nullable Builder styleBuilder;
protected AbstractComponentBuilder() {
}
protected AbstractComponentBuilder(final @NotNull C component) {
final List<Component> children = component.children();
if (!children.isEmpty()) {
this.children = new ArrayList<>(children);
}
if (component.hasStyling()) {
this.style = component.style();
}
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B append(final @NotNull Component component) {
if (component == Component.empty()) return (B) this;
this.prepareChildren();
this.children.add(requireNonNull(component, "component"));
return (B) this;
}
@Override
public @NotNull B append(final @NotNull Component@NotNull... components) {
return this.append((ComponentLike[]) components);
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B append(final @NotNull ComponentLike@NotNull... components) {
requireNonNull(components, "components");
boolean prepared = false;
for (int i = 0, length = components.length; i < length; i++) {
final Component component = requireNonNull(components[i], "components[?]").asComponent();
if (component != Component.empty()) {
if (!prepared) {
this.prepareChildren();
prepared = true;
}
this.children.add(requireNonNull(component, "components[?]"));
}
}
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B append(final @NotNull Iterable<? extends ComponentLike> components) {
requireNonNull(components, "components");
boolean prepared = false;
for (final ComponentLike like : components) {
final Component component = requireNonNull(like, "components[?]").asComponent();
if (component != Component.empty()) {
if (!prepared) {
this.prepareChildren();
prepared = true;
}
this.children.add(requireNonNull(component, "components[?]"));
}
}
return (B) this;
}
private void prepareChildren() {
if (this.children == Collections.<Component>emptyList()) {
this.children = new ArrayList<>();
}
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B applyDeep(final @NotNull Consumer<? super ComponentBuilder<?, ?>> consumer) {
this.apply(consumer);
if (this.children == Collections.<Component>emptyList()) {
return (B) this;
}
final ListIterator<Component> it = this.children.listIterator();
while (it.hasNext()) {
final Component child = it.next();
if (!(child instanceof BuildableComponent<?, ?>)) {
continue;
}
final ComponentBuilder<?, ?> childBuilder = ((BuildableComponent<?, ?>) child).toBuilder();
childBuilder.applyDeep(consumer);
it.set(childBuilder.build());
}
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B mapChildren(final @NotNull Function<BuildableComponent<?, ?>, ? extends BuildableComponent<?, ?>> function) {
if (this.children == Collections.<Component>emptyList()) {
return (B) this;
}
final ListIterator<Component> it = this.children.listIterator();
while (it.hasNext()) {
final Component child = it.next();
if (!(child instanceof BuildableComponent<?, ?>)) {
continue;
}
final BuildableComponent<?, ?> mappedChild = requireNonNull(function.apply((BuildableComponent<?, ?>) child), "mappedChild");
if (child == mappedChild) {
continue;
}
it.set(mappedChild);
}
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B mapChildrenDeep(final @NotNull Function<BuildableComponent<?, ?>, ? extends BuildableComponent<?, ?>> function) {
if (this.children == Collections.<Component>emptyList()) {
return (B) this;
}
final ListIterator<Component> it = this.children.listIterator();
while (it.hasNext()) {
final Component child = it.next();
if (!(child instanceof BuildableComponent<?, ?>)) {
continue;
}
final BuildableComponent<?, ?> mappedChild = requireNonNull(function.apply((BuildableComponent<?, ?>) child), "mappedChild");
if (mappedChild.children().isEmpty()) {
if (child == mappedChild) {
continue;
}
it.set(mappedChild);
} else {
final ComponentBuilder<?, ?> builder = mappedChild.toBuilder();
builder.mapChildrenDeep(function);
it.set(builder.build());
}
}
return (B) this;
}
@Override
public @NotNull List<Component> children() {
return Collections.unmodifiableList(this.children);
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B style(final @NotNull Style style) {
this.style = style;
this.styleBuilder = null;
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B style(final @NotNull Consumer<Style.Builder> consumer) {
consumer.accept(this.styleBuilder());
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B font(final @Nullable Key font) {
this.styleBuilder().font(font);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B color(final @Nullable TextColor color) {
this.styleBuilder().color(color);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B colorIfAbsent(final @Nullable TextColor color) {
this.styleBuilder().colorIfAbsent(color);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) {
this.styleBuilder().decoration(decoration, state);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B decorationIfAbsent(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) {
this.styleBuilder().decorationIfAbsent(decoration, state);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B clickEvent(final @Nullable ClickEvent event) {
this.styleBuilder().clickEvent(event);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B hoverEvent(final @Nullable HoverEventSource<?> source) {
this.styleBuilder().hoverEvent(source);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B insertion(final @Nullable String insertion) {
this.styleBuilder().insertion(insertion);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B mergeStyle(final @NotNull Component that, final @NotNull Set<Style.Merge> merges) {
this.styleBuilder().merge(requireNonNull(that, "component").style(), merges);
return (B) this;
}
@Override
@SuppressWarnings("unchecked")
public @NotNull B resetStyle() {
this.style = null;
this.styleBuilder = null;
return (B) this;
}
private Style.@NotNull Builder styleBuilder() {
if (this.styleBuilder == null) {
if (this.style != null) {
this.styleBuilder = this.style.toBuilder();
this.style = null;
} else {
this.styleBuilder = Style.style();
}
}
return this.styleBuilder;
}
protected final boolean hasStyle() {
return this.styleBuilder != null || this.style != null;
}
protected @NotNull Style buildStyle() {
if (this.styleBuilder != null) {
return this.styleBuilder.build();
} else if (this.style != null) {
return this.style;
} else {
return Style.empty();
}
}
}