]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/adapter/NewAdapterFactory.java
Improved Bindings.getBinding(Class) caching for Datatype.class
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / adapter / NewAdapterFactory.java
1 package org.simantics.databoard.adapter;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
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;
10
11 public abstract class NewAdapterFactory {
12         
13         /**
14          * Map of failed constructions. 
15          */
16         protected Map<Datatype, BindingConstructionException> failures = new HashMap<Datatype, BindingConstructionException>();
17         
18         /**
19          * Map that contains in incomplete constructions.
20          */
21         protected Map<Datatype, Binding> inprogress = new HashMap<Datatype, Binding>();
22         
23         /**
24          * Repository where successful constructions are placed. 
25          */
26         protected Map<Datatype, Binding> repository;    
27         
28         /**
29          * Create a scheme factory. 
30          */
31         public NewAdapterFactory() {            
32         }
33         
34         /**
35          * Create scheme factory that appends constructed bindings to the user given
36          * repository  
37          * 
38          * @param repository repository where bindings are placed
39          */
40         public NewAdapterFactory(Map<Datatype, Binding> repository) {
41                 this.repository = repository;
42         }
43         
44         /**
45          * Get adapter repository
46          * 
47          * @return adapter repository
48          */
49         public Map<Datatype, Binding> getRepository() {
50                 return repository;
51         }
52
53         /**
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.
58          * 
59          *  e.g. 
60          *   inprogress.put(request, notCompletelyConstructedBinding);
61          *   Binding componentBinding = construct( componentRequest );
62          *   notCompletelyConstructedBinding.setChild( componentBinding );
63          *   inprogress.remove(request);
64          *   
65          * try-finally is not needed.
66          * 
67          * @param request
68          * @return
69          * @throws BindingConstructionException
70          */
71         protected abstract Binding doConstruct(Datatype request) throws BindingConstructionException;
72         
73         public Binding construct(Datatype request) throws BindingConstructionException
74         {
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);
78                 
79                 // Start construction
80                 try {                   
81                         Binding binding = doConstruct(request);
82                         repository.put(request, binding);
83                         return binding;
84                 } catch (BindingConstructionException e) {
85                         inprogress.remove( request );
86                         failures.put(request, e);
87                         throw e;
88                 }
89         }
90
91         public Binding getBinding(Datatype type)
92                         throws BindingConstructionException {           
93                 return construct(type);
94         }
95         
96         public Binding getBindingUnchecked(Datatype type)
97                         throws RuntimeBindingConstructionException {
98                 try {
99                         return construct(type);
100                 } catch (BindingConstructionException e) {
101                         throw new RuntimeBindingConstructionException(e);
102                 }
103         }       
104
105 }