]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/UnsignedByteBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / UnsignedByteBinding.java
1 package org.simantics.databoard.binding.impl;\r
2 \r
3 import org.simantics.databoard.binding.ByteBinding;\r
4 import org.simantics.databoard.binding.error.BindingException;\r
5 import org.simantics.databoard.primitives.UnsignedByte;\r
6 import org.simantics.databoard.type.ByteType;\r
7 \r
8 /**\r
9  * Binding of {@link UnsignedByte} to byte type.\r
10  * \r
11  * @author Toni Kalajainen <toni.kalajainen@iki.fi>\r
12  */\r
13 public abstract class UnsignedByteBinding extends ByteBinding {\r
14         \r
15         UnsignedByteBinding(ByteType type) {\r
16                 super(type);\r
17         }\r
18         \r
19         public static class Immutable extends UnsignedByteBinding {\r
20                 \r
21                 public Immutable(ByteType type) {\r
22                         super(type);\r
23                 }               \r
24                 \r
25                 @Override\r
26                 public Object create(Number value) throws BindingException {\r
27                         try {\r
28                                 return UnsignedByte.valueOf(value.longValue());\r
29                         } catch (java.lang.IllegalArgumentException e) {\r
30                                 throw new BindingException( e );\r
31                         }\r
32                 }\r
33 \r
34                 @Override\r
35                 public Object create(String value) throws BindingException {\r
36                         try {\r
37                                 return UnsignedByte.valueOf( Long.valueOf(value) );\r
38                         } catch (java.lang.IllegalArgumentException e) {\r
39                                 throw new BindingException( e );\r
40                         }\r
41                 }\r
42 \r
43                 @Override\r
44                 public void setValue(Object obj, Number value) throws BindingException {\r
45                         throw new BindingException("UnsignedByte is immutable class");\r
46                 }\r
47 \r
48                 @Override\r
49                 public boolean isImmutable() {\r
50                         return true;\r
51                 }\r
52 \r
53                 @Override\r
54                 public boolean isInstance(Object obj) {\r
55                         return obj instanceof UnsignedByte.Immutable;\r
56                 }\r
57 \r
58                 @Override\r
59                 public Object create(byte value) throws BindingException {\r
60                         try {\r
61                                 return UnsignedByte.valueOf(value);\r
62                         } catch (java.lang.IllegalArgumentException e) {\r
63                                 throw new BindingException( e );\r
64                         }\r
65                 }\r
66 \r
67                 @Override\r
68                 public Object create(Byte value) throws BindingException {\r
69                         try {\r
70                                 return UnsignedByte.valueOf(value);\r
71                         } catch (java.lang.IllegalArgumentException e) {\r
72                                 throw new BindingException( e );\r
73                         }\r
74                 }\r
75 \r
76                 @Override\r
77                 public void setValue(Object obj, byte value) throws BindingException {\r
78                         throw new BindingException("UnsignedByte is immutable class");\r
79                 }\r
80                                 \r
81         }\r
82         \r
83         public static class Mutable extends UnsignedByteBinding {\r
84                 \r
85                 public Mutable(ByteType type) {\r
86                         super(type);\r
87                 }               \r
88 \r
89                 @Override\r
90                 public Object create(Number value) throws BindingException {\r
91                         return new UnsignedByte.Mutable(value.longValue());\r
92                 }\r
93 \r
94                 @Override\r
95                 public Object create(String value) throws BindingException {\r
96                         return new UnsignedByte.Mutable(value);\r
97                 }\r
98 \r
99                 @Override\r
100                 public void setValue(Object obj, Number value) throws BindingException {\r
101                         UnsignedByte.Mutable bb = (UnsignedByte.Mutable) obj;\r
102                         bb.setBits(value.intValue());\r
103                 }\r
104 \r
105                 @Override\r
106                 public boolean isImmutable() {\r
107                         return false;\r
108                 }\r
109                 \r
110                 @Override\r
111                 public boolean isInstance(Object obj) {\r
112                         return obj instanceof UnsignedByte.Mutable;\r
113                 }\r
114 \r
115                 @Override\r
116                 public Object create(byte value) throws BindingException {\r
117                         return new UnsignedByte.Mutable(value);\r
118                 }\r
119 \r
120                 @Override\r
121                 public Object create(Byte value) throws BindingException {\r
122                         return new UnsignedByte.Mutable(value);\r
123                 }\r
124 \r
125                 @Override\r
126                 public void setValue(Object obj, byte value) throws BindingException {\r
127                         UnsignedByte.Mutable bb = (UnsignedByte.Mutable) obj;\r
128                         bb.setBits(value);\r
129                 }               \r
130         }       \r
131 \r
132         @Override\r
133         public Byte getValue(Object obj) throws BindingException {\r
134                 return (byte) ((UnsignedByte)obj).toBits();\r
135         }\r
136 \r
137         @Override\r
138         public byte getValue_(Object obj) throws BindingException {\r
139                 return (byte) ((UnsignedByte)obj).toBits();\r
140         }\r
141         \r
142         @Override\r
143         public String toString(Object value) throws BindingException {\r
144                 return value.toString();\r
145         }\r
146         \r
147 }\r