]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/BindingExample.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / BindingExample.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.example.old;
13
14 import java.awt.geom.Rectangle2D;
15 import java.io.IOException;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.Datatypes;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.databoard.binding.RecordBinding;
21 import org.simantics.databoard.binding.error.BindingException;
22 import org.simantics.databoard.type.Datatype;
23
24 /**
25  * {@link Binding} is a link between an instance and a {@link Datatype}.
26  * {@link Datatypes#getContentBinding(Class)} creates binding object which is based on
27  * reflection. Although the performance is good, it can be improved by writing
28  * a taylored binding by hand.
29  * <p>
30  * The example demonstrates how to write binding to {@link Rectangle2D}.
31  *
32  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
33  */
34 public class BindingExample {
35         
36         static class Rectangle2DBinding extends RecordBinding {
37                 
38                 public Rectangle2DBinding() {
39                         componentBindings = new Binding[4];
40                         componentBindings[0] = Bindings.getBindingUnchecked(Double.class);
41                         componentBindings[1] = Bindings.getBindingUnchecked(Double.class);
42                         componentBindings[2] = Bindings.getBindingUnchecked(Double.class);
43                         componentBindings[3] = Bindings.getBindingUnchecked(Double.class);
44
45                         type = Datatypes.getDatatypeUnchecked( Rectangle2D.Double.class );
46                 }
47
48                 @Override
49                 public Object create(Object... value) {
50                         return new Rectangle2D.Double((Double)value[0], (Double)value[1], (Double)value[2], (Double)value[3]);
51                 }
52
53                 @Override
54                 public Object getComponent(Object obj, int index) {
55                         Rectangle2D rect = (Rectangle2D) obj;
56                         switch (index) {
57                         case 0: return rect.getX();
58                         case 1: return rect.getY();
59                         case 2: return rect.getWidth();
60                         case 3: return rect.getHeight();
61                         default: throw new IllegalArgumentException("index out of range, expected 0..3, got "+index);
62                         }
63                 }
64
65                 @Override
66                 public Object createPartial() throws BindingException {
67                         return new Rectangle2D.Double();
68                 }
69
70                 @Override
71                 public void setComponents(Object obj, Object... value) {
72                         Rectangle2D rect = (Rectangle2D) obj;
73                         rect.setFrame((Double)value[0], (Double)value[1], (Double)value[2], (Double)value[3]);
74                 }
75                 
76                 @Override
77                 public void setComponent(Object obj, int index, Object value)
78                                 throws BindingException {
79                         Rectangle2D rect = (Rectangle2D) obj;
80                         switch (index) {
81                         case 0: rect.setRect((Double)value, rect.getY(), rect.getWidth(), rect.getHeight()); break;
82                         case 1: rect.setRect(rect.getX(), (Double)value, rect.getWidth(), rect.getHeight()); break;
83                         case 2: rect.setRect(rect.getX(), rect.getY(), (Double)value, rect.getHeight()); break;
84                         case 3: rect.setRect(rect.getX(), rect.getY(), rect.getWidth(), (Double)value); break;
85                         default: throw new IllegalArgumentException("index out of range, expected 0..3, got "+index);
86                         }
87                 }
88
89                 @Override
90                 public boolean isInstance(Object obj) {
91                         return obj instanceof Rectangle2D;
92                 }
93                 
94                 @Override
95                 public void setDouble(Object r, int index, double x)
96                                 throws BindingException {
97                         Rectangle2D.Double rect = (Rectangle2D.Double) r;
98                         switch (index) {
99                         case 0: rect.x = x; break;
100                         case 1: rect.y = x; break;
101                         case 2: rect.width = x; break;
102                         case 3: rect.height = x; break;
103                         default: throw new IllegalArgumentException("index out of range, expected 0..3, got "+index);
104                         }
105                 }
106                 
107                 @Override
108                 public double getDouble(Object r, int index) throws BindingException {
109                         Rectangle2D.Double rect = (Rectangle2D.Double) r;
110                         switch (index) {
111                         case 0: return rect.x; 
112                         case 1: return rect.y;
113                         case 2: return rect.width;
114                         case 3: return rect.height;
115                         default: throw new IllegalArgumentException("index out of range, expected 0..3, got "+index);
116                         }
117                 }
118                 
119         }
120         
121         public static void main(String[] args) throws IOException, BindingException 
122         {
123                 
124                 Datatype dataType = Datatypes.getDatatypeUnchecked(Rectangle2D.Double.class);
125                 System.out.println(dataType);
126                 Binding reflectionBinding = Bindings.getBindingUnchecked(Rectangle2D.Double.class);             
127                 Binding handWrittenBinding = new Rectangle2DBinding(); 
128                 
129                 System.out.println("Lets measure performance... (4,000,000 reads and 1,000,000 writes*4)");
130                 
131                 Rectangle2D rect = new Rectangle2D.Double(5, 5, 100, 200);
132                 
133                 for ( int i=0; i<10; i++ ) {
134                         long time1 = testBinding(reflectionBinding, rect);
135                         long time2 = testBinding(handWrittenBinding, rect);
136                         
137                         time1 = testBinding(reflectionBinding, rect);
138                         time2 = testBinding(handWrittenBinding, rect);
139         
140                         System.out.println("Time of reflection based binding: "+time1+" ms");
141                         System.out.println("Time of Hand-Written binding: "+time2+" ms");
142                 }
143         }
144         
145         static long testBinding(Binding b, Rectangle2D rect) throws BindingException
146         {
147                 RecordBinding record = (RecordBinding) b;
148                 Runtime.getRuntime().gc();              
149                 long timeAtStart = System.currentTimeMillis();
150                 for (int i=0; i<10000000; i++)
151                 {
152                         record.getComponent(rect, 0); 
153                         record.getComponent(rect, 1); 
154                         record.getComponent(rect, 2); 
155                         record.getComponent(rect, 3);
156                         record.setComponents(rect, 5.0, 5.0, 100.0, 200.0);                                             
157                 }
158                 return System.currentTimeMillis() - timeAtStart;                
159         }
160         
161
162 }
163