]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/SerializerFactory.java
Improved Bindings.getBinding(Class) caching for Datatype.class
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / SerializerFactory.java
1 package org.simantics.databoard.serialization;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.simantics.databoard.binding.Binding;
7
8 /**
9  *
10  * 
11  * @author Toni Kalajainen
12  */
13 public abstract class SerializerFactory implements SerializerScheme {
14
15         /**
16          * Map of failed constructions. 
17          */
18         protected Map<Binding, SerializerConstructionException> failures = new HashMap<Binding, SerializerConstructionException>();
19         
20         /**
21          * Repository where serializers are placed. 
22          */
23         Map<Binding, Serializer> repository;    
24         
25         /**
26          * Map that contains in incomplete constructions.
27          */
28         Map<Binding, Serializer> inprogress = new HashMap<Binding, Serializer>();
29         
30         /**
31          * Construct a new serializer.
32          */
33         public SerializerFactory() {
34                 this.repository = new HashMap<Binding, Serializer>();
35         }
36         
37         /**
38          * Construct a new serializer factory that places constructed serializers
39          * into user given repository.
40          * 
41          * @param repository
42          */
43         public SerializerFactory(Map<Binding, Serializer> repository) {
44                 this.repository = repository;
45         }
46         
47         public Map<Binding, Serializer> getRepository() {
48                 return repository;
49         }
50         
51         /**
52          * Constructs a serilizer for a binding. Implement this.
53          * It should use the inprogress -map for construction of 
54          * serializers that have component types.
55          * 
56          *  e.g. 
57          *   inprogress.put(binding, notCompletelyConstructedSerializer);
58          *   Serializer componentSerializer = construct( componentBinding );
59          *   notCompletelyConstructedSerializer.setComponent( componentSerializer );
60          *   inprogress.remove(binding);
61          *   
62          * try-finally is not needed.
63          * 
64          * @param request
65          * @return
66          * @throws SerializerConstructionException
67          */
68         protected abstract Serializer doConstruct(Binding request) throws SerializerConstructionException;
69         
70         public Serializer construct(Binding request) throws SerializerConstructionException
71         {
72                 // Optimization: if binding provides a cached serializer, just return it.
73                 { Serializer ser = request.cachedSerializer(); if (ser != null) return ser; } 
74                 { Serializer ser = repository.get(request); if(ser != null) return ser; }
75                 { Serializer ser = inprogress.get(request); if(ser != null) return ser; }
76                 { SerializerConstructionException e = failures.get(request); if(e != null) throw e; }
77                 
78                 // Start construction
79                 try {                   
80                         Serializer binding = doConstruct(request);
81                         repository.put(request, binding);
82                         request.cacheSerializer(binding);
83                         return binding;
84                 } catch (SerializerConstructionException e) {
85                         inprogress.remove( request );
86                         failures.put(request, e);
87                         throw e;
88                 }
89         }
90
91         @Override
92         public Serializer getSerializer(Binding binding)
93                         throws SerializerConstructionException {                
94                 return construct(binding);
95         }
96         
97         public Serializer getSerializerUnchecked(Binding binding)
98                         throws RuntimeSerializerConstructionException {
99                 try {
100                         return construct(binding);
101                 } catch (SerializerConstructionException e) {
102                         throw new RuntimeSerializerConstructionException(e);
103                 }
104         }       
105         
106         
107 }