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;
|
85
|
16 import java.nonstandard.RuntimeTraits;
|
78
|
17
|
|
18 import org.eclipse.core.databinding.BindingException;
|
|
19 import org.eclipse.core.databinding.conversion.IConverter;
|
|
20
|
|
21 /**
|
|
22 * TheIdentityConverter. Returns the source value (the identity function).
|
|
23 */
|
|
24 public class IdentityConverter : IConverter {
|
|
25
|
85
|
26 private TypeInfo fromType;
|
78
|
27
|
85
|
28 private TypeInfo toType;
|
78
|
29
|
|
30 /**
|
|
31 * @param type
|
|
32 */
|
85
|
33 public this(TypeInfo type) {
|
78
|
34 this.fromType = type;
|
|
35 this.toType = type;
|
85
|
36 initPrimitiveMap();
|
78
|
37 }
|
|
38
|
|
39 /**
|
|
40 * @param fromType
|
|
41 * @param toType
|
|
42 */
|
85
|
43 public this(TypeInfo fromType, TypeInfo toType) {
|
78
|
44 this.fromType = fromType;
|
|
45 this.toType = toType;
|
85
|
46 initPrimitiveMap();
|
78
|
47 }
|
|
48
|
85
|
49 private TypeInfo[][] primitiveMap;
|
|
50
|
|
51 private void initPrimitiveMap(){
|
|
52 primitiveMap = [
|
|
53 [ cast(TypeInfo)Integer.TYPE, typeid(Integer) ], [ cast(TypeInfo)Short.TYPE, typeid(Short) ],
|
|
54 [ cast(TypeInfo)Long.TYPE, typeid(Long) ], [ cast(TypeInfo)Double.TYPE, typeid(Double) ],
|
|
55 [ cast(TypeInfo)Byte.TYPE, typeid(Byte) ], [ cast(TypeInfo)Float.TYPE, typeid(Float) ],
|
|
56 [ cast(TypeInfo)Boolean.TYPE, typeid(Boolean) ],
|
|
57 [ cast(TypeInfo)Character.TYPE, typeid(Character) ] ];
|
|
58 }
|
78
|
59
|
|
60 /*
|
|
61 * (non-Javadoc)
|
|
62 *
|
|
63 * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object)
|
|
64 */
|
|
65 public Object convert(Object source) {
|
85
|
66 if ( isJavaPrimitive(toType)) {
|
78
|
67 if (source is null) {
|
|
68 throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$
|
|
69 }
|
|
70 }
|
|
71 if (source !is null) {
|
85
|
72 TypeInfo sourceClass = getTypeInfo(source.classinfo);
|
|
73 if (isJavaPrimitive(toType) || isJavaPrimitive(sourceClass)) {
|
|
74 if (sourceClass.opEquals(toType)
|
78
|
75 || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) {
|
|
76 return source;
|
|
77 }
|
|
78 throw new BindingException(
|
|
79 "Boxed and unboxed types do not match"); //$NON-NLS-1$
|
|
80 }
|
85
|
81 if (!isImplicitly(sourceClass, toType)) {
|
|
82 throw new BindingException(asClass(sourceClass).name
|
|
83 ~ " is not assignable to " ~ asClass(toType).name); //$NON-NLS-1$
|
78
|
84 }
|
|
85 }
|
|
86 return source;
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * (Non-API) isPrimitiveTypeMatchedWithBoxed.
|
|
91 *
|
|
92 * @param sourceClass
|
|
93 * @param toClass
|
|
94 * @return true if sourceClass and toType are matched primitive/boxed types
|
|
95 */
|
85
|
96 public bool isPrimitiveTypeMatchedWithBoxed(TypeInfo sourceClass,
|
|
97 TypeInfo toClass) {
|
78
|
98 for (int i = 0; i < primitiveMap.length; i++) {
|
85
|
99 if (toClass.opEquals(primitiveMap[i][0])
|
|
100 && sourceClass.opEquals(primitiveMap[i][1])) {
|
78
|
101 return true;
|
|
102 }
|
85
|
103 if (sourceClass.opEquals(primitiveMap[i][0])
|
|
104 && toClass.opEquals(primitiveMap[i][1])) {
|
78
|
105 return true;
|
|
106 }
|
|
107 }
|
|
108 return false;
|
|
109 }
|
|
110
|
|
111 public Object getFromType() {
|
|
112 return fromType;
|
|
113 }
|
|
114
|
|
115 public Object getToType() {
|
|
116 return toType;
|
|
117 }
|
|
118
|
|
119 }
|