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