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