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