]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/BeanExample.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / BeanExample.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 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;
13
14 import java.io.IOException;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.simantics.databoard.annotations.Identifier;
19 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
20 import org.simantics.databoard.util.Bean;
21
22 public class BeanExample {
23
24         public static class MyBean extends Bean {
25                 public String name;
26                 public List<Integer> values;
27         }
28
29         public static class MyBean2 extends Bean.Id {
30                 public @Identifier String name;
31                 public List<Integer> values;
32         }
33         
34         public static void main(String[] args) throws IOException, DataTypeSyntaxError {
35                 
36                 // Init
37                 MyBean bean = new MyBean();
38                 bean.init();
39                 bean.name = "MyBean";
40                 bean.values.add(5);
41                 bean.values.add(4);
42                 bean.values.add(2);
43                 bean.values.add(1);
44                 bean.values.add(0);
45
46                 // toString
47                 System.out.println( "toString()");
48                 System.out.println( "  Bean = " + bean );
49                 System.out.println( "" );
50                 
51                 // Serialization
52                 System.out.println( "Serialization" );
53                 byte[] data = bean.serialize();
54                 MyBean bean2 = new MyBean();
55                 bean2.deserialize( data );
56                 System.out.println( "  Serialize(Bean) = " + Arrays.toString(data) );
57                 System.out.println( "  Deserialize(data) = " + bean2 );
58                 System.out.println( "" );
59                 
60                 // JSON
61                 System.out.println("JSON");
62                 String json = bean.print();
63                 System.out.println( "  JSON(Bean3) = " + json);
64                 MyBean bean3 = new MyBean();
65                 bean3.parse( json );
66                 System.out.println( "  Parse(json) = " + bean3 );
67                 System.out.println( "" );
68                 
69                 // Hash-Equals
70                 System.out.println( "Hash-Equals" );
71                 System.out.println( "  hashCode(Bean) = " + bean.hashCode() );
72                 System.out.println( "  hashCode(Bean2) = " + bean2.hashCode() );
73                 System.out.println( "  equals(Bean, Bean2) = " + bean.equals(bean2) );
74                 System.out.println( "" );
75                 
76                 // Comparator
77                 System.out.println( "Compare");
78                 System.out.println( "  compare(Bean, Bean2) = " + bean.compareTo(bean2) );
79                 System.out.println( "" );
80                 
81                 // Clone
82                 MyBean bean4 = (MyBean) bean.clone();
83                 System.out.println( "Clone" );
84                 System.out.println( "  Bean4 = Bean.Clone()" );
85                 System.out.println( "  Bean4 = "+bean4 );
86                 System.out.println( "" );
87                 
88                 // ReadFrom
89                 System.out.println( "ReadFrom");                
90                 System.out.println( "  Bean.Name = \""+bean.name+"\"");
91                 bean4.name = "New Name";
92                 System.out.println( "  Bean4.Name <-- \""+bean4.name+"\"");
93                 System.out.println( "  Bean.readFrom(Bean4)");
94                 bean.readFrom( bean4 );
95                 System.out.println( "  Bean.Name = \""+bean.name+"\"");
96                 
97                 
98         }
99         
100 }