]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/method/MethodTypeDefinition.java
Improved Bindings.getBinding(Class) caching for Datatype.class
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / method / MethodTypeDefinition.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.method;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.Datatypes;
16 import org.simantics.databoard.binding.Binding;
17 import org.simantics.databoard.type.Component;
18 import org.simantics.databoard.type.Datatype;
19 import org.simantics.databoard.type.UnionType;
20
21
22 public class MethodTypeDefinition implements Comparable<MethodTypeDefinition> {
23
24         public static Binding BINDING = Bindings.getBindingUnchecked( MethodTypeDefinition.class );
25         
26         String name;
27         MethodType type;
28         
29         private transient int hash;     
30         
31         public MethodTypeDefinition(String name, MethodType type)
32         {
33                 this.name = name;
34                 this.type = type;
35                 hash = name.hashCode() + type.hashCode()*7;
36         }
37
38         public String getName() {
39                 return name;
40         }
41
42         public void setName(String name) {
43                 this.name = name;
44                 hash = name.hashCode() + type.hashCode()*7;
45         }
46         
47         public MethodType getType() {
48                 return type;
49         }
50         
51         @Override
52         public String toString() {
53                 Datatype void_ = Datatypes.getDatatypeUnchecked(void.class); 
54                 StringBuilder sb = new StringBuilder();
55                 
56                 Datatype res = type.getResponseType();
57                 Datatype req = type.getRequestType();
58                 UnionType err = (UnionType) type.getErrorType();
59
60                 sb.append("method ");
61                 sb.append(name);
62                 sb.append(" : ");
63                 sb.append(req.toSingleLineString());
64                 sb.append(" -> ");
65                 
66                 if (res.equals(void_))
67                         sb.append("void");
68                 else
69                         sb.append(res.toSingleLineString());
70                 
71                 if (err.getComponentCount()>0) {
72                         sb.append(" throws ");
73                         for (int i=0; i<err.getComponentCount(); i++) {
74                                 if (i>0) sb.append(", ");
75                                 Component c = err.getComponent(i); 
76                                 sb.append( c.name );
77                                 sb.append( c.type.toSingleLineString() );
78                         }
79                 }
80                 return sb.toString();
81         }
82
83         @Override
84         public int hashCode() {
85                 return hash;
86         }
87         
88         @Override
89         public boolean equals(Object obj) {
90                 if (obj instanceof MethodTypeDefinition==false) return false;
91                 MethodTypeDefinition other = (MethodTypeDefinition) obj;
92                 return other.name.equals(name) && other.type.equals(type);
93         }
94
95         @Override
96         public int compareTo(MethodTypeDefinition o) {
97                 int c = name.compareTo( o.getName() );
98                 if (c!=0) return c;
99                 c = type.compareTo( o.getType() );
100                 return c;
101         }
102         
103 }
104