Skip to content

Commit

Permalink
Cache services in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Aug 2, 2024
1 parent 0f8d94b commit 67d7d51
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions common/src/main/java/juuxel/adorn/util/Services.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package juuxel.adorn.util;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.ServiceLoader;

public final class Services {
private static final Map<Class<?>, Object> CACHE = new IdentityHashMap<>();

@SuppressWarnings("unchecked")
public static <T> T load(Class<T> c) {
return (T) CACHE.computeIfAbsent(c, Services::loadService);
}

private static <T> T loadService(Class<T> c) {
return ServiceLoader.load(c)
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not find Adorn platform service " + c.getSimpleName()));
Expand Down

0 comments on commit 67d7d51

Please sign in to comment.