]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/util/IsReferableQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / util / IsReferableQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2010- 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.util;
13
14 import java.util.IdentityHashMap;
15
16 import org.simantics.databoard.type.ArrayType;
17 import org.simantics.databoard.type.BooleanType;
18 import org.simantics.databoard.type.ByteType;
19 import org.simantics.databoard.type.Component;
20 import org.simantics.databoard.type.Datatype;
21 import org.simantics.databoard.type.Datatype.Visitor;
22 import org.simantics.databoard.type.DoubleType;
23 import org.simantics.databoard.type.FloatType;
24 import org.simantics.databoard.type.IntegerType;
25 import org.simantics.databoard.type.LongType;
26 import org.simantics.databoard.type.MapType;
27 import org.simantics.databoard.type.OptionalType;
28 import org.simantics.databoard.type.RecordType;
29 import org.simantics.databoard.type.StringType;
30 import org.simantics.databoard.type.UnionType;
31 import org.simantics.databoard.type.VariantType;
32
33 public class IsReferableQuery implements Visitor<Result> {
34         
35         public static Result isReferable(Datatype type) {
36                 IsReferableQuery query = new IsReferableQuery();
37                 Result result = type.accept(query);
38                 return result;
39         }
40         
41         IdentityHashMap<Datatype, Result> visited = new IdentityHashMap<Datatype, Result>();    
42         
43         @Override
44         public Result visit(ArrayType b) {
45                 visited.put(b, Result.No);
46                 Datatype ct = b.componentType();
47                 Result cr = visited.get(ct);
48                 if (cr!=null) return cr;                
49                 cr = ct.accept(this);
50                 if (cr!=Result.No) visited.put(b, cr);          
51                 return cr; 
52         }
53
54         @Override
55         public Result visit(BooleanType b) {
56                 return null;
57         }
58
59         @Override
60         public Result visit(DoubleType b) {
61                 return null;
62         }
63
64         @Override
65         public Result visit(FloatType b) {
66                 return null;
67         }
68
69         @Override
70         public Result visit(IntegerType b) {
71                 return null;
72         }
73
74         @Override
75         public Result visit(ByteType b) {
76                 return null;
77         }
78
79         @Override
80         public Result visit(LongType b) {
81                 return null;
82         }
83
84         @Override
85         public Result visit(OptionalType b) {
86                 visited.put(b, Result.No);
87                 Datatype ct = b.getComponentType();
88                 Result cr = visited.get(ct);
89                 if (cr!=null) return cr;                
90                 cr = ct.accept(this);
91                 if (cr!=Result.No) visited.put(b, cr);          
92                 return cr; 
93         }
94
95         @Override
96         public Result visit(RecordType b) {             
97                 if (b.isReferable()) {
98                         visited.put(b, Result.Yes);
99                         return Result.Yes;
100                 }
101                                 
102                 Result result = Result.No;
103                 visited.put(b, result);
104                 
105                 for (Component c : b.getComponents()) {
106                         Datatype ct = c.type;
107                         
108                         Result cr = visited.get(ct);
109                         if (cr==null) cr = ct.accept(this);
110                         if (cr!=null) {
111                                 if (cr == Result.Yes) {
112                                         result = cr;
113                                         break;
114                                 }
115                                 if (cr == Result.Possible) {
116                                         result = cr;                                    
117                                 }
118                         }                       
119                 }
120                 
121                 if (result!=Result.No) visited.put(b, result);          
122                 return result;
123         }
124
125         @Override
126         public Result visit(StringType b) {
127                 return Result.No;
128         }
129
130         @Override
131         public Result visit(UnionType b) {
132                 Result result = Result.No;
133                 visited.put(b, result);
134                 
135                 for (Component c : b.getComponents()) {
136                         Datatype ct = c.type;
137                         
138                         Result cr = visited.get(ct);
139                         if (cr==null) cr = ct.accept(this);
140                         if (cr!=null) {
141                                 if (cr == Result.Yes) {
142                                         result = cr;
143                                         break;
144                                 }
145                                 if (cr == Result.Possible) {
146                                         result = cr;                                    
147                                 }
148                         }                       
149                 }
150                 
151                 if (result!=Result.No) visited.put(b, result);          
152                 return result;
153         }
154
155         @Override
156         public Result visit(VariantType b) {
157                 return Result.Possible;
158         }
159
160         @Override
161         public Result visit(MapType b) {
162                 visited.put(b, Result.No);
163                 Datatype kt = b.keyType;
164                 Result kr = visited.get(kt);
165                 if (kr == null) kr = kt.accept(this);
166                 
167                 Datatype vt = b.valueType;
168                 Result vr = visited.get(vt);
169                 if (vr == null) vr = vt.accept(this);
170                 
171                 Result result = Result.No;
172                 if (kr==Result.Yes || vr==Result.Yes) result = Result.Yes;
173                 else if (kr==Result.Possible || vr==Result.Possible) result = Result.Possible;
174
175                 if (result!=Result.No) 
176                         visited.put(b, Result.No);
177                 
178                 return result; 
179         }
180         
181 }
182