KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > conversion > IdentityConverter


1 /*
2  * Copyright (C) 2005 db4objects Inc. http://www.db4o.com
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  */

12 package org.eclipse.core.internal.databinding.conversion;
13
14 import org.eclipse.core.databinding.BindingException;
15 import org.eclipse.core.databinding.conversion.IConverter;
16
17 /**
18  * TheIdentityConverter. Returns the source value (the identity function).
19  */

20 public class IdentityConverter implements IConverter {
21
22     private Class JavaDoc fromType;
23
24     private Class JavaDoc toType;
25
26     /**
27      * @param type
28      */

29     public IdentityConverter(Class JavaDoc type) {
30         this.fromType = type;
31         this.toType = type;
32     }
33
34     /**
35      * @param fromType
36      * @param toType
37      */

38     public IdentityConverter(Class JavaDoc fromType, Class JavaDoc toType) {
39         this.fromType = fromType;
40         this.toType = toType;
41     }
42
43     private Class JavaDoc[][] primitiveMap = new Class JavaDoc[][] {
44             { Integer.TYPE, Integer JavaDoc.class }, { Short.TYPE, Short JavaDoc.class },
45             { Long.TYPE, Long JavaDoc.class }, { Double.TYPE, Double JavaDoc.class },
46             { Byte.TYPE, Byte JavaDoc.class }, { Float.TYPE, Float JavaDoc.class },
47             { Boolean.TYPE, Boolean JavaDoc.class }, };
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object)
53      */

54     public Object JavaDoc convert(Object JavaDoc source) {
55         if (toType.isPrimitive()) {
56             if (source == null) {
57                 throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$
58
}
59         }
60         if (source != null) {
61             Class JavaDoc sourceClass = source.getClass();
62             if (toType.isPrimitive() || sourceClass.isPrimitive()) {
63                 if (sourceClass.equals(toType)
64                         || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) {
65                     return source;
66                 }
67                 throw new BindingException(
68                         "Boxed and unboxed types do not match"); //$NON-NLS-1$
69
}
70             if (!toType.isAssignableFrom(sourceClass)) {
71                 throw new BindingException(sourceClass.getName()
72                         + " is not assignable to " + toType.getName()); //$NON-NLS-1$
73
}
74         }
75         return source;
76     }
77
78     /**
79      * (Non-API) isPrimitiveTypeMatchedWithBoxed.
80      *
81      * @param sourceClass
82      * @param toClass
83      * @return true if sourceClass and toType are matched primitive/boxed types
84      */

85     public boolean isPrimitiveTypeMatchedWithBoxed(Class JavaDoc sourceClass,
86             Class JavaDoc toClass) {
87         for (int i = 0; i < primitiveMap.length; i++) {
88             if (toClass.equals(primitiveMap[i][0])
89                     && sourceClass.equals(primitiveMap[i][1])) {
90                 return true;
91             }
92             if (sourceClass.equals(primitiveMap[i][0])
93                     && toClass.equals(primitiveMap[i][1])) {
94                 return true;
95             }
96         }
97         return false;
98     }
99
100     public Object JavaDoc getFromType() {
101         return fromType;
102     }
103
104     public Object JavaDoc getToType() {
105         return toType;
106     }
107
108 }
109
Popular Tags