1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2005, 2007 db4objects Inc.  http://www.db4o.com  and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     db4objects - Initial API and implementation
 *     Matt Carter - Character support completed (bug 197679)
 */
package org.eclipse.core.internal.databinding.conversion;

import org.eclipse.core.databinding.BindingException;
import org.eclipse.core.databinding.conversion.IConverter;

/**
 * TheIdentityConverter. Returns the source value (the identity function).
 */
public class IdentityConverter implements IConverter {

    private Class fromType;

    private Class toType;

    /**
     * @param type
     */
    public IdentityConverter(Class type) {
        this.fromType = type;
        this.toType = type;
    }

    /**
     * @param fromType
     * @param toType
     */
    public IdentityConverter(Class fromType, Class toType) {
        this.fromType = fromType;
        this.toType = toType;
    }

    private Class[][] primitiveMap = new Class[][] {
            { Integer.TYPE, Integer.class }, { Short.TYPE, Short.class },
            { Long.TYPE, Long.class }, { Double.TYPE, Double.class },
            { Byte.TYPE, Byte.class }, { Float.TYPE, Float.class },
            { Boolean.TYPE, Boolean.class },
            { Character.TYPE, Character.class } };

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.binding.converter.IConverter#convert(java.lang.Object)
     */
    public Object convert(Object source) {
        if (toType.isPrimitive()) {
            if (source == null) {
                throw new BindingException("Cannot convert null to a primitive"); //$NON-NLS-1$
            }
        }
        if (source != null) {
            Class sourceClass = source.getClass();
            if (toType.isPrimitive() || sourceClass.isPrimitive()) {
                if (sourceClass.equals(toType)
                        || isPrimitiveTypeMatchedWithBoxed(sourceClass, toType)) {
                    return source;
                }
                throw new BindingException(
                        "Boxed and unboxed types do not match"); //$NON-NLS-1$
            }
            if (!toType.isAssignableFrom(sourceClass)) {
                throw new BindingException(sourceClass.getName()
                        + " is not assignable to " + toType.getName()); //$NON-NLS-1$
            }
        }
        return source;
    }

    /**
     * (Non-API) isPrimitiveTypeMatchedWithBoxed.
     * 
     * @param sourceClass
     * @param toClass
     * @return true if sourceClass and toType are matched primitive/boxed types
     */
    public boolean isPrimitiveTypeMatchedWithBoxed(Class sourceClass,
            Class toClass) {
        for (int i = 0; i < primitiveMap.length; i++) {
            if (toClass.equals(primitiveMap[i][0])
                    && sourceClass.equals(primitiveMap[i][1])) {
                return true;
            }
            if (sourceClass.equals(primitiveMap[i][0])
                    && toClass.equals(primitiveMap[i][1])) {
                return true;
            }
        }
        return false;
    }

    public Object getFromType() {
        return fromType;
    }

    public Object getToType() {
        return toType;
    }

}
			
			

Browsed Source: [clear]