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