FactoryTreeBuilderAttributeFiller implements AttributeFiller<R> { @Override @SuppressWarnings("unchecked") public void fillNewAttributes(R root, DataStorageMetadataDictionary oldDataStorageMetadataDictionary) { factoryTreeBuilder.fillFromExistingFactoryTree(root); for (FactoryBase<?,R> factory : root.internal().collectChildrenDeep()) { boolean[] containsNewAttributes=new boolean[1]; factory.internal().visitAttributesMetadata((attributeMetadata) -> { if (isNewAttribute(factory, oldDataStorageMetadataDictionary, attributeMetadata.attributeVariableName)) { containsNewAttributes[0]=true; oldDataStorageMetadataDictionary.containsAttribute(factory.getClass().getName(), attributeMetadata.attributeVariableName); } }); if (containsNewAttributes[0]){ Class<? extends FactoryBase> aClass = factory.getClass(); FactoryBase<?, R> newBuild = factoryTreeBuilder.buildNewSubTree(aClass); FactoryMetadataManager.getMetadata(newBuild.getClass()).addBackReferencesToAttributes(newBuild,root); this.fillNewAttributes(factory,newBuild,oldDataStorageMetadataDictionary); } } } FactoryTreeBuilderAttributeFiller(FactoryTreeBuilder<L, R> factoryTreeBuilder); @Override @SuppressWarnings("unchecked") void fillNewAttributes(R root, DataStorageMetadataDictionary oldDataStorageMetadataDictionary);  }
@Test public void test_add(){ FactoryTreeBuilder<ExampleLiveObjectA, ExampleFactoryA> builder = new FactoryTreeBuilder<>(ExampleFactoryA.class, ctx -> { ExampleFactoryA exampleFactoryA = new ExampleFactoryA(); exampleFactoryA.referenceAttribute.set(ctx.get(ExampleFactoryB.class)); return exampleFactoryA; }); builder.addFactory(ExampleFactoryB.class, Scope.SINGLETON, ctx-> { ExampleFactoryB exampleFactoryB=new ExampleFactoryB(); exampleFactoryB.stringAttribute.set("111"); return exampleFactoryB; }); FactoryTreeBuilderAttributeFiller<ExampleLiveObjectA, ExampleFactoryA> filler = new FactoryTreeBuilderAttributeFiller<>(builder); ExampleFactoryA exampleFactoryA = new ExampleFactoryA(); exampleFactoryA.internal().finalise(); DataStorageMetadataDictionary dataStorageMetadataDictionaryFromRoot = exampleFactoryA.internal().createDataStorageMetadataDictionaryFromRoot(); dataStorageMetadataDictionaryFromRoot.getDataStorageMetadata(ExampleFactoryA.class.getName()).removeAttribute("referenceAttribute"); ExampleFactoryA exampleFactoryNew = new ExampleFactoryA(); exampleFactoryNew.internal().finalise(); Assertions.assertNull(exampleFactoryNew.referenceAttribute.get()); filler.fillNewAttributes(exampleFactoryNew, dataStorageMetadataDictionaryFromRoot); Assertions.assertEquals("111",exampleFactoryNew.referenceAttribute.get().stringAttribute.get()); }
@Test public void test_dont_override(){ FactoryTreeBuilder<ExampleLiveObjectA, ExampleFactoryA> builder = new FactoryTreeBuilder<>(ExampleFactoryA.class, ctx -> { ExampleFactoryA exampleFactoryA = new ExampleFactoryA(); exampleFactoryA.referenceAttribute.set(ctx.get(ExampleFactoryB.class)); return exampleFactoryA; }); builder.addFactory(ExampleFactoryB.class, Scope.SINGLETON, ctx-> { ExampleFactoryB exampleFactoryB=new ExampleFactoryB(); exampleFactoryB.stringAttribute.set("111"); return exampleFactoryB; }); FactoryTreeBuilderAttributeFiller<ExampleLiveObjectA, ExampleFactoryA> filler = new FactoryTreeBuilderAttributeFiller<>(builder); ExampleFactoryA exampleFactoryA = new ExampleFactoryA(); exampleFactoryA.internal().finalise(); DataStorageMetadataDictionary dataStorageMetadataDictionaryFromRoot = exampleFactoryA.internal().createDataStorageMetadataDictionaryFromRoot(); ExampleFactoryA exampleFactoryNew = new ExampleFactoryA(); exampleFactoryNew.internal().finalise(); Assertions.assertNull(exampleFactoryNew.referenceAttribute.get()); filler.fillNewAttributes(exampleFactoryNew,dataStorageMetadataDictionaryFromRoot); Assertions.assertNull(exampleFactoryNew.referenceAttribute.get()); }
@Test public void test_add_singleton(){ FactoryTreeBuilder< ExampleLiveObjectA, ExampleFactoryA> builder = new FactoryTreeBuilder<>(ExampleFactoryA.class, ctx -> { ExampleFactoryA exampleFactoryA = new ExampleFactoryA(); exampleFactoryA.referenceAttribute.set(ctx.get(ExampleFactoryB.class)); exampleFactoryA.referenceListAttribute.add(ctx.get(ExampleFactoryB.class)); return exampleFactoryA; }); builder.addFactory(ExampleFactoryB.class, Scope.SINGLETON, ctx-> { ExampleFactoryB exampleFactoryB=new ExampleFactoryB(); exampleFactoryB.stringAttribute.set("111"); return exampleFactoryB; }); FactoryTreeBuilderAttributeFiller<ExampleLiveObjectA, ExampleFactoryA> filler = new FactoryTreeBuilderAttributeFiller<>(builder); ExampleFactoryA exampleFactoryA = new ExampleFactoryA(); exampleFactoryA.internal().finalise(); DataStorageMetadataDictionary dataStorageMetadataDictionaryFromRoot = exampleFactoryA.internal().createDataStorageMetadataDictionaryFromRoot(); dataStorageMetadataDictionaryFromRoot.getDataStorageMetadata(ExampleFactoryA.class.getName()).removeAttribute("referenceAttribute"); ExampleFactoryA exampleFactoryNew = new ExampleFactoryA(); exampleFactoryNew.referenceAttribute.set(null); exampleFactoryNew.referenceListAttribute.add(new ExampleFactoryB()); exampleFactoryNew.internal().finalise(); filler.fillNewAttributes(exampleFactoryNew,dataStorageMetadataDictionaryFromRoot); Assertions.assertEquals(exampleFactoryNew.referenceAttribute.get(),exampleFactoryNew.referenceListAttribute.get(0)); }