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