-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
AbstractComponent.java
128 lines (113 loc) · 4.55 KB
/
AbstractComponent.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
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 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.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.util.Buildable;
import net.kyori.examination.ExaminableProperty;
import net.kyori.examination.string.StringExaminer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.Debug;
import static java.util.Objects.requireNonNull;
/**
* An abstract implementation of a text component.
*
* @since 4.0.0
*/
@Debug.Renderer(text = "this.debuggerString()", childrenArray = "this.children().toArray()", hasChildren = "!this.children().isEmpty()")
public abstract class AbstractComponent implements Component {
private static final Predicate<Component> NOT_EMPTY = component -> component != Component.empty();
/**
* The list of children.
*/
protected final List<Component> children;
/**
* The style of this component.
*/
protected final Style style;
protected AbstractComponent(final @NonNull List<? extends ComponentLike> children, final @NonNull Style style) {
this.children = ComponentLike.asComponents(children, NOT_EMPTY);
this.style = style;
}
@Override
public final @NonNull List<Component> children() {
return this.children;
}
@Override
public final @NonNull Style style() {
return this.style;
}
@Override
public @NonNull Component replaceText(final @NonNull Consumer<TextReplacementConfig.Builder> configurer) {
requireNonNull(configurer, "configurer");
return this.replaceText(Buildable.configureAndBuild(TextReplacementConfig.builder(), configurer));
}
@Override
public @NonNull Component replaceText(final @NonNull TextReplacementConfig config) {
requireNonNull(config, "replacement");
if(!(config instanceof TextReplacementConfigImpl)) {
throw new IllegalArgumentException("Provided replacement was a custom TextReplacementConfig implementation, which is not supported.");
}
return TextReplacementRenderer.INSTANCE.render(this, ((TextReplacementConfigImpl) config).createState());
}
@Override
public boolean equals(final @Nullable Object other) {
if(this == other) return true;
if(!(other instanceof AbstractComponent)) return false;
final AbstractComponent that = (AbstractComponent) other;
return Objects.equals(this.children, that.children)
&& Objects.equals(this.style, that.style);
}
@Override
public int hashCode() {
int result = this.children.hashCode();
result = (31 * result) + this.style.hashCode();
return result;
}
@SuppressWarnings("unused")
private String debuggerString() {
return StringExaminer.simpleEscaping().examine(this.examinableName(), this.examinablePropertiesWithoutChildren());
}
protected Stream<? extends ExaminableProperty> examinablePropertiesWithoutChildren() {
return Stream.of(ExaminableProperty.of("style", this.style));
}
@Override
public @NonNull Stream<? extends ExaminableProperty> examinableProperties() {
return Stream.concat(
this.examinablePropertiesWithoutChildren(),
Stream.of(
ExaminableProperty.of("children", this.children)
)
);
}
@Override
public String toString() {
return this.examine(StringExaminer.simpleEscaping());
}
}