]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/SerializerExample3.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / SerializerExample3.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 gnu.trove.map.hash.TObjectIntHashMap;
15
16 import java.awt.geom.Rectangle2D;
17 import java.io.DataInput;
18 import java.io.DataOutput;
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22
23 import org.simantics.databoard.Bindings;
24 import org.simantics.databoard.Datatypes;
25 import org.simantics.databoard.binding.Binding;
26 import org.simantics.databoard.binding.error.BindingConstructionException;
27 import org.simantics.databoard.binding.error.BindingException;
28 import org.simantics.databoard.binding.error.DatatypeConstructionException;
29 import org.simantics.databoard.serialization.SerializationException;
30 import org.simantics.databoard.serialization.Serializer;
31 import org.simantics.databoard.serialization.Serializer.NonRecursiveSerializer;
32 import org.simantics.databoard.serialization.SerializerConstructionException;
33 import org.simantics.databoard.type.Datatype;
34 import org.simantics.databoard.util.binary.BinaryReadable;
35 import org.simantics.databoard.util.binary.BinaryWriteable;
36 import org.simantics.databoard.util.binary.ByteBufferReadable;
37 import org.simantics.databoard.util.binary.ByteBufferWriteable;
38
39 /**
40  * {@link Serializer} is a link between a {@link Binding} and a binary data. 
41  * {@link Binding#serializer(SerializationFormat)} creates Serializer object 
42  * which is based on the binding. Although the performance is good, it can be 
43  * improved by writing a taylored serializer by hand.
44  * <p>
45  * The example demonstrates how to write create hand-written 
46  * {@link Serializer} implementation to {@link Rectangle2D}.
47  *
48  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
49  */
50 public class SerializerExample3 {
51         
52         static class Rectangle2DSerializer extends NonRecursiveSerializer {
53                 
54                 public static final Rectangle2DSerializer INSTANCE = new Rectangle2DSerializer();
55
56                 @Override
57                 public void serialize(DataOutput out, Object obj)
58                 throws IOException 
59                 {
60                         Rectangle2D rect = (Rectangle2D) obj;
61                         out.writeDouble(rect.getX());
62                         out.writeDouble(rect.getY());
63                         out.writeDouble(rect.getWidth());
64                         out.writeDouble(rect.getHeight());
65                 }
66                 
67                 @Override
68                 public Object deserialize(DataInput in)
69                 throws IOException {
70                         double x = in.readDouble();
71                         double y = in.readDouble();
72                         double width = in.readDouble();
73                         double height = in.readDouble();
74                         return new Rectangle2D.Double(x, y, width, height);
75                 }
76                 
77                 @Override
78                 public void deserializeTo(DataInput in, Object obj)
79                                 throws IOException {
80                         Rectangle2D.Double r = (Rectangle2D.Double) obj;
81                         r.x = in.readDouble();
82                         r.y = in.readDouble();
83                         r.width = in.readDouble();
84                         r.height = in.readDouble();
85                 }
86
87                 @Override
88                 public Integer getConstantSize() {
89                         return 8*4;
90                 }
91
92                 @Override
93                 public int getSize(Object obj) {
94                         return 8*4;
95                 }
96
97                 @Override
98                 public void skip(DataInput in) throws IOException {
99                         in.skipBytes(8*4);
100                 }
101                 
102                 @Override
103                 public int getMinSize() {
104                         return 8*4;
105                 }
106                 
107                 
108         }
109         
110         public static void main(String[] args) throws IOException, SerializerConstructionException, SerializationException, BindingException, DatatypeConstructionException, BindingConstructionException 
111         {
112                 
113                 Datatype dataType = Datatypes.getDatatype(Rectangle2D.Double.class);
114                 System.out.println(dataType);
115                 Binding reflectionBinding = Bindings.getBinding(Rectangle2D.Double.class);              
116                 Binding handWrittenBinding = new BindingExample.Rectangle2DBinding(); 
117                 
118                 for (int i=0; i<10; i++) {
119                 
120                         System.out.println((i+1)+": Lets measure serialization performance... (1,000,000 serialization + deserialization)");
121                         Serializer serializer1 = reflectionBinding.serializer();
122                         Serializer serializer2 = handWrittenBinding.serializer();
123                         Serializer serializer3 = Rectangle2DSerializer.INSTANCE;
124                         
125                         long time1 = getTime(serializer1);
126                         long time2 = getTime(serializer2);
127                         long time3 = getTime(serializer3);
128                         
129                         System.out.println("Time of Reflection Binding + Automatic Serializer: "+time1+" ms");
130                         System.out.println("Time of Hand-Written Binding + Automatic Serializer: "+time2+" ms");
131                         System.out.println("Time of Hand-Written Binding + Hand-Written Serializer: "+time3+" ms");
132                         System.out.println();
133                 }
134         }
135         
136         static long getTime(Serializer serializer) 
137         throws IOException, SerializationException, BindingException
138         {               
139                 Rectangle2D rect = new Rectangle2D.Double(5, 5, 100, 200);
140                 TObjectIntHashMap<Object> identities = new TObjectIntHashMap<Object>(0);
141                 int size = serializer.getSize(rect, identities);                
142                 identities.clear();
143                 ByteBuffer data = ByteBuffer.allocate( size );
144                 ArrayList<Object> identities2 = new ArrayList<Object>();
145                 BinaryWriteable out = new ByteBufferWriteable(data);
146                 BinaryReadable in = new ByteBufferReadable(data);
147                 
148                 Runtime.getRuntime().gc();              
149                 long timeAtStart = System.currentTimeMillis();
150                 for (int i=0; i<1000000; i++)
151                 {
152                         serializer.serialize(out, identities, rect);
153                         data.rewind();
154                         serializer.deserialize(in, identities2);
155                         data.rewind();
156                 }
157                 return System.currentTimeMillis() - timeAtStart;
158                 
159         }
160         
161
162 }
163