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