diff --git a/src/main/java/com/volmit/host/FeatureCatalog.java b/src/main/java/com/volmit/host/FeatureCatalog.java index 0f663801a..6e316874e 100644 --- a/src/main/java/com/volmit/host/FeatureCatalog.java +++ b/src/main/java/com/volmit/host/FeatureCatalog.java @@ -5,11 +5,13 @@ import com.volmit.service.Providers; import com.volmit.service.Service; import java.util.Collection; import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; import java.util.function.Function; +import java.util.stream.Collectors; /** Frozen registrations. Lists use lexical feature/layer/kind ID order, and validators, lifecycle hooks, service * providers are in module id order, each module's in the order it registered them, so @@ -17,6 +19,19 @@ import java.util.function.Function; * concurrency contracts. */ public final class FeatureCatalog { + /** Services are asked for on the generation hot path, so each type's annotation is read once. */ + private static final ClassValue SERVICE_PROVIDERS = new ClassValue<>() { + @Override + protected Providers computeValue(Class type) { + var service = type.isInterface() ? type.getAnnotation(Service.class) : null; + if (service == null) { + throw new IllegalArgumentException(type.getName() + " is not a service: services are interfaces marked @" + + Service.class.getName()); + } + return service.value(); + } + }; + private final Map, FeatureDescriptor> settings; private final Map, ObjectRegistration> objects; private final Map> mantle; @@ -24,7 +39,8 @@ public final class FeatureCatalog { private final List heldTypes; private final List validators; private final List lifecycles; - private final List services; + private final Map, List> registrationsByService; + private final Map, List> providersByService; FeatureCatalog( Map, FeatureDescriptor> settings, @@ -42,7 +58,8 @@ public final class FeatureCatalog { this.heldTypes = sorted(heldTypes, registration -> registration.type().getName()); this.validators = inModuleOrder(validators, ValidatorRegistration::module); this.lifecycles = inModuleOrder(lifecycles, LifecycleRegistration::module); - this.services = inModuleOrder(services, ServiceRegistration::module); + this.registrationsByService = byService(inModuleOrder(services, ServiceRegistration::module)); + this.providersByService = providersByService(registrationsByService); } /** Settings types that activate stages; the host decodes them from pack JSON. */ @@ -81,39 +98,53 @@ public final class FeatureCatalog { /** Every provider of {@code service}, an interface marked {@link Service}, in module id order and each module's in the * order it registered them; empty when no module provides it. */ + @SuppressWarnings("unchecked") // ModuleRegistry.registerService checked that every provider implements its service. public List services(Class service) { - requireService(service); - return services.stream() - .filter(registration -> registration.service() == service) - .map(registration -> service.cast(registration.provider())) - .toList(); + var providers = providersByService.get(service); + if (providers == null) { + providersOf(service); + return List.of(); + } + return (List) providers; } /** Every provider of {@code service} with the module that registered it, in the order of {@link #services}, so the host * can name the module whose provider failed. */ public List registrations(Class service) { - requireService(service); - return services.stream().filter(registration -> registration.service() == service).toList(); + var registrations = registrationsByService.get(service); + if (registrations == null) { + providersOf(service); + return List.of(); + } + return registrations; } /** The one provider of {@code service}, a service whose providers are {@link Providers#ONE}, if a module provides it. */ public Optional service(Class service) { - if (requireService(service).value() != Providers.ONE) { + if (providersOf(service) != Providers.ONE) { throw new IllegalArgumentException("Service " + service.getName() + " may have several providers; ask for all of" + " them with services(...)"); } - return services(service).stream().findFirst(); + var providers = services(service); + return providers.isEmpty() ? Optional.empty() : Optional.of(providers.getFirst()); } - /** The {@link Service} annotation of {@code type}, which must be an interface carrying one. */ - static Service requireService(Class type) { - var service = type.isInterface() ? type.getAnnotation(Service.class) : null; - if (service == null) { - throw new IllegalArgumentException(type.getName() + " is not a service: services are interfaces marked @" - + Service.class.getName()); - } - return service; + /** How many modules may provide {@code service}, which must be an interface marked {@link Service}. */ + static Providers providersOf(Class service) { + return SERVICE_PROVIDERS.get(service); + } + + private static Map, List> byService(List inModuleOrder) { + return Map.copyOf(inModuleOrder.stream() + .collect(Collectors.groupingBy(ServiceRegistration::service, Collectors.toUnmodifiableList()))); + } + + private static Map, List> providersByService(Map, List> registrationsByService) { + var providers = new HashMap, List>(); + registrationsByService.forEach((service, registrations) -> + providers.put(service, registrations.stream().map(ServiceRegistration::provider).toList())); + return Map.copyOf(providers); } private static List inModuleOrder(List registrations, Function module) { diff --git a/src/main/java/com/volmit/host/ModuleRegistry.java b/src/main/java/com/volmit/host/ModuleRegistry.java index a84b5cb18..d10b30ec3 100644 --- a/src/main/java/com/volmit/host/ModuleRegistry.java +++ b/src/main/java/com/volmit/host/ModuleRegistry.java @@ -161,7 +161,7 @@ public final class ModuleRegistry implements FeatureRegistry { Objects.requireNonNull(service, "service"); Objects.requireNonNull(provider, "provider"); requireRegistering("Provider of " + service.getName()); - var providers = FeatureCatalog.requireService(service).value(); + var providers = FeatureCatalog.providersOf(service); if (!service.isInstance(provider)) { throw new IllegalArgumentException("Provider " + provider + " of module " + registering + " does not implement " + service.getName()); diff --git a/src/test/java/com/volmit/host/ModuleRegistryTest.java b/src/test/java/com/volmit/host/ModuleRegistryTest.java index ef27a5a28..f796f375d 100644 --- a/src/test/java/com/volmit/host/ModuleRegistryTest.java +++ b/src/test/java/com/volmit/host/ModuleRegistryTest.java @@ -395,6 +395,11 @@ class ModuleRegistryTest { var several = assertThrows(IllegalArgumentException.class, () -> catalog.service(Greeting.class)); assertTrue(several.getMessage().contains("services(...)"), several.getMessage()); assertThrows(IllegalArgumentException.class, () -> catalog.services(NotAService.class)); + assertThrows(IllegalArgumentException.class, () -> catalog.registrations(NotAService.class), "asking again still refuses it"); + assertEquals(List.of(alphaFirst, alphaSecond, zeta), + catalog.registrations(Greeting.class).stream().map(ServiceRegistration::provider).toList()); + assertEquals(List.of(), catalog.registrations(Clock.class)); + assertThrows(UnsupportedOperationException.class, () -> catalog.services(Greeting.class).clear(), "the catalog is frozen"); } @Test