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.IdentityHashMap;
16 import org.simantics.databoard.Datatypes;
17 import org.simantics.databoard.type.ArrayType;
18 import org.simantics.databoard.type.BooleanType;
19 import org.simantics.databoard.type.ByteType;
20 import org.simantics.databoard.type.Datatype;
21 import org.simantics.databoard.type.DoubleType;
22 import org.simantics.databoard.type.FloatType;
23 import org.simantics.databoard.type.IntegerType;
24 import org.simantics.databoard.type.LongType;
25 import org.simantics.databoard.type.MapType;
26 import org.simantics.databoard.type.OptionalType;
27 import org.simantics.databoard.type.RecordType;
28 import org.simantics.databoard.type.StringType;
29 import org.simantics.databoard.type.UnionType;
30 import org.simantics.databoard.type.VariantType;
33 * Signature Visitor builds a signature string from a datatype.
34 * The argument is StringBuilder.
36 * Signature is construtructed with the following notation.
45 * R Referable RecordType
46 * o* OptionalType, * denotes componentType
47 * a* ArrayType, * denotes componentType
48 * r**e RecordType, ** denotes fields
49 * u UnionType, * denotes components
50 * m** MapType, ** denotes keyType and valueType
53 * For example, The signature of UUID.class is "rjje"
55 * @author toni.kalajainen
57 public class SignatureVisitor implements Datatype.Visitor1 {
59 public StringBuilder sb = new StringBuilder();
60 public int hashcode = 0xbcbcbca;
61 public IdentityHashMap<Datatype, Boolean> visited = new IdentityHashMap<Datatype, Boolean>();
63 public static String toSignature(Datatype type)
65 SignatureVisitor sv = new SignatureVisitor();
66 type.accept( sv, null );
67 String sig = sv.sb.toString();
72 public void visit(ArrayType b, Object obj) {
74 b.componentType.accept(this, obj);
75 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
79 public void visit(BooleanType b, Object obj) {
81 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
85 public void visit(DoubleType b, Object obj) {
87 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
91 public void visit(FloatType b, Object obj) {
93 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
97 public void visit(IntegerType b, Object obj) {
99 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
103 public void visit(ByteType b, Object obj) {
105 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
109 public void visit(LongType b, Object obj) {
111 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
115 public void visit(OptionalType b, Object obj) {
117 b.componentType.accept(this, sb);
118 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
122 public void visit(RecordType b, Object obj) {
123 if ( wasVisited(b) ) {
127 if ( b.isReferable() ) {
133 for (int i=0; i<b.getComponentCount(); i++) {
134 b.getComponentType(i).accept(this, sb);
138 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
139 for (int i=0; i<b.getComponentCount(); i++) {
140 hashcode = 13*hashcode + b.getComponent(i).name.hashCode();
145 public void visit(StringType b, Object obj) {
147 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
151 public void visit(UnionType b, Object obj) {
152 if ( b.equals( Datatypes.getDatatypeUnchecked(Datatype.class) )) {
156 if ( wasVisited(b) ) {
161 for (int i=0; i<b.getComponentCount(); i++) {
162 b.getComponentType(i).accept(this, sb);
165 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
169 public void visit(VariantType b, Object obj) {
171 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
175 public void visit(MapType b, Object obj) {
176 if ( wasVisited(b) ) return;
178 b.keyType.accept(this, obj);
179 b.valueType.accept(this, obj);
180 hashcode = 13*hashcode + b.getClass().getName().hashCode() + 133*b.metadataHashCode();
184 * Visit and return if was visited
188 boolean wasVisited(Datatype type) {
189 Boolean result = visited.put(type, Boolean.TRUE);
190 return result == null ? false : result;