2010-05-25 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Core / System.Linq.jvm / Conversion.cs
bloba3dc754ba0a798ba0afae648d92155fe22923589
1 //
2 // Conversion.cs
3 //
4 // (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com)
5 // (C) 2008 db4objects, Inc. (http://www.db4o.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 using System;
29 namespace System.Linq.jvm {
31 class Conversion {
33 public static object ConvertPrimitiveUnChecked (Type from, Type to, object value)
35 unchecked {
36 switch (Type.GetTypeCode (from)) {
37 case TypeCode.Byte:
38 return ConvertByte ((byte) value, to);
39 case TypeCode.Char:
40 return ConvertChar ((char) value, to);
41 case TypeCode.Decimal:
42 return ConvertDecimal ((decimal) value, to);
43 case TypeCode.Double:
44 return ConvertDouble ((double) value, to);
45 case TypeCode.Int16:
46 return ConvertShort ((short) value, to);
47 case TypeCode.Int32:
48 return ConvertInt ((int) value, to);
49 case TypeCode.Int64:
50 return ConvertLong ((long) value, to);
51 case TypeCode.SByte:
52 return ConvertSByte ((sbyte) value, to);
53 case TypeCode.Single:
54 return ConvertFloat ((float) value, to);
55 case TypeCode.UInt16:
56 return ConvertUShort ((ushort) value, to);
57 case TypeCode.UInt32:
58 return ConvertUInt ((uint) value, to);
59 case TypeCode.UInt64:
60 return ConvertULong ((ulong) value, to);
61 default:
62 throw new NotImplementedException ();
67 static object ConvertByte (byte b, Type to)
69 unchecked {
70 switch (Type.GetTypeCode (to)) {
71 case TypeCode.Byte:
72 return (byte) b;
73 case TypeCode.Char:
74 return (char) b;
75 case TypeCode.Decimal:
76 return (decimal) b;
77 case TypeCode.Double:
78 return (double) b;
79 case TypeCode.Int16:
80 return (short) b;
81 case TypeCode.Int32:
82 return (int) b;
83 case TypeCode.Int64:
84 return (long) b;
85 case TypeCode.SByte:
86 return (sbyte) b;
87 case TypeCode.Single:
88 return (float) b;
89 case TypeCode.UInt16:
90 return (ushort) b;
91 case TypeCode.UInt32:
92 return (uint) b;
93 case TypeCode.UInt64:
94 return (ulong) b;
96 return null;
100 static object ConvertChar (char b, Type to)
102 unchecked {
103 switch (Type.GetTypeCode (to)) {
104 case TypeCode.Byte:
105 return (byte) b;
106 case TypeCode.Char:
107 return (char) b;
108 case TypeCode.Decimal:
109 return (decimal) b;
110 case TypeCode.Double:
111 return (double) b;
112 case TypeCode.Int16:
113 return (short) b;
114 case TypeCode.Int32:
115 return (int) b;
116 case TypeCode.Int64:
117 return (long) b;
118 case TypeCode.SByte:
119 return (sbyte) b;
120 case TypeCode.Single:
121 return (float) b;
122 case TypeCode.UInt16:
123 return (ushort) b;
124 case TypeCode.UInt32:
125 return (uint) b;
126 case TypeCode.UInt64:
127 return (ulong) b;
129 return null;
133 static object ConvertDecimal (decimal b, Type to)
135 unchecked {
136 switch (Type.GetTypeCode (to)) {
137 case TypeCode.Byte:
138 return (byte) b;
139 case TypeCode.Char:
140 return (char) (short) b;
141 case TypeCode.Decimal:
142 return (decimal) b;
143 case TypeCode.Double:
144 return (double) b;
145 case TypeCode.Int16:
146 return (short) b;
147 case TypeCode.Int32:
148 return (int) b;
149 case TypeCode.Int64:
150 return (long) b;
151 case TypeCode.SByte:
152 return (sbyte) b;
153 case TypeCode.Single:
154 return (float) b;
155 case TypeCode.UInt16:
156 return (ushort) b;
157 case TypeCode.UInt32:
158 return (uint) b;
159 case TypeCode.UInt64:
160 return (ulong) b;
162 return null;
166 static object ConvertDouble (double b, Type to)
168 unchecked {
169 switch (Type.GetTypeCode (to)) {
170 case TypeCode.Byte:
171 return (byte) b;
172 case TypeCode.Char:
173 return (char) b;
174 case TypeCode.Decimal:
175 return (decimal) b;
176 case TypeCode.Double:
177 return (double) b;
178 case TypeCode.Int16:
179 return (short) b;
180 case TypeCode.Int32:
181 return (int) b;
182 case TypeCode.Int64:
183 return (long) b;
184 case TypeCode.SByte:
185 return (sbyte) b;
186 case TypeCode.Single:
187 return (float) b;
188 case TypeCode.UInt16:
189 return (ushort) b;
190 case TypeCode.UInt32:
191 return (uint) b;
192 case TypeCode.UInt64:
193 return (ulong) b;
195 return null;
199 static object ConvertShort (short b, Type to)
201 unchecked {
202 switch (Type.GetTypeCode (to)) {
203 case TypeCode.Byte:
204 return (byte) b;
205 case TypeCode.Char:
206 return (char) b;
207 case TypeCode.Decimal:
208 return (decimal) b;
209 case TypeCode.Double:
210 return (double) b;
211 case TypeCode.Int16:
212 return (short) b;
213 case TypeCode.Int32:
214 return (int) b;
215 case TypeCode.Int64:
216 return (long) b;
217 case TypeCode.SByte:
218 return (sbyte) b;
219 case TypeCode.Single:
220 return (float) b;
221 case TypeCode.UInt16:
222 return (ushort) b;
223 case TypeCode.UInt32:
224 return (uint) b;
225 case TypeCode.UInt64:
226 return (ulong) b;
228 return null;
232 static object ConvertInt (int b, Type to)
234 unchecked {
235 switch (Type.GetTypeCode (to)) {
236 case TypeCode.Byte:
237 return (byte) b;
238 case TypeCode.Char:
239 return (char) b;
240 case TypeCode.Decimal:
241 return (decimal) b;
242 case TypeCode.Double:
243 return (double) b;
244 case TypeCode.Int16:
245 return (short) b;
246 case TypeCode.Int32:
247 return (int) b;
248 case TypeCode.Int64:
249 return (long) b;
250 case TypeCode.SByte:
251 return (sbyte) b;
252 case TypeCode.Single:
253 return (float) b;
254 case TypeCode.UInt16:
255 return (ushort) b;
256 case TypeCode.UInt32:
257 return (uint) b;
258 case TypeCode.UInt64:
259 return (ulong) b;
261 return null;
265 static object ConvertLong (long b, Type to)
267 unchecked {
268 switch (Type.GetTypeCode (to)) {
269 case TypeCode.Byte:
270 return (byte) b;
271 case TypeCode.Char:
272 return (char) b;
273 case TypeCode.Decimal:
274 return (decimal) b;
275 case TypeCode.Double:
276 return (double) b;
277 case TypeCode.Int16:
278 return (short) b;
279 case TypeCode.Int32:
280 return (int) b;
281 case TypeCode.Int64:
282 return (long) b;
283 case TypeCode.SByte:
284 return (sbyte) b;
285 case TypeCode.Single:
286 return (float) b;
287 case TypeCode.UInt16:
288 return (ushort) b;
289 case TypeCode.UInt32:
290 return (uint) b;
291 case TypeCode.UInt64:
292 return (ulong) b;
294 return null;
298 static object ConvertSByte (sbyte b, Type to)
300 unchecked {
301 switch (Type.GetTypeCode (to)) {
302 case TypeCode.Byte:
303 return (byte) b;
304 case TypeCode.Char:
305 return (char) b;
306 case TypeCode.Decimal:
307 return (decimal) b;
308 case TypeCode.Double:
309 return (double) b;
310 case TypeCode.Int16:
311 return (short) b;
312 case TypeCode.Int32:
313 return (int) b;
314 case TypeCode.Int64:
315 return (long) b;
316 case TypeCode.SByte:
317 return (sbyte) b;
318 case TypeCode.Single:
319 return (float) b;
320 case TypeCode.UInt16:
321 return (ushort) b;
322 case TypeCode.UInt32:
323 return (uint) b;
324 case TypeCode.UInt64:
325 return (ulong) b;
327 return null;
331 static object ConvertFloat (float b, Type to)
333 unchecked {
334 switch (Type.GetTypeCode (to)) {
335 case TypeCode.Byte:
336 return (byte) b;
337 case TypeCode.Char:
338 return (char) b;
339 case TypeCode.Decimal:
340 return (decimal) b;
341 case TypeCode.Double:
342 return (double) b;
343 case TypeCode.Int16:
344 return (short) b;
345 case TypeCode.Int32:
346 return (int) b;
347 case TypeCode.Int64:
348 return (long) b;
349 case TypeCode.SByte:
350 return (sbyte) b;
351 case TypeCode.Single:
352 return (float) b;
353 case TypeCode.UInt16:
354 return (ushort) b;
355 case TypeCode.UInt32:
356 return (uint) b;
357 case TypeCode.UInt64:
358 return (ulong) b;
360 return null;
364 static object ConvertUShort (ushort b, Type to)
366 unchecked {
367 switch (Type.GetTypeCode (to)) {
368 case TypeCode.Byte:
369 return (byte) b;
370 case TypeCode.Char:
371 return (char) b;
372 case TypeCode.Decimal:
373 return (decimal) b;
374 case TypeCode.Double:
375 return (double) b;
376 case TypeCode.Int16:
377 return (short) b;
378 case TypeCode.Int32:
379 return (int) b;
380 case TypeCode.Int64:
381 return (long) b;
382 case TypeCode.SByte:
383 return (sbyte) b;
384 case TypeCode.Single:
385 return (float) b;
386 case TypeCode.UInt16:
387 return (ushort) b;
388 case TypeCode.UInt32:
389 return (uint) b;
390 case TypeCode.UInt64:
391 return (ulong) b;
393 return null;
397 static object ConvertUInt (uint b, Type to)
399 unchecked {
400 switch (Type.GetTypeCode (to)) {
401 case TypeCode.Byte:
402 return (byte) b;
403 case TypeCode.Char:
404 return (char) b;
405 case TypeCode.Decimal:
406 return (decimal) b;
407 case TypeCode.Double:
408 return (double) b;
409 case TypeCode.Int16:
410 return (short) b;
411 case TypeCode.Int32:
412 return (int) b;
413 case TypeCode.Int64:
414 return (long) b;
415 case TypeCode.SByte:
416 return (sbyte) b;
417 case TypeCode.Single:
418 return (float) b;
419 case TypeCode.UInt16:
420 return (ushort) b;
421 case TypeCode.UInt32:
422 return (uint) b;
423 case TypeCode.UInt64:
424 return (ulong) b;
426 return null;
430 static object ConvertULong (ulong b, Type to)
432 unchecked {
433 switch (Type.GetTypeCode (to)) {
434 case TypeCode.Byte:
435 return (byte) b;
436 case TypeCode.Char:
437 return (char) b;
438 case TypeCode.Decimal:
439 return (decimal) b;
440 case TypeCode.Double:
441 return (double) b;
442 case TypeCode.Int16:
443 return (short) b;
444 case TypeCode.Int32:
445 return (int) b;
446 case TypeCode.Int64:
447 return (long) b;
448 case TypeCode.SByte:
449 return (sbyte) b;
450 case TypeCode.Single:
451 return (float) b;
452 case TypeCode.UInt16:
453 return (ushort) b;
454 case TypeCode.UInt32:
455 return (uint) b;
456 case TypeCode.UInt64:
457 return (ulong) b;
459 return null;