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