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.conversion.NumberToNumberConverter; 17 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 21 29 public abstract class NumberToNumberValidator implements IValidator { 30 private final NumberToNumberConverter converter; 31 32 private final Number min; 33 34 private final Number max; 35 36 private String outOfRangeMessage; 37 38 private final boolean primitive; 39 40 47 protected NumberToNumberValidator(NumberToNumberConverter converter, 48 Number min, Number max) { 49 this.converter = converter; 50 this.min = min; 51 this.max = max; 52 53 primitive = ((Class ) converter.getToType()).isPrimitive(); 54 } 55 56 61 public final IStatus validate(Object value) { 62 if (value == null) { 63 if (primitive) { 64 throw new IllegalArgumentException ( 65 "Parameter 'value' cannot be null."); } 67 68 return Status.OK_STATUS; 69 } 70 71 if (!(value instanceof Number )) { 72 throw new IllegalArgumentException ( 73 "Parameter 'value' is not of type Number."); } 75 76 Number number = (Number ) value; 77 if (inRange(number)) { 78 return Status.OK_STATUS; 79 } 80 81 synchronized (this) { 82 if (outOfRangeMessage == null && min != null && max != null) { 83 outOfRangeMessage = StringToNumberParser 84 .createOutOfRangeMessage(min, max, converter 85 .getNumberFormat()); 86 } 87 88 return ValidationStatus.error(outOfRangeMessage); 89 } 90 } 91 92 98 protected abstract boolean inRange(Number number); 99 } 100 | Popular Tags |