X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Ffactory%2FTypeBindingFactory.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Ffactory%2FTypeBindingFactory.java;h=cd4304b09067f8fc7a7b51c610fb556ebcd4506e;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/TypeBindingFactory.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/TypeBindingFactory.java new file mode 100644 index 000000000..cd4304b09 --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/TypeBindingFactory.java @@ -0,0 +1,115 @@ +package org.simantics.databoard.binding.factory; + +import java.util.HashMap; +import java.util.Map; + +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; +import org.simantics.databoard.type.Datatype; + +/** + * Type Factory constructs data types from reflection requests. + * Successfully constructed types are placed in the repository that was given + * at construction time. + * + * @author Toni Kalajainen + */ +public abstract class TypeBindingFactory implements BindingScheme { + + /** + * Map of failed constructions. + */ + protected Map failures = new HashMap(); + + /** + * Map that contains in incomplete constructions. + */ + protected Map inprogress = new HashMap(); + + /** + * Repository where successful constructions are placed. + */ + protected Map repository; + + /** + * Create a scheme factory. + */ + public TypeBindingFactory() { + this.repository = new HashMap(); + } + + /** + * Create scheme factory that appends constructed bindings to the user given + * repository + * + * @param repository repository where bindings are placed + */ + public TypeBindingFactory(Map repository) { + this.repository = repository; + } + + /** + * Get Repository + * @return binding repository + */ + public Map getRepository() { + return repository; + } + + /** + * Constructs a binding to comply to datatype request. + * This is the method sub-classes implement. + * The implementation should use the inprogress -map for construction of + * bindings that have component types. + * + * e.g. + * inprogress.put(request, notCompletelyConstructedBinding); + * Binding componentBinding = construct( componentRequest ); + * notCompletelyConstructedBinding.setChild( componentBinding ); + * inprogress.remove(request); + * + * try-finally is not needed. + * + * @param request + * @return + * @throws BindingConstructionException + */ + protected abstract Binding doConstruct(Datatype request) throws BindingConstructionException; + + public Binding construct(Datatype request) throws BindingConstructionException + { + if (failures.containsKey(request)) throw failures.get(request); + if (inprogress.containsKey(request)) return inprogress.get(request); + if (repository.containsKey(request)) return repository.get(request); + + // Start construction + try { + Binding binding = doConstruct(request); + repository.put(request, binding); + return binding; + } catch (BindingConstructionException e) { + inprogress.remove( request ); + failures.put(request, e); + throw e; + } + } + + @Override + public Binding getBinding(Datatype type) + throws BindingConstructionException { + return construct(type); + } + + @Override + public Binding getBindingUnchecked(Datatype type) + throws RuntimeBindingConstructionException { + try { + return construct(type); + } catch (BindingConstructionException e) { + throw new RuntimeBindingConstructionException(e); + } + } + + +}