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.example.old;
14 import java.awt.geom.Rectangle2D;
15 import java.io.IOException;
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;
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.
30 * The example demonstrates how to write binding to {@link Rectangle2D}.
32 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
34 public class BindingExample {
36 static class Rectangle2DBinding extends RecordBinding {
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);
45 type = Datatypes.getDatatypeUnchecked( Rectangle2D.Double.class );
49 public Object create(Object... value) {
50 return new Rectangle2D.Double((Double)value[0], (Double)value[1], (Double)value[2], (Double)value[3]);
54 public Object getComponent(Object obj, int index) {
55 Rectangle2D rect = (Rectangle2D) obj;
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);
66 public Object createPartial() throws BindingException {
67 return new Rectangle2D.Double();
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]);
77 public void setComponent(Object obj, int index, Object value)
78 throws BindingException {
79 Rectangle2D rect = (Rectangle2D) obj;
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);
90 public boolean isInstance(Object obj) {
91 return obj instanceof Rectangle2D;
95 public void setDouble(Object r, int index, double x)
96 throws BindingException {
97 Rectangle2D.Double rect = (Rectangle2D.Double) r;
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);
108 public double getDouble(Object r, int index) throws BindingException {
109 Rectangle2D.Double rect = (Rectangle2D.Double) r;
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);
121 public static void main(String[] args) throws IOException, BindingException
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();
129 System.out.println("Lets measure performance... (4,000,000 reads and 1,000,000 writes*4)");
131 Rectangle2D rect = new Rectangle2D.Double(5, 5, 100, 200);
133 for ( int i=0; i<10; i++ ) {
134 long time1 = testBinding(reflectionBinding, rect);
135 long time2 = testBinding(handWrittenBinding, rect);
137 time1 = testBinding(reflectionBinding, rect);
138 time2 = testBinding(handWrittenBinding, rect);
140 System.out.println("Time of reflection based binding: "+time1+" ms");
141 System.out.println("Time of Hand-Written binding: "+time2+" ms");
145 static long testBinding(Binding b, Rectangle2D rect) throws BindingException
147 RecordBinding record = (RecordBinding) b;
148 Runtime.getRuntime().gc();
149 long timeAtStart = System.currentTimeMillis();
150 for (int i=0; i<10000000; i++)
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);
158 return System.currentTimeMillis() - timeAtStart;