]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/classfactory/TypeClassFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / classfactory / TypeClassFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.classfactory;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
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;
22
23 /**
24  * Creates class for a type. 
25  * 
26  * @author toni.kalajainen
27  */
28 public class TypeClassFactory {
29         
30         /**
31          * Map of failed constructions. 
32          */
33         Map<Datatype, BindingConstructionException> failures = new HashMap<Datatype, BindingConstructionException>();
34         
35         /**
36          * Map that contains in incomplete constructions.
37          */
38         Map<Datatype, Datatype> inprogress = new HashMap<Datatype, Datatype>();
39         
40         /**
41          * Repository where successful constructions are placed. 
42          */
43         TypeRepository repository;
44         
45         List<TypeClassSubFactory> subFactories = new ArrayList<TypeClassSubFactory>();  
46         
47         public TypeClassFactory() {
48                 repository = new TypeRepository(); 
49         }
50         
51         public TypeClassFactory(TypeRepository repository)
52         {
53                 this.repository = repository;
54         }       
55         
56         public void addFactory(TypeClassSubFactory factory) 
57         {
58                 if (!subFactories.contains(factory)) {
59                         this.subFactories.add(factory);
60                 }
61         }
62         
63         public void addFactoryFirst(TypeClassSubFactory factory) 
64         {
65                 if (!subFactories.contains(factory)) {
66                         this.subFactories.add(0, factory);
67                 }
68         }
69         
70         
71         public BindingRequest getClass(Datatype type) throws BindingConstructionException
72         {
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);
77                 
78                 // Start construction
79                 synchronized(type) {
80                         inprogress.put(type, type);
81                         try {   
82                                 BindingRequest br = null;
83                                 for ( TypeClassSubFactory sf : subFactories ) {
84                                         br = sf.construct(this, type);
85                                         if ( br != null ) break;
86                                 }
87                                 if (br==null) throw new BindingConstructionException("Failed to create BindingRequest for type "+type);
88                                 repository.put(type, br);
89                                 return br;
90                         } catch (RuntimeException e) {
91                                 failures.put(type, new BindingConstructionException(e));
92                                 throw e;
93                         } catch (BindingConstructionException bce) {
94                                 failures.put(type, bce);
95                                 throw bce;
96                         } finally {
97                                 inprogress.remove(type);
98                         }
99                 }
100                 
101         }
102         
103         public TypeRepository getRepository() {
104                 return repository;
105         }
106         
107 }