CommonCore/src/de/steamwar/command/TabCompletionCache.java

94 Zeilen
3.6 KiB
Java

2022-06-20 21:20:42 +02:00
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-06-20 13:16:13 +02:00
package de.steamwar.command;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.experimental.UtilityClass;
import java.util.*;
import java.util.concurrent.TimeUnit;
2022-06-20 13:16:13 +02:00
import java.util.function.Supplier;
@UtilityClass
public class TabCompletionCache {
private Map<Key, TabCompletions> tabCompletionCache = new HashMap<>();
Set<AbstractTypeMapper<?, ?>> cached = new HashSet<>();
Set<AbstractTypeMapper<?, ?>> global = new HashSet<>();
Map<AbstractTypeMapper<?, ?>, Long> cacheDuration = new HashMap<>();
2022-06-20 19:35:02 +02:00
void add(AbstractTypeMapper<?, ?> typeMapper, AbstractSWCommand.Cached cached) {
if (cached != null) {
add(typeMapper, cached.global(), cached.cacheDuration(), cached.timeUnit());
2022-06-20 19:35:02 +02:00
}
}
public void add(AbstractTypeMapper<?, ?> typeMapper, boolean global, long cacheDuration, TimeUnit timeUnit) {
TabCompletionCache.cached.add(typeMapper);
if (global) TabCompletionCache.global.add(typeMapper);
TabCompletionCache.cacheDuration.put(typeMapper, timeUnit.toMillis(cacheDuration));
}
2022-06-20 13:16:13 +02:00
@EqualsAndHashCode
@AllArgsConstructor
private static class Key {
private Object sender;
2023-01-21 17:06:56 +01:00
private String arg;
2022-06-20 13:16:13 +02:00
private AbstractTypeMapper<?, ?> typeMapper;
}
@AllArgsConstructor
private static class TabCompletions {
private long timestamp;
private Collection<String> tabCompletions;
}
2023-01-21 21:46:48 +01:00
Collection<String> tabComplete(Object sender, String arg, AbstractTypeMapper<Object, ?> typeMapper, Supplier<Collection<String>> tabCompleteSupplier) {
2022-06-20 13:16:13 +02:00
if (!cached.contains(typeMapper)) return tabCompleteSupplier.get();
2023-01-21 21:46:48 +01:00
2023-01-21 17:06:56 +01:00
String normalizedArg = typeMapper.normalize(sender, arg);
if (normalizedArg == null) normalizedArg = "";
2023-01-21 21:46:48 +01:00
Key key = new Key(global.contains(typeMapper) ? null : sender, normalizedArg, typeMapper);
2022-06-20 13:16:13 +02:00
TabCompletions tabCompletions = tabCompletionCache.computeIfAbsent(key, ignore -> {
2023-01-21 21:46:48 +01:00
return new TabCompletions(System.currentTimeMillis(), tabCompleteSupplier.get());
2022-06-20 13:16:13 +02:00
});
2023-01-21 21:46:48 +01:00
if (System.currentTimeMillis() - tabCompletions.timestamp > cacheDuration.get(typeMapper)) {
tabCompletions.tabCompletions = tabCompleteSupplier.get();
2022-06-20 13:16:13 +02:00
}
tabCompletions.timestamp = System.currentTimeMillis();
return tabCompletions.tabCompletions;
}
public void invalidateOldEntries() {
Set<Key> toRemove = new HashSet<>();
for (Map.Entry<Key, TabCompletions> tabCompletionsEntry : tabCompletionCache.entrySet()) {
if (System.currentTimeMillis() - tabCompletionsEntry.getValue().timestamp > cacheDuration.get(tabCompletionsEntry.getKey().typeMapper)) {
toRemove.add(tabCompletionsEntry.getKey());
}
}
for (Key key : toRemove) {
tabCompletionCache.remove(key);
}
}
}