MCPcopy Create free account
hub / github.com/egg82/fetcharr / AbstractArrAPI

Class AbstractArrAPI

Lib/src/main/java/me/egg82/arr/common/AbstractArrAPI.java:35–443  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33import java.util.concurrent.TimeUnit;
34
35public abstract class AbstractArrAPI implements ArrAPI {
36 protected final Logger logger = LoggerFactory.getLogger(getClass());
37
38 private final ConcurrentMap<Class<? extends FetchableAPIObject>, @Nullable Constructor<?>> constructors = new ConcurrentHashMap<>();
39 private final ConcurrentMap<Class<? extends APIObject>, @Nullable Constructor<?>> genericConstructors = new ConcurrentHashMap<>();
40 private final ConcurrentMap<Class<? extends FetchableAPIObject>, @Nullable Object> unknowns = new ConcurrentHashMap<>();
41
42 private final ExpiringMap<Pair<Class<? extends FetchableAPIObject>, String>, @Nullable Object> cache = ExpiringMap.builder().expirationPolicy(ExpirationPolicy.CREATED).variableExpiration().build();
43 private final ExpiringMap<Pair<ObjectIntPair<Class<? extends FetchableAPIObject>>, String>, @Nullable Object> idCache = ExpiringMap.builder().expirationPolicy(ExpirationPolicy.CREATED).variableExpiration().build();
44
45 protected final String baseUrl;
46 protected final String apiKey;
47 protected final int id;
48
49 public AbstractArrAPI(@NotNull String baseUrl, @NotNull String apiKey, int id) {
50 this.baseUrl = baseUrl;
51 this.apiKey = apiKey;
52 this.id = id;
53 }
54
55 @Override
56 public @NotNull String baseUrl() {
57 return this.baseUrl;
58 }
59
60 @Override
61 public @NotNull String apiKey() {
62 return this.apiKey;
63 }
64
65 @Override
66 public <T extends FetchableAPIObject> @Nullable T fetch(@NotNull Class<T> type, @Nullable Map<String, @NotNull Object> params) {
67 Tristate memoryCache = CacheConfigVars.getTristate(CacheConfigVars.USE_MEMORY_CACHE);
68 Tristate fileCache = CacheConfigVars.getTristate(CacheConfigVars.USE_FILE_CACHE);
69 if ((memoryCache == Tristate.AUTO && fileCache == Tristate.AUTO && !isCacheWritable()) || (memoryCache == Tristate.AUTO && fileCache == Tristate.FALSE) || memoryCache == Tristate.TRUE) {
70 Pair<Class<? extends FetchableAPIObject>, String> key = Pair.of(type, encode(params));
71 T r = type.cast(cache.computeIfAbsent(key, k -> fetchInternal(type, params)));
72 if (r != null) {
73 Duration expires = r.expiresIn();
74 cache.setExpiration(key, expires.toMillis(), TimeUnit.MILLISECONDS);
75 }
76 return r;
77 }
78 return fetchInternal(type, params);
79 }
80
81 private <T extends FetchableAPIObject> @Nullable T fetchInternal(@NotNull Class<T> type, @Nullable Map<String, @NotNull Object> params) {
82 T cache = getCache(type, params);
83 if (cache != null) {
84 if (params == null || params.isEmpty()) {
85 logger.debug("Loaded {} from cache ({}_{})", type.getSimpleName(), this.type(), this.id);
86 } else {
87 logger.debug("Loaded {} ({}) from cache ({}_{})", type.getSimpleName(), encode(params), this.type(), this.id);
88 }
89 return cache;
90 }
91
92 T unknown = buildUnknown(type);

Callers

nothing calls this directly

Calls 1

buildMethod · 0.45

Tested by

no test coverage detected