FactoryTreeBuilder { public <F extends FactoryBase<?,R>> void addFactory(FactoryTemplateId<F> templateId, Scope scope, Function<FactoryContext<R>, F> creator){ factoryContext.addFactoryCreator(new FactoryCreator<>(templateId,scope,creator)); } protected  FactoryTreeBuilder(FactoryTemplateId<R> rootTemplateId, FactoryContext<R> factoryContext); protected  FactoryTreeBuilder(FactoryTemplateId<R> rootTemplateId, Consumer<FactoryTreeBuilder<L,R>> rootTemplateAdder);  FactoryTreeBuilder(FactoryTemplateId<R> rootTemplateId, Function<FactoryContext<R>, R> creator);  FactoryTreeBuilder(Class<R> rootClass);  FactoryTreeBuilder(Class<R> rootClass, Function<FactoryContext<R>, R> creator); void addFactory(FactoryTemplateId<F> templateId, Scope scope, Function<FactoryContext<R>, F> creator); void addFactory(Class<F> clazz, Scope scope, Function<FactoryContext<R>, F> creator); void addFactory(String name, Scope scope, Function<FactoryContext<R>, F> creator); void addFactory(Class<F> clazz, String name, Scope scope, Function<FactoryContext<R>, F> creator); void addFactory(Class<F> clazz, Scope scope); void addSingleton(FactoryTemplateId<F> templateId, Function<FactoryContext<R>, F> creator); void addSingleton(Class<F> clazz, Function<FactoryContext<R>, F> creator); void addSingleton(String name, Function<FactoryContext<R>, F> creator); void addSingleton(Class<F> clazz, String name, Function<FactoryContext<R>, F> creator); void addSingleton(Class<F> clazz, String name); void addSingleton(Class<F> clazz); void addPrototype(FactoryTemplateId<F> templateId, Function<FactoryContext<R>, F> creator); void addPrototype(Class<F> clazz, Function<FactoryContext<R>, F> creator); void addPrototype(String name, Function<FactoryContext<R>, F> creator); void addPrototype(Class<F> clazz, String name, Function<FactoryContext<R>, F> creator); void addPrototype(Class<F> clazz, String name); void addPrototype(Class<F> clazz); void removeFactory(FactoryTemplateId<F> templateId); @SuppressWarnings("unchecked") void addFactoryUnsafe(FactoryTemplateId<?> templateId, Scope scope, Function<FactoryContext<R>, F> creator); @SuppressWarnings("unchecked") void addFactoryUnsafe(Class<?> clazz, Scope scope, Function<FactoryContext<R>, F> creator); void addFactoryUnsafe(String name, Scope scope, Function<FactoryContext<R>, F> creator); @SuppressWarnings("unchecked") void addFactoryUnsafe(Class<?> clazz, String name, Scope scope, Function<FactoryContext<R>, F> creator); @SuppressWarnings("unchecked") void addFactoryUnsafe(Class<?> clazz, Scope scope); R buildTree(); void markAsNonPersistentFactoryBuilder(); boolean isPersistentFactoryBuilder(); R buildTreeUnvalidated(); FO buildNewSubTree(Class<FO> factoryClazz); FO buildSubTree(Class<FO> factoryClazz); List<FO> buildSubTrees(Class<FO> factoryClazz); List<FO> buildSubTreesForLiveObject(Class<LO> liveObjectClass); BranchSelector<R> branch(); Scope getScope(FactoryTemplateId<?> factoryTemplateId); void fillFromExistingFactoryTree(R root); MicroserviceBuilder<L,R> microservice(); MicroserviceBuilder<L,R> microservice(SimpleObjectMapper simpleObjectMapper); MicroserviceBuilder<L,R> microservice(Consumer<R> mocker); boolean isRebuildAble(List<FactoryBase<?, R>> existingFactoryBases); R rebuildTreeUnvalidated(List<FactoryBase<?, R>> existingFactoryBases); void addBuilder(Function<FactoryContext<R>, NestedBuilder<R>> builderCreators); FactoryTemplateId<R> getRootTemplateId(); FactoryTreeBuilder<L,R> copy();  }
@Test public void test_double_add_factory(){ Assertions.assertThrows(IllegalArgumentException.class, () -> { FactoryTreeBuilder<Void, TreeFactoryTestA> builder = new FactoryTreeBuilder<>(TreeFactoryTestA.class, context -> { TreeFactoryTestA factory = new TreeFactoryTestA(); factory.referenceAttribute1.set(context.get(TreeExampleFactoryB.class)); return factory; }); builder.addFactory(TreeExampleFactoryB.class, Scope.SINGLETON, context -> { return new ExampleFactoryB2(); }); builder.addFactory(TreeExampleFactoryB.class, Scope.SINGLETON, context -> { return new ExampleFactoryB2(); }); }); }