Skip to content

Commit a5e2d65

Browse files
added the EnumSuggester
1 parent 12a1db4 commit a5e2d65

File tree

1 file changed

+66
-0
lines changed
  • commands/src/main/java/com/wizardlybump17/wlib/command/suggestion/object

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.wizardlybump17.wlib.command.suggestion.object;
2+
3+
import com.wizardlybump17.wlib.command.exception.SuggesterException;
4+
import com.wizardlybump17.wlib.command.node.CommandNode;
5+
import com.wizardlybump17.wlib.command.sender.CommandSender;
6+
import com.wizardlybump17.wlib.command.suggestion.Suggester;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.util.List;
10+
import java.util.Objects;
11+
12+
public interface EnumSuggester<E extends Enum<E>> extends Suggester<E> {
13+
14+
@Override
15+
default @NotNull String getStringRepresentation(@NotNull E value) {
16+
return value.name();
17+
}
18+
19+
@NotNull Class<E> enumType();
20+
21+
static <E extends Enum<E>> @NotNull Any<E> any(@NotNull Class<E> enumType) {
22+
return new Any<>(enumType);
23+
}
24+
25+
final class Any<E extends Enum<E>> implements EnumSuggester<E> {
26+
27+
private final @NotNull List<E> cache;
28+
private final @NotNull Class<E> enumType;
29+
30+
private Any(@NotNull Class<E> enumType) {
31+
this.enumType = enumType;
32+
this.cache = List.of(enumType.getEnumConstants());
33+
}
34+
35+
@Override
36+
public @NotNull List<E> getSuggestions(@NotNull CommandSender<?> sender, @NotNull List<String> input, @NotNull String current, @NotNull CommandNode<?> currentNode) throws SuggesterException {
37+
return cache;
38+
}
39+
40+
@Override
41+
public @NotNull Class<E> enumType() {
42+
return enumType;
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (o == null || getClass() != o.getClass())
48+
return false;
49+
Any<?> any = (Any<?>) o;
50+
return Objects.equals(cache, any.cache) && Objects.equals(enumType, any.enumType);
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(cache, enumType);
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "EnumSuggester$Any{" +
61+
"cache=" + cache +
62+
", enumType=" + enumType +
63+
'}';
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)