Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/conversion/IdentityConverter.d @ 88:9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:10:09 +0200 |
parents | 6be48cf9f95c |
children |
rev | line source |
---|---|
78 | 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 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
25 private Class fromType; |
78 | 26 |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
27 private Class toType; |
78 | 28 |
29 /** | |
30 * @param type | |
31 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
32 public this(Class type) { |
78 | 33 this.fromType = type; |
34 this.toType = type; | |
85 | 35 initPrimitiveMap(); |
78 | 36 } |
37 | |
38 /** | |
39 * @param fromType | |
40 * @param toType | |
41 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
42 public this(Class fromType, Class toType) { |
78 | 43 this.fromType = fromType; |
44 this.toType = toType; | |
85 | 45 initPrimitiveMap(); |
78 | 46 } |
47 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
48 private Class[][] primitiveMap; |
85 | 49 |
50 private void initPrimitiveMap(){ | |
51 primitiveMap = [ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
52 [ cast(Class)Integer.TYPE, Class.fromType!(Integer) ], [ cast(Class)Short.TYPE, Class.fromType!(Short) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
53 [ cast(Class)Long.TYPE, Class.fromType!(Long) ], [ cast(Class)Double.TYPE, Class.fromType!(Double) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
54 [ cast(Class)Byte.TYPE, Class.fromType!(Byte) ], [ cast(Class)Float.TYPE, Class.fromType!(Float) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
55 [ cast(Class)Boolean.TYPE, Class.fromType!(Boolean) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
56 [ cast(Class)Character.TYPE, Class.fromType!(Character) ] ]; |
85 | 57 } |
78 | 58 |
59 /* | |
60 * (non-Javadoc) | |
61 * | |
62 * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object) | |
63 */ | |
64 public Object convert(Object source) { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
65 if ( toType.isPrimitive()) { |
78 | 66 if (source is null) { |
67 throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$ | |
68 } | |
69 } | |
70 if (source !is null) { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
71 Class sourceClass = Class.fromObject(source); |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
72 if (toType.isPrimitive() || sourceClass.isPrimitive()) { |
85 | 73 if (sourceClass.opEquals(toType) |
78 | 74 || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) { |
75 return source; | |
76 } | |
77 throw new BindingException( | |
78 "Boxed and unboxed types do not match"); //$NON-NLS-1$ | |
79 } | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
80 if (!toType.isAssignableFrom(sourceClass)) { |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
81 throw new BindingException(Class.fromObject(sourceClass).getName() |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
82 ~ " is not assignable to " ~ Class.fromObject(toType).getName()); //$NON-NLS-1$ |
78 | 83 } |
84 } | |
85 return source; | |
86 } | |
87 | |
88 /** | |
89 * (Non-API) isPrimitiveTypeMatchedWithBoxed. | |
90 * | |
91 * @param sourceClass | |
92 * @param toClass | |
93 * @return true if sourceClass and toType are matched primitive/boxed types | |
94 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
95 public bool isPrimitiveTypeMatchedWithBoxed(Class sourceClass, |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
96 Class toClass) { |
78 | 97 for (int i = 0; i < primitiveMap.length; i++) { |
85 | 98 if (toClass.opEquals(primitiveMap[i][0]) |
99 && sourceClass.opEquals(primitiveMap[i][1])) { | |
78 | 100 return true; |
101 } | |
85 | 102 if (sourceClass.opEquals(primitiveMap[i][0]) |
103 && toClass.opEquals(primitiveMap[i][1])) { | |
78 | 104 return true; |
105 } | |
106 } | |
107 return false; | |
108 } | |
109 | |
110 public Object getFromType() { | |
111 return fromType; | |
112 } | |
113 | |
114 public Object getToType() { | |
115 return toType; | |
116 } | |
117 | |
118 } |