forked from SkriptDev/skript-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxStringBuilder.java
More file actions
103 lines (91 loc) · 3.21 KB
/
SyntaxStringBuilder.java
File metadata and controls
103 lines (91 loc) · 3.21 KB
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
package io.github.syst3ms.skriptparser.lang;
import org.jetbrains.annotations.NotNull;
import java.util.StringJoiner;
/**
* Utility class to build syntax strings, primarily intended for use
* in {@link SyntaxElement#toString(TriggerContext, boolean)} implementations.
* Spaces are automatically added between the provided objects.
*/
public class SyntaxStringBuilder {
private final boolean debug;
private final TriggerContext context;
private final StringJoiner joiner = new StringJoiner(" ");
/**
* Creates a new SyntaxStringBuilder.
*
* @param context The context to get information from.
* @param debug If true this should print more information, if false this should print what is shown to the end user
*/
public SyntaxStringBuilder(TriggerContext context, boolean debug) {
this.context = context;
this.debug = debug;
}
/**
* Adds an object to the string and returns the builder.
* Spaces are automatically added between the provided objects.
* If the object is a {@link SyntaxElement} it will be formatted using
* {@link SyntaxElement#toString(TriggerContext, boolean)}.
*
* @param object The object to add.
* @return The builder.
* @see #appendIf(boolean, Object)
*/
public SyntaxStringBuilder append(@NotNull Object object) {
if (object instanceof SyntaxElement debuggable) {
joiner.add(debuggable.toString(context, debug));
} else {
joiner.add(object.toString());
}
return this;
}
/**
* Adds multiple objects to the string and returns the builder.
* Spaces are automatically added between the provided objects.
*
* @param objects The objects to add.
* @return The builder.
* @see #appendIf(boolean, Object...)
*/
public SyntaxStringBuilder append(@NotNull Object... objects) {
for (Object object : objects) {
append(object);
}
return this;
}
/**
* Adds an object to the string and returns the builder, if the given condition is true.
* Spaces are automatically added between the provided objects.
* If the object is a {@link SyntaxElement} it will be formatted using
* {@link SyntaxElement#toString(TriggerContext, boolean)}.
*
* @param condition The condition.
* @param object The object to add. Ensure this is not null.
* @return The builder.
* @see #append(Object)
*/
public SyntaxStringBuilder appendIf(boolean condition, Object object) {
if (condition) {
append(object);
}
return this;
}
/**
* Adds multiple objects to the string and returns the builder, if the given condition is true.
* Spaces are automatically added between the provided objects.
*
* @param condition The condition.
* @param objects The objects to add. Ensure this is not null.
* @return The builder.
* @see #append(Object...)
*/
public SyntaxStringBuilder appendIf(boolean condition, Object... objects) {
if (condition) {
append(objects);
}
return this;
}
@Override
public String toString() {
return joiner.toString();
}
}