KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > validation > ObjectToPrimitiveValidator


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.core.internal.databinding.validation;
13
14 import org.eclipse.core.databinding.validation.IValidator;
15 import org.eclipse.core.databinding.validation.ValidationStatus;
16 import org.eclipse.core.internal.databinding.BindingMessages;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19
20 /**
21  * @since 3.2
22  *
23  */

24 public class ObjectToPrimitiveValidator implements IValidator {
25
26     private Class JavaDoc toType;
27
28     private Class JavaDoc[][] primitiveMap = new Class JavaDoc[][] {
29             { Integer.TYPE, Integer JavaDoc.class }, { Short.TYPE, Short JavaDoc.class },
30             { Long.TYPE, Long JavaDoc.class }, { Double.TYPE, Double JavaDoc.class },
31             { Byte.TYPE, Byte JavaDoc.class }, { Float.TYPE, Float JavaDoc.class },
32             { Boolean.TYPE, Boolean JavaDoc.class }, };
33
34     /**
35      * @param toType
36      */

37     public ObjectToPrimitiveValidator(Class JavaDoc toType) {
38         this.toType = toType;
39     }
40
41     protected Class JavaDoc getToType() {
42         return this.toType;
43     }
44
45     public IStatus validate(Object JavaDoc value) {
46         return doValidate(value);
47     }
48
49     private IStatus doValidate(Object JavaDoc value) {
50         if (value != null) {
51             if (!mapContainsValues(toType, value.getClass())) {
52                 return ValidationStatus.error(getClassHint());
53             }
54             return Status.OK_STATUS;
55         }
56         return ValidationStatus.error(getNullHint());
57     }
58
59     private boolean mapContainsValues(Class JavaDoc toType, Class JavaDoc fromType) {
60         for (int i = 0; i < primitiveMap.length; i++) {
61             if ((primitiveMap[i][0].equals(toType))
62                     && (primitiveMap[i][1].equals(fromType))) {
63                 return true;
64             }
65         }
66         return false;
67     }
68
69     /**
70      * @return a hint string
71      */

72     public String JavaDoc getNullHint() {
73         return BindingMessages.getString("Validate_ConversionToPrimitive"); //$NON-NLS-1$
74
}
75
76     /**
77      * @return a hint string
78      */

79     public String JavaDoc getClassHint() {
80         return BindingMessages
81                 .getString("Validate_ConversionFromClassToPrimitive"); //$NON-NLS-1$
82
}
83 }
84
Popular Tags