]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/method/Interface.java
Improved Bindings.getBinding(Class) caching for Datatype.class
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / method / Interface.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 java.util.Set;
15 import java.util.TreeSet;
16
17 /**
18  * An interface, a description of methods. 
19  *
20  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
21  */
22 public class Interface {
23
24         Set<MethodTypeDefinition> methodDefinitions = new TreeSet<MethodTypeDefinition>( MethodTypeDefinition.BINDING );
25         
26         public Interface()
27         {
28         }
29         
30         public Interface(MethodTypeDefinition...methodDefs)
31         {               
32                 for (MethodTypeDefinition def : methodDefs)
33                 {
34                         methodDefinitions.add(def);
35                 }
36         }
37                 
38         /**
39          * Add new method definition
40          * 
41          * @param methodDef
42          */
43         void add(MethodTypeDefinition methodDef)
44         {
45                 methodDefinitions.add(methodDef);
46         }
47
48         /**
49          * Remove method
50          * 
51          * @param methodDef method type definition
52          */
53         public void remove(MethodTypeDefinition methodDef)
54         {
55                 if ( !methodDefinitions.remove(methodDef) ) return;
56         }
57
58         /**
59          * Get ordered list of method descriptions
60          * 
61          * @return method descriptions
62          */
63         public MethodTypeDefinition[] getMethodDefinitions() {
64                 return methodDefinitions.toArray(new MethodTypeDefinition[methodDefinitions.size()]);
65         }
66         
67         /**
68          * Get ordered list of method descriptions
69          * 
70          * @return method descriptions
71          */
72         public Set<MethodTypeDefinition> getMethodDefinitionSet() {
73                 return methodDefinitions;
74         }
75         
76         @Override
77         public String toString() {
78                 if (methodDefinitions.isEmpty()) return "{}";
79                 
80                 StringBuilder sb = new StringBuilder();
81                 sb.append("{\n");
82                 for (MethodTypeDefinition def : methodDefinitions) {
83                         sb.append("  ");
84                         sb.append(def);
85                         sb.append("\n");
86                 }
87                 sb.append("}");
88                 return sb.toString();
89         }
90         
91         @Override
92         public boolean equals(Object obj) {
93                 if (obj instanceof Interface == false) return false;
94                 Interface other = (Interface) obj;
95                 if (!other.methodDefinitions.equals( methodDefinitions )) return false;
96                 return true;
97         }
98         
99         @Override
100         public int hashCode() {
101                 int result = 0x3a44a54a;
102                 for (MethodTypeDefinition def : methodDefinitions)
103                         result += def.hashCode();
104                 return result;
105         }
106         
107 }
108