-package org.simantics.databoard.serialization;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import org.simantics.databoard.binding.Binding;\r
-\r
-/**\r
- *\r
- * \r
- * @author Toni Kalajainen\r
- */\r
-public abstract class SerializerFactory implements SerializerScheme {\r
-\r
- /**\r
- * Map of failed constructions. \r
- */\r
- protected Map<Binding, SerializerConstructionException> failures = new HashMap<Binding, SerializerConstructionException>();\r
- \r
- /**\r
- * Repository where serializers are placed. \r
- */\r
- Map<Binding, Serializer> repository; \r
- \r
- /**\r
- * Map that contains in incomplete constructions.\r
- */\r
- Map<Binding, Serializer> inprogress = new HashMap<Binding, Serializer>();\r
- \r
- /**\r
- * Construct a new serializer.\r
- */\r
- public SerializerFactory() {\r
- this.repository = new HashMap<Binding, Serializer>();\r
- }\r
- \r
- /**\r
- * Construct a new serializer factory that places constructed serializers\r
- * into user given repository.\r
- * \r
- * @param repository\r
- */\r
- public SerializerFactory(Map<Binding, Serializer> repository) {\r
- this.repository = repository;\r
- }\r
- \r
- public Map<Binding, Serializer> getRepository() {\r
- return repository;\r
- }\r
- \r
- /**\r
- * Constructs a serilizer for a binding. Implement this.\r
- * It should use the inprogress -map for construction of \r
- * serializers that have component types.\r
- * \r
- * e.g. \r
- * inprogress.put(binding, notCompletelyConstructedSerializer);\r
- * Serializer componentSerializer = construct( componentBinding );\r
- * notCompletelyConstructedSerializer.setComponent( componentSerializer );\r
- * inprogress.remove(binding);\r
- * \r
- * try-finally is not needed.\r
- * \r
- * @param request\r
- * @return\r
- * @throws SerializerConstructionException\r
- */\r
- protected abstract Serializer doConstruct(Binding request) throws SerializerConstructionException;\r
- \r
- public Serializer construct(Binding request) throws SerializerConstructionException\r
- {\r
- // Optimization: if binding provides a cached serializer, just return it.\r
- { Serializer ser = request.cachedSerializer(); if (ser != null) return ser; } \r
- { Serializer ser = repository.get(request); if(ser != null) return ser; }\r
- { Serializer ser = inprogress.get(request); if(ser != null) return ser; }\r
- { SerializerConstructionException e = failures.get(request); if(e != null) throw e; }\r
- \r
- // Start construction\r
- try { \r
- Serializer binding = doConstruct(request);\r
- repository.put(request, binding);\r
- request.cacheSerializer(binding);\r
- return binding;\r
- } catch (SerializerConstructionException e) {\r
- inprogress.remove( request );\r
- failures.put(request, e);\r
- throw e;\r
- }\r
- }\r
-\r
- @Override\r
- public Serializer getSerializer(Binding binding)\r
- throws SerializerConstructionException { \r
- return construct(binding);\r
- }\r
- \r
- public Serializer getSerializerUnchecked(Binding binding)\r
- throws RuntimeSerializerConstructionException {\r
- try {\r
- return construct(binding);\r
- } catch (SerializerConstructionException e) {\r
- throw new RuntimeSerializerConstructionException(e);\r
- }\r
- } \r
- \r
- \r
-}\r
+package org.simantics.databoard.serialization;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.simantics.databoard.binding.Binding;
+
+/**
+ *
+ *
+ * @author Toni Kalajainen
+ */
+public abstract class SerializerFactory implements SerializerScheme {
+
+ /**
+ * Map of failed constructions.
+ */
+ protected Map<Binding, SerializerConstructionException> failures = new HashMap<Binding, SerializerConstructionException>();
+
+ /**
+ * Repository where serializers are placed.
+ */
+ Map<Binding, Serializer> repository;
+
+ /**
+ * Map that contains in incomplete constructions.
+ */
+ Map<Binding, Serializer> inprogress = new HashMap<Binding, Serializer>();
+
+ /**
+ * Construct a new serializer.
+ */
+ public SerializerFactory() {
+ this.repository = new HashMap<Binding, Serializer>();
+ }
+
+ /**
+ * Construct a new serializer factory that places constructed serializers
+ * into user given repository.
+ *
+ * @param repository
+ */
+ public SerializerFactory(Map<Binding, Serializer> repository) {
+ this.repository = repository;
+ }
+
+ public Map<Binding, Serializer> getRepository() {
+ return repository;
+ }
+
+ /**
+ * Constructs a serilizer for a binding. Implement this.
+ * It should use the inprogress -map for construction of
+ * serializers that have component types.
+ *
+ * e.g.
+ * inprogress.put(binding, notCompletelyConstructedSerializer);
+ * Serializer componentSerializer = construct( componentBinding );
+ * notCompletelyConstructedSerializer.setComponent( componentSerializer );
+ * inprogress.remove(binding);
+ *
+ * try-finally is not needed.
+ *
+ * @param request
+ * @return
+ * @throws SerializerConstructionException
+ */
+ protected abstract Serializer doConstruct(Binding request) throws SerializerConstructionException;
+
+ public Serializer construct(Binding request) throws SerializerConstructionException
+ {
+ // Optimization: if binding provides a cached serializer, just return it.
+ { Serializer ser = request.cachedSerializer(); if (ser != null) return ser; }
+ { Serializer ser = repository.get(request); if(ser != null) return ser; }
+ { Serializer ser = inprogress.get(request); if(ser != null) return ser; }
+ { SerializerConstructionException e = failures.get(request); if(e != null) throw e; }
+
+ // Start construction
+ try {
+ Serializer binding = doConstruct(request);
+ repository.put(request, binding);
+ request.cacheSerializer(binding);
+ return binding;
+ } catch (SerializerConstructionException e) {
+ inprogress.remove( request );
+ failures.put(request, e);
+ throw e;
+ }
+ }
+
+ @Override
+ public Serializer getSerializer(Binding binding)
+ throws SerializerConstructionException {
+ return construct(binding);
+ }
+
+ public Serializer getSerializerUnchecked(Binding binding)
+ throws RuntimeSerializerConstructionException {
+ try {
+ return construct(binding);
+ } catch (SerializerConstructionException e) {
+ throw new RuntimeSerializerConstructionException(e);
+ }
+ }
+
+
+}