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.parser.unparsing;
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.procedure.TObjectObjectProcedure;
16 import gnu.trove.set.hash.THashSet;
18 import org.simantics.databoard.type.ArrayType;
19 import org.simantics.databoard.type.BooleanType;
20 import org.simantics.databoard.type.ByteType;
21 import org.simantics.databoard.type.Component;
22 import org.simantics.databoard.type.Datatype;
23 import org.simantics.databoard.type.Datatype.Visitor;
24 import org.simantics.databoard.type.DoubleType;
25 import org.simantics.databoard.type.FloatType;
26 import org.simantics.databoard.type.IntegerType;
27 import org.simantics.databoard.type.LongType;
28 import org.simantics.databoard.type.MapType;
29 import org.simantics.databoard.type.OptionalType;
30 import org.simantics.databoard.type.RecordType;
31 import org.simantics.databoard.type.StringType;
32 import org.simantics.databoard.type.UnionType;
33 import org.simantics.databoard.type.VariantType;
37 * @author Hannu Niemistö
39 public class DataTypePrinter2 {
43 THashMap<Datatype, String> refs = new THashMap<Datatype, String>();
45 private DataTypePrinter2(StringBuilder sb) {
51 for(int i=0;i<indentation;++i)
55 boolean useRef = true;
56 Visitor<Object> printVisitor = new Visitor<Object>() {
59 public Object visit(ArrayType b) {
62 b.componentType.accept(printVisitor);
68 public Object visit(BooleanType b) {
75 public Object visit(DoubleType b) {
82 public Object visit(FloatType b) {
89 public Object visit(IntegerType b) {
96 public Object visit(ByteType b) {
103 public Object visit(LongType b) {
110 public Object visit(OptionalType b) {
112 sb.append("Optional(");
113 b.componentType.accept(printVisitor);
119 public Object visit(RecordType b) {
120 if(refs.containsKey(b) && useRef)
121 sb.append(refs.get(b));
126 for(int i=0;i<b.getComponentCount();++i) {
129 Component c = b.getComponent(i);
133 c.type.accept(printVisitor);
143 public Object visit(StringType b) {
150 public Object visit(UnionType b) {
151 if(refs.containsKey(b) && useRef)
152 sb.append(refs.get(b));
156 for(int i=0;i<b.getComponentCount();++i) {
159 Component c = b.getComponent(i);
162 c.type.accept(printVisitor);
170 public Object visit(VariantType b) {
172 sb.append("Variant");
177 public Object visit(MapType b) {
180 b.keyType.accept(printVisitor);
182 b.valueType.accept(printVisitor);
189 THashSet<Datatype> seen = new THashSet<Datatype>();
191 private void collectRefs(Datatype dt) {
192 if(dt instanceof RecordType || dt instanceof UnionType) {
194 if(!refs.containsKey(dt))
195 refs.put(dt, "T" + refs.size());
199 dt.accept(refCollectVisitor);
202 Visitor<Object> refCollectVisitor = new Visitor<Object>() {
205 public Object visit(ArrayType b) {
206 collectRefs(b.componentType);
211 public Object visit(BooleanType b) {
216 public Object visit(DoubleType b) {
221 public Object visit(FloatType b) {
226 public Object visit(IntegerType b) {
231 public Object visit(ByteType b) {
236 public Object visit(LongType b) {
241 public Object visit(OptionalType b) {
242 collectRefs(b.componentType);
247 public Object visit(RecordType b) {
248 for(Component c : b.getComponents())
254 public Object visit(StringType b) {
259 public Object visit(UnionType b) {
260 for(Component c : b.getComponents())
266 public Object visit(VariantType b) {
271 public Object visit(MapType b) {
272 collectRefs(b.keyType);
273 collectRefs(b.valueType);
279 void printDt(Datatype dt) {
281 dt.accept(printVisitor);
282 if(!refs.isEmpty()) {
286 refs.forEachEntry(new TObjectObjectProcedure<Datatype, String>() {
288 public boolean execute(Datatype a, String b) {
290 sb.append(b).append(" = ");
292 a.accept(printVisitor);
301 public static String print(Datatype dt) {
302 StringBuilder sb = new StringBuilder();
303 new DataTypePrinter2(sb).printDt(dt);
304 return sb.toString();