1 /*******************************************************************************
2 * Copyright (c) 2010- 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.util;
14 import java.util.IdentityHashMap;
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;
33 public class IsReferableQuery implements Visitor<Result> {
35 public static Result isReferable(Datatype type) {
36 IsReferableQuery query = new IsReferableQuery();
37 Result result = type.accept(query);
41 IdentityHashMap<Datatype, Result> visited = new IdentityHashMap<Datatype, Result>();
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;
50 if (cr!=Result.No) visited.put(b, cr);
55 public Result visit(BooleanType b) {
60 public Result visit(DoubleType b) {
65 public Result visit(FloatType b) {
70 public Result visit(IntegerType b) {
75 public Result visit(ByteType b) {
80 public Result visit(LongType b) {
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;
91 if (cr!=Result.No) visited.put(b, cr);
96 public Result visit(RecordType b) {
97 if (b.isReferable()) {
98 visited.put(b, Result.Yes);
102 Result result = Result.No;
103 visited.put(b, result);
105 for (Component c : b.getComponents()) {
106 Datatype ct = c.type;
108 Result cr = visited.get(ct);
109 if (cr==null) cr = ct.accept(this);
111 if (cr == Result.Yes) {
115 if (cr == Result.Possible) {
121 if (result!=Result.No) visited.put(b, result);
126 public Result visit(StringType b) {
131 public Result visit(UnionType b) {
132 Result result = Result.No;
133 visited.put(b, result);
135 for (Component c : b.getComponents()) {
136 Datatype ct = c.type;
138 Result cr = visited.get(ct);
139 if (cr==null) cr = ct.accept(this);
141 if (cr == Result.Yes) {
145 if (cr == Result.Possible) {
151 if (result!=Result.No) visited.put(b, result);
156 public Result visit(VariantType b) {
157 return Result.Possible;
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);
167 Datatype vt = b.valueType;
168 Result vr = visited.get(vt);
169 if (vr == null) vr = vt.accept(this);
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;
175 if (result!=Result.No)
176 visited.put(b, Result.No);