1 package org.simantics.databoard.adapter;
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;
11 public abstract class NewAdapterFactory {
14 * Map of failed constructions.
16 protected Map<Datatype, BindingConstructionException> failures = new HashMap<Datatype, BindingConstructionException>();
19 * Map that contains in incomplete constructions.
21 protected Map<Datatype, Binding> inprogress = new HashMap<Datatype, Binding>();
24 * Repository where successful constructions are placed.
26 protected Map<Datatype, Binding> repository;
29 * Create a scheme factory.
31 public NewAdapterFactory() {
35 * Create scheme factory that appends constructed bindings to the user given
38 * @param repository repository where bindings are placed
40 public NewAdapterFactory(Map<Datatype, Binding> repository) {
41 this.repository = repository;
45 * Get adapter repository
47 * @return adapter repository
49 public Map<Datatype, Binding> getRepository() {
54 * Constructs a binding to comply to datatype request.
55 * This is the method sub-classes implement.
56 * The implementation should use the inprogress -map for construction of
57 * bindings that have component types.
60 * inprogress.put(request, notCompletelyConstructedBinding);
61 * Binding componentBinding = construct( componentRequest );
62 * notCompletelyConstructedBinding.setChild( componentBinding );
63 * inprogress.remove(request);
65 * try-finally is not needed.
69 * @throws BindingConstructionException
71 protected abstract Binding doConstruct(Datatype request) throws BindingConstructionException;
73 public Binding construct(Datatype request) throws BindingConstructionException
75 if (failures.containsKey(request)) throw failures.get(request);
76 if (inprogress.containsKey(request)) return inprogress.get(request);
77 if (repository.containsKey(request)) return repository.get(request);
81 Binding binding = doConstruct(request);
82 repository.put(request, binding);
84 } catch (BindingConstructionException e) {
85 inprogress.remove( request );
86 failures.put(request, e);
91 public Binding getBinding(Datatype type)
92 throws BindingConstructionException {
93 return construct(type);
96 public Binding getBindingUnchecked(Datatype type)
97 throws RuntimeBindingConstructionException {
99 return construct(type);
100 } catch (BindingConstructionException e) {
101 throw new RuntimeBindingConstructionException(e);