/** * Returns all instances that were registered with the specified category * * @param category The category for which instances are. * @param useOrder whether to return instances in topological order as specified by {@link * #setOrder} * @return The instances registered for the specified category. */ public <T> Stream<T> streamInstances(final Class<T> category, final boolean useOrder) { return instanceRegistry(category).stream(useOrder); }
/** * Deregisters all instances with the same type as the specified one from the specified * category. The category must be registered. If instances of the same type are registered under * other categories they remain available under them. * * @param instance The instance to deregister. * @param category The category, from which the instance should be removed. * @return {true} if an instance of the same type was previously registered */ public <T> boolean deregisterInstance(final T instance, final Class<T> category) { return instanceRegistry(category).deregister(instance); }
/** * Registers the specified instance under the specified category. The category itself must * already be registered. * * @param instance The instance to register. * @param category The category to register the instance under. * @return {@code true} if this the first instance of its class. */ public <T> boolean registerInstance(final T instance, final Class<T> category) { return instanceRegistry(category).register(instance); }
/** * Deregisters all instances registered under the specified category. The category itself remain * registered. * * @param category The category from which to deregister instances. */ public void deregisterInstances(Class<?> category) { instanceRegistry(category).clear(); }
/** * Registers the specified instance under all categories that are supertypes of the instance. * * @param instance The instance to register. */ @SuppressWarnings("unchecked") public <T> void registerInstance(final T instance) { ensureArgumentNonNull("instance", instance); streamCategories() .filter(category -> category.isAssignableFrom(instance.getClass())) // the cast is correct because the filter above only leaves categories that are // supertypes of `instance` .map(category -> (InstanceRegistry<? super T>) instanceRegistry(category)) .forEach(registry -> registry.register(instance)); }
/** * Deregisters all instances with the same type as the specified one from all categories. * * @param instance The instance to deregister. */ @SuppressWarnings("unchecked") public <T> void deregisterInstance(final T instance) { ensureArgumentNonNull("instance", instance); streamCategories() .filter(category -> category.isAssignableFrom(instance.getClass())) // the cast is correct because the filter above only leaves categories that are // supertypes of `instance` .map(category -> (InstanceRegistry<? super T>) instanceRegistry(category)) .forEach(registry -> registry.deregister(instance)); }
/** * Orders the specified instances, so that the first appears before the second when {@link * #streamInstances(Class, boolean) iterateInstances} is called with {@code useOrder = true}. * * @param category The category to order instances for. * @return {@code true} if this establishes a new order */ public <T> boolean setOrder(Class<T> category, T firstInstance, T secondInstance) { ensureArgumentNonNull("firstInstance", firstInstance); ensureArgumentNonNull("secondInstance", secondInstance); return instanceRegistry(category).setOrder(firstInstance, secondInstance); }
/** * Removes the ordering between the specified instances, so that the first no longer appears * before the second when {@link #streamInstances(Class, boolean) iterateInstances} is called * with {@code useOrder = true}. * * @param category The category to clear instance order for. * @return {@code true} if that ordering existed before */ public <T> boolean clearOrder(Class<T> category, T firstInstance, T secondInstance) { ensureArgumentNonNull("firstInstance", firstInstance); ensureArgumentNonNull("secondInstance", secondInstance); return instanceRegistry(category).clearOrder(firstInstance, secondInstance); }