-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathArgumentStack.java
141 lines (125 loc) · 5.08 KB
/
ArgumentStack.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
package revxrsal.commands.command;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import org.jetbrains.annotations.UnmodifiableView;
import revxrsal.commands.autocomplete.AutoCompleter;
import revxrsal.commands.core.LinkedArgumentStack;
import revxrsal.commands.util.tokenize.QuotedStringTokenizer;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
/**
* Represents a mutable stack of strings represented as command arguments.
* <p>
* This class holds extremely similar functionality to a LinkedList, and in
* most contexts should be safely castable to one.
*/
public interface ArgumentStack extends Deque<String>, List<String>, Cloneable {
/**
* Joins all present arguments in this stack
*
* @param delimiter Delimiter between these arguments.
* @return The combined string
*/
@NotNull String join(String delimiter);
/**
* Joins all present arguments in this stack, starting from
* the specified index
*
* @param delimiter Delimiter between these arguments
* @param startIndex The start index to combine from
* @return The combined string
*/
@NotNull String join(@NotNull String delimiter, int startIndex);
/**
* Returns (and removes) the string in which might get concatenated with the rest
* of the arguments if the parameter {@link CommandParameter#consumesAllString() consumes all strings}
* that follow it.
*
* @param parameter The parameter to get for
* @return The string for this parameter. Will return the first argument if the parameter cannot
* consume all strings.
* @see CommandParameter#consumesAllString()
*/
String popForParameter(@NotNull CommandParameter parameter);
/**
* Returns this argument stack as an immutable view. This can be therefore
* passed to any conditions or resolvers without having to worry about being
* unintentionally modified.
* <p>
* Note that this does not create an independent copy, and instead returns
* a view which does not allow modifications. If this argument stack gets
* modified from somewhere else, the immutable view will also be modified.
*
* @return The argument stack as an immutable view
*/
@NotNull @UnmodifiableView List<String> asImmutableView();
/**
* Returns an immutable copy of this stack. This copy will behave
* independently of the original {@link ArgumentStack}.
*
* @return An immutable copy of this {@link ArgumentStack}.
*/
@NotNull @Unmodifiable List<String> asImmutableCopy();
/**
* Returns a new {@link ArgumentStack} with the specified arguments.
*
* @param arguments Arguments to clone from
* @return The newly created argument stack.
*/
static @NotNull ArgumentStack of(@NotNull Collection<String> arguments) {
if (arguments.size() == 0) return empty();
return new LinkedArgumentStack(QuotedStringTokenizer.tokenize(String.join(" ", arguments)));
}
/**
* Returns a new {@link ArgumentStack} with the specified arguments, without
* doing any special parsing for quotes.
*
* @param arguments Arguments to clone from
* @return The newly created argument stack.
*/
static @NotNull ArgumentStack exactly(@NotNull Collection<String> arguments) {
if (arguments.size() == 0) return empty();
return new LinkedArgumentStack(arguments.toArray(new String[0]));
}
/**
* Returns a new {@link ArgumentStack} with the specified arguments.
*
* @param arguments Arguments to clone from
* @return The newly created argument stack.
*/
static @NotNull ArgumentStack of(@NotNull String... arguments) {
if (arguments.length == 0) return empty();
return new LinkedArgumentStack(QuotedStringTokenizer.tokenize(String.join(" ", arguments)));
}
/**
* Returns a new {@link ArgumentStack} with the specified arguments. This
* will not remove trailing space, and hence may give incorrect
* measures. This should only be used with {@link AutoCompleter#complete(CommandActor, ArgumentStack)}.
*
* @param arguments Arguments to clone from
* @return The newly created argument stack.
*/
static @NotNull ArgumentStack ofUnsafe(@NotNull String... arguments) {
if (arguments.length == 0) return ArgumentStack.empty();
return new LinkedArgumentStack(QuotedStringTokenizer.tokenizeUnsafe(String.join(" ", arguments)));
}
/**
* Returns a new {@link ArgumentStack} with the specified arguments,
* from splitting the string by whitespace.
*
* @param arguments Arguments to split
* @return The newly created argument stack.
*/
static @NotNull ArgumentStack fromString(@NotNull String arguments) {
return new LinkedArgumentStack(QuotedStringTokenizer.tokenize(arguments));
}
/**
* Returns a new, empty {@link ArgumentStack}.
*
* @return A new, empty argument stack
*/
static @NotNull ArgumentStack empty() {
return new LinkedArgumentStack();
}
}