Merge pull request 'CMDAPIBetterCaching' (#39) from CMDAPIBetterCaching into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #39
Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Lixfel 2023-01-21 17:38:32 +01:00
Commit 61999b0507
5 geänderte Dateien mit 23 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -50,4 +50,13 @@ public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
default Collection<String> tabCompletes(K sender, PreviousArguments previousArguments, String s) { default Collection<String> tabCompletes(K sender, PreviousArguments previousArguments, String s) {
return tabCompletes(sender, previousArguments.userArgs, s); return tabCompletes(sender, previousArguments.userArgs, s);
} }
/**
* Normalize the cache key by sender and user provided argument. <br>
* Note: The CommandSender can be null! <br>
* Returning null and the empty string are equivalent.
*/
default String normalize(K sender, String s) {
return null;
}
} }

Datei anzeigen

@ -165,7 +165,7 @@ class CommandPart<T> {
} }
private Collection<String> tabCompletes(T sender, String[] args, List<Object> mappedArgs, int startIndex) { private Collection<String> tabCompletes(T sender, String[] args, List<Object> mappedArgs, int startIndex) {
return TabCompletionCache.tabComplete(sender, typeMapper, command, () -> { return TabCompletionCache.tabComplete(sender, args[startIndex], (AbstractTypeMapper<Object, ?>) typeMapper, command, () -> {
try { try {
return typeMapper.tabCompletes(sender, new PreviousArguments(Arrays.copyOf(args, startIndex), mappedArgs.toArray()), args[startIndex]); return typeMapper.tabCompletes(sender, new PreviousArguments(Arrays.copyOf(args, startIndex), mappedArgs.toArray()), args[startIndex]);
} catch (Throwable e) { } catch (Throwable e) {

Datei anzeigen

@ -51,6 +51,7 @@ public class TabCompletionCache {
@AllArgsConstructor @AllArgsConstructor
private static class Key { private static class Key {
private Object sender; private Object sender;
private String arg;
private AbstractTypeMapper<?, ?> typeMapper; private AbstractTypeMapper<?, ?> typeMapper;
} }
@ -61,9 +62,11 @@ public class TabCompletionCache {
private Collection<String> tabCompletions; private Collection<String> tabCompletions;
} }
Collection<String> tabComplete(Object sender, AbstractTypeMapper<?, ?> typeMapper, AbstractSWCommand<?> command, Supplier<Collection<String>> tabCompleteSupplier) { Collection<String> tabComplete(Object sender, String arg, AbstractTypeMapper<Object, ?> typeMapper, AbstractSWCommand<?> command, Supplier<Collection<String>> tabCompleteSupplier) {
if (!cached.contains(typeMapper)) return tabCompleteSupplier.get(); if (!cached.contains(typeMapper)) return tabCompleteSupplier.get();
Key key = global.contains(typeMapper) ? new Key(null, typeMapper) : new Key(sender, typeMapper); String normalizedArg = typeMapper.normalize(sender, arg);
if (normalizedArg == null) normalizedArg = "";
Key key = global.contains(typeMapper) ? new Key(null, normalizedArg, typeMapper) : new Key(sender, normalizedArg, typeMapper);
TabCompletions tabCompletions = tabCompletionCache.computeIfAbsent(key, ignore -> { TabCompletions tabCompletions = tabCompletionCache.computeIfAbsent(key, ignore -> {
return new TabCompletions(command, System.currentTimeMillis(), tabCompleteSupplier.get()); return new TabCompletions(command, System.currentTimeMillis(), tabCompleteSupplier.get());
}); });

Datei anzeigen

@ -41,7 +41,6 @@ public class CacheCommand extends TestSWCommand {
@Cached @Cached
@Mapper(value = "int", local = true) @Mapper(value = "int", local = true)
public AbstractTypeMapper<String, Integer> typeMapper() { public AbstractTypeMapper<String, Integer> typeMapper() {
System.out.println("TypeMapper register");
return new TestTypeMapper<Integer>() { return new TestTypeMapper<Integer>() {
@Override @Override
public Integer map(String sender, PreviousArguments previousArguments, String s) { public Integer map(String sender, PreviousArguments previousArguments, String s) {

Datei anzeigen

@ -36,6 +36,14 @@ public class CacheCommandTest {
assertThat(tabCompletions1, is(equalTo(tabCompletions2))); assertThat(tabCompletions1, is(equalTo(tabCompletions2)));
} }
@Test
public void testCachingWithDifferentMessages() {
CacheCommand cmd = new CacheCommand();
List<String> tabCompletions1 = cmd.tabComplete("test", "", new String[]{""});
List<String> tabCompletions2 = cmd.tabComplete("test", "", new String[]{"0"});
assertThat(tabCompletions1, is(equalTo(tabCompletions2)));
}
@Test @Test
public void testCachingWithDifferentSenders() { public void testCachingWithDifferentSenders() {
CacheCommand cmd = new CacheCommand(); CacheCommand cmd = new CacheCommand();