X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fclassfactory%2FTypeClassFactory.java;h=1896e0739c41515a5811dedb26e2a0237e991cda;hb=refs%2Fchanges%2F65%2F2565%2F2;hp=3931c893bf3c07de1b57103a62cd46da814b481a;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/classfactory/TypeClassFactory.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/classfactory/TypeClassFactory.java index 3931c893b..1896e0739 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/classfactory/TypeClassFactory.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/classfactory/TypeClassFactory.java @@ -1,107 +1,107 @@ -/******************************************************************************* - * 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 failures = new HashMap(); - - /** - * Map that contains in incomplete constructions. - */ - Map inprogress = new HashMap(); - - /** - * Repository where successful constructions are placed. - */ - TypeRepository repository; - - List subFactories = new ArrayList(); - - 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; - } - -} +/******************************************************************************* + * 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 failures = new HashMap(); + + /** + * Map that contains in incomplete constructions. + */ + Map inprogress = new HashMap(); + + /** + * Repository where successful constructions are placed. + */ + TypeRepository repository; + + List subFactories = new ArrayList(); + + 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; + } + +}