--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.binding.classfactory;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.factory.TypeRepository;\r
+import org.simantics.databoard.binding.reflection.BindingRequest;\r
+import org.simantics.databoard.type.Datatype;\r
+\r
+/**\r
+ * Creates class for a type. \r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class TypeClassFactory {\r
+ \r
+ /**\r
+ * Map of failed constructions. \r
+ */\r
+ Map<Datatype, BindingConstructionException> failures = new HashMap<Datatype, BindingConstructionException>();\r
+ \r
+ /**\r
+ * Map that contains in incomplete constructions.\r
+ */\r
+ Map<Datatype, Datatype> inprogress = new HashMap<Datatype, Datatype>();\r
+ \r
+ /**\r
+ * Repository where successful constructions are placed. \r
+ */\r
+ TypeRepository repository;\r
+ \r
+ List<TypeClassSubFactory> subFactories = new ArrayList<TypeClassSubFactory>(); \r
+ \r
+ public TypeClassFactory() {\r
+ repository = new TypeRepository(); \r
+ }\r
+ \r
+ public TypeClassFactory(TypeRepository repository)\r
+ {\r
+ this.repository = repository;\r
+ } \r
+ \r
+ public void addFactory(TypeClassSubFactory factory) \r
+ {\r
+ if (!subFactories.contains(factory)) {\r
+ this.subFactories.add(factory);\r
+ }\r
+ }\r
+ \r
+ public void addFactoryFirst(TypeClassSubFactory factory) \r
+ {\r
+ if (!subFactories.contains(factory)) {\r
+ this.subFactories.add(0, factory);\r
+ }\r
+ }\r
+ \r
+ \r
+ public BindingRequest getClass(Datatype type) throws BindingConstructionException\r
+ {\r
+ Datatype progress = inprogress.get(type);\r
+ if (progress != null) { synchronized(progress) {} }\r
+ if (failures.containsKey(type)) throw failures.get(type);\r
+ if (repository.containsType(type)) return repository.getRequestForType(type);\r
+ \r
+ // Start construction\r
+ synchronized(type) {\r
+ inprogress.put(type, type);\r
+ try { \r
+ BindingRequest br = null;\r
+ for ( TypeClassSubFactory sf : subFactories ) {\r
+ br = sf.construct(this, type);\r
+ if ( br != null ) break;\r
+ }\r
+ if (br==null) throw new BindingConstructionException("Failed to create BindingRequest for type "+type);\r
+ repository.put(type, br);\r
+ return br;\r
+ } catch (RuntimeException e) {\r
+ failures.put(type, new BindingConstructionException(e));\r
+ throw e;\r
+ } catch (BindingConstructionException bce) {\r
+ failures.put(type, bce);\r
+ throw bce;\r
+ } finally {\r
+ inprogress.remove(type);\r
+ }\r
+ }\r
+ \r
+ }\r
+ \r
+ public TypeRepository getRepository() {\r
+ return repository;\r
+ }\r
+ \r
+}\r