1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.binding.classfactory;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
18 import org.simantics.databoard.binding.error.BindingConstructionException;
19 import org.simantics.databoard.binding.factory.TypeRepository;
20 import org.simantics.databoard.binding.reflection.BindingRequest;
21 import org.simantics.databoard.type.Datatype;
24 * Creates class for a type.
26 * @author toni.kalajainen
28 public class TypeClassFactory {
31 * Map of failed constructions.
33 Map<Datatype, BindingConstructionException> failures = new HashMap<Datatype, BindingConstructionException>();
36 * Map that contains in incomplete constructions.
38 Map<Datatype, Datatype> inprogress = new HashMap<Datatype, Datatype>();
41 * Repository where successful constructions are placed.
43 TypeRepository repository;
45 List<TypeClassSubFactory> subFactories = new ArrayList<TypeClassSubFactory>();
47 public TypeClassFactory() {
48 repository = new TypeRepository();
51 public TypeClassFactory(TypeRepository repository)
53 this.repository = repository;
56 public void addFactory(TypeClassSubFactory factory)
58 if (!subFactories.contains(factory)) {
59 this.subFactories.add(factory);
63 public void addFactoryFirst(TypeClassSubFactory factory)
65 if (!subFactories.contains(factory)) {
66 this.subFactories.add(0, factory);
71 public BindingRequest getClass(Datatype type) throws BindingConstructionException
73 Datatype progress = inprogress.get(type);
74 if (progress != null) { synchronized(progress) {} }
75 if (failures.containsKey(type)) throw failures.get(type);
76 if (repository.containsType(type)) return repository.getRequestForType(type);
80 inprogress.put(type, type);
82 BindingRequest br = null;
83 for ( TypeClassSubFactory sf : subFactories ) {
84 br = sf.construct(this, type);
85 if ( br != null ) break;
87 if (br==null) throw new BindingConstructionException("Failed to create BindingRequest for type "+type);
88 repository.put(type, br);
90 } catch (RuntimeException e) {
91 failures.put(type, new BindingConstructionException(e));
93 } catch (BindingConstructionException bce) {
94 failures.put(type, bce);
97 inprogress.remove(type);
103 public TypeRepository getRepository() {