Mercurial > projects > dwt2
comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IdentityConverter.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /* | |
2 * Copyright cast(C) 2005 db4objects Inc. http://www.db4o.com and others. | |
3 * | |
4 * All rights reserved. This program and the accompanying materials | |
5 * are made available under the terms of the Eclipse Public License v1.0 | |
6 * which accompanies this distribution, and is available at | |
7 * http://www.eclipse.org/legal/epl-v10.html | |
8 * | |
9 * Contributors: | |
10 * db4objects - Initial API and implementation | |
11 * Matt Carter - Character support completed (bug 197679) | |
12 */ | |
13 module org.eclipse.core.internal.databinding.conversion.IdentityConverter; | |
14 | |
15 import java.lang.all; | |
16 | |
17 import org.eclipse.core.databinding.BindingException; | |
18 import org.eclipse.core.databinding.conversion.IConverter; | |
19 | |
20 /** | |
21 * TheIdentityConverter. Returns the source value (the identity function). | |
22 */ | |
23 public class IdentityConverter : IConverter { | |
24 | |
25 private ClassInfo fromType; | |
26 | |
27 private ClassInfo toType; | |
28 | |
29 /** | |
30 * @param type | |
31 */ | |
32 public this(ClassInfo type) { | |
33 this.fromType = type; | |
34 this.toType = type; | |
35 } | |
36 | |
37 /** | |
38 * @param fromType | |
39 * @param toType | |
40 */ | |
41 public this(ClassInfo fromType, ClassInfo toType) { | |
42 this.fromType = fromType; | |
43 this.toType = toType; | |
44 } | |
45 | |
46 private ClassInfo[][] primitiveMap = new ClassInfo[][] [ | |
47 [ Integer.TYPE, Integer.classinfo ], [ Short.TYPE, Short.classinfo ], | |
48 [ Long.TYPE, Long.classinfo ], [ Double.TYPE, Double.classinfo ], | |
49 [ Byte.TYPE, Byte.classinfo ], [ Float.TYPE, Float.classinfo ], | |
50 [ Boolean.TYPE, Boolean.classinfo ], | |
51 [ Character.TYPE, Character.classinfo ] ]; | |
52 | |
53 /* | |
54 * (non-Javadoc) | |
55 * | |
56 * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) | |
57 */ | |
58 public Object convert(Object source) { | |
59 if (toType.isPrimitive()) { | |
60 if (source is null) { | |
61 throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$ | |
62 } | |
63 } | |
64 if (source !is null) { | |
65 ClassInfo sourceClass = source.getClass(); | |
66 if (toType.isPrimitive() || sourceClass.isPrimitive()) { | |
67 if (sourceClass.equals(toType) | |
68 || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) { | |
69 return source; | |
70 } | |
71 throw new BindingException( | |
72 "Boxed and unboxed types do not match"); //$NON-NLS-1$ | |
73 } | |
74 if (!toType.isAssignableFrom(sourceClass)) { | |
75 throw new BindingException(sourceClass.getName() | |
76 + " is not assignable to " + toType.getName()); //$NON-NLS-1$ | |
77 } | |
78 } | |
79 return source; | |
80 } | |
81 | |
82 /** | |
83 * (Non-API) isPrimitiveTypeMatchedWithBoxed. | |
84 * | |
85 * @param sourceClass | |
86 * @param toClass | |
87 * @return true if sourceClass and toType are matched primitive/boxed types | |
88 */ | |
89 public bool isPrimitiveTypeMatchedWithBoxed(ClassInfo sourceClass, | |
90 ClassInfo toClass) { | |
91 for (int i = 0; i < primitiveMap.length; i++) { | |
92 if (toClass.equals(primitiveMap[i][0]) | |
93 && sourceClass.equals(primitiveMap[i][1])) { | |
94 return true; | |
95 } | |
96 if (sourceClass.equals(primitiveMap[i][0]) | |
97 && toClass.equals(primitiveMap[i][1])) { | |
98 return true; | |
99 } | |
100 } | |
101 return false; | |
102 } | |
103 | |
104 public Object getFromType() { | |
105 return fromType; | |
106 } | |
107 | |
108 public Object getToType() { | |
109 return toType; | |
110 } | |
111 | |
112 } |