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