1 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 24 public class ObjectToPrimitiveValidator implements IValidator { 25 26 private Class toType; 27 28 private Class [][] primitiveMap = new Class [][] { 29 { Integer.TYPE, Integer .class }, { Short.TYPE, Short .class }, 30 { Long.TYPE, Long .class }, { Double.TYPE, Double .class }, 31 { Byte.TYPE, Byte .class }, { Float.TYPE, Float .class }, 32 { Boolean.TYPE, Boolean .class }, }; 33 34 37 public ObjectToPrimitiveValidator(Class toType) { 38 this.toType = toType; 39 } 40 41 protected Class getToType() { 42 return this.toType; 43 } 44 45 public IStatus validate(Object value) { 46 return doValidate(value); 47 } 48 49 private IStatus doValidate(Object 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 toType, Class 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 72 public String getNullHint() { 73 return BindingMessages.getString("Validate_ConversionToPrimitive"); } 75 76 79 public String getClassHint() { 80 return BindingMessages 81 .getString("Validate_ConversionFromClassToPrimitive"); } 83 } 84 | Popular Tags |