1 package org.simantics.databoard.binding.factory;
3 import java.util.HashMap;
6 import org.simantics.databoard.binding.Binding;
7 import org.simantics.databoard.binding.error.BindingConstructionException;
8 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
9 import org.simantics.databoard.type.Datatype;
12 * Type Factory constructs data types from reflection requests.
13 * Successfully constructed types are placed in the repository that was given
14 * at construction time.
16 * @author Toni Kalajainen
18 public abstract class TypeBindingFactory implements BindingScheme {
21 * Map of failed constructions.
23 protected Map<Datatype, BindingConstructionException> failures = new HashMap<Datatype, BindingConstructionException>();
26 * Map that contains in incomplete constructions.
28 protected Map<Datatype, Binding> inprogress = new HashMap<Datatype, Binding>();
31 * Repository where successful constructions are placed.
33 protected Map<Datatype, Binding> repository;
36 * Create a scheme factory.
38 public TypeBindingFactory() {
39 this.repository = new HashMap<Datatype, Binding>();
43 * Create scheme factory that appends constructed bindings to the user given
46 * @param repository repository where bindings are placed
48 public TypeBindingFactory(Map<Datatype, Binding> repository) {
49 this.repository = repository;
54 * @return binding repository
56 public Map<Datatype, Binding> getRepository() {
61 * Constructs a binding to comply to datatype request.
62 * This is the method sub-classes implement.
63 * The implementation should use the inprogress -map for construction of
64 * bindings that have component types.
67 * inprogress.put(request, notCompletelyConstructedBinding);
68 * Binding componentBinding = construct( componentRequest );
69 * notCompletelyConstructedBinding.setChild( componentBinding );
70 * inprogress.remove(request);
72 * try-finally is not needed.
76 * @throws BindingConstructionException
78 protected abstract Binding doConstruct(Datatype request) throws BindingConstructionException;
80 public Binding construct(Datatype request) throws BindingConstructionException
82 if (failures.containsKey(request)) throw failures.get(request);
83 if (inprogress.containsKey(request)) return inprogress.get(request);
84 if (repository.containsKey(request)) return repository.get(request);
88 Binding binding = doConstruct(request);
89 repository.put(request, binding);
91 } catch (BindingConstructionException e) {
92 inprogress.remove( request );
93 failures.put(request, e);
99 public Binding getBinding(Datatype type)
100 throws BindingConstructionException {
101 return construct(type);
105 public Binding getBindingUnchecked(Datatype type)
106 throws RuntimeBindingConstructionException {
108 return construct(type);
109 } catch (BindingConstructionException e) {
110 throw new RuntimeBindingConstructionException(e);