]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/ByteBindingDefault.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / ByteBindingDefault.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.binding.impl;
13
14 import org.simantics.databoard.binding.ByteBinding;\r
15 import org.simantics.databoard.binding.error.BindingException;\r
16 import org.simantics.databoard.binding.error.UnsupportedOperationException;\r
17 import org.simantics.databoard.type.ByteType;\r
18
19 /**
20  * Binds ByteType to java.lang.Byte-class
21  *
22  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
23  */
24 public class ByteBindingDefault extends ByteBinding {
25         
26         public ByteBindingDefault(ByteType type) {
27                 super(type);
28         }
29         
30         public Object create(byte value) {
31                 return Byte.valueOf(value);
32         }
33         
34         public Object create(Byte value) {
35                 return value;
36         }
37         
38         @Override
39         public Object create(Number value) 
40         {
41                 if (value.getClass()==Byte.class) return value;
42                 return Byte.valueOf( value.byteValue() );
43         }       
44         
45         @Override
46         public Object create(String value) throws BindingException {
47                 try {\r
48                         return Byte.parseByte(value);\r
49                 } catch (java.lang.NumberFormatException e) {\r
50                         throw new BindingException( e );\r
51                 }\r
52         }
53                 
54         public Byte getValue(Object obj)
55         throws BindingException
56         {
57                 if (obj.getClass()!=Byte.class) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", Byte expected");
58                 return ((Byte)obj);
59         }
60         
61         public byte getValue_(Object obj)
62         throws BindingException
63         {
64                 if (obj.getClass()!=Byte.class) throw new BindingException("Unexpected class "+obj.getClass().getSimpleName()+", Byte expected");
65                 return ((Byte)obj);
66         }
67
68         @Override
69         public void setValue(Object obj, Number value) throws BindingException {
70                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Byte");
71         }
72                 
73         public void setValue(Object obj, byte value) throws BindingException {
74                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Byte");
75         }
76         
77         @Override
78         public boolean isInstance(Object obj) {
79                 return obj instanceof Byte;
80         }
81         
82         @Override
83         public boolean isImmutable() {
84                 return true;
85         }
86 \r
87     @Override\r
88     public int compare(Object o1, Object o2)\r
89     {\r
90         Byte d1 = (Byte) o1;\r
91         Byte d2 = (Byte) o2;\r
92         int d = d1-d2;\r
93         return d == 0 ? 0 : (d<0?-1:1);\r
94     }\r
95         \r
96
97 }
98