1 16 package javax.faces.validator; 17 18 import javax.faces.component.StateHolder; 19 import javax.faces.component.UIComponent; 20 import javax.faces.context.FacesContext; 21 22 34 public class DoubleRangeValidator 35 implements Validator, StateHolder 36 { 37 public static final String VALIDATOR_ID = "javax.faces.DoubleRange"; 39 public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM"; 40 public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM"; 41 public static final String TYPE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.TYPE"; 42 43 private Double _minimum = null; 44 private Double _maximum = null; 45 private boolean _transient = false; 46 47 public DoubleRangeValidator() 49 { 50 } 51 52 public DoubleRangeValidator(double maximum) 53 { 54 _maximum = new Double (maximum); 55 } 56 57 public DoubleRangeValidator(double maximum, 58 double minimum) 59 { 60 _maximum = new Double (maximum); 61 _minimum = new Double (minimum); 62 } 63 64 public void validate(FacesContext facesContext, 66 UIComponent uiComponent, 67 Object value) 68 throws ValidatorException 69 { 70 if (facesContext == null) throw new NullPointerException ("facesContext"); 71 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 72 73 if (value == null) 74 { 75 return; 76 } 77 78 double dvalue = parseDoubleValue(facesContext, uiComponent,value); 79 if (_minimum != null && _maximum != null) 80 { 81 if (dvalue < _minimum.doubleValue() || 82 dvalue > _maximum.doubleValue()) 83 { 84 Object [] args = {_minimum, _maximum,uiComponent.getId()}; 85 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID, args)); 86 } 87 } 88 else if (_minimum != null) 89 { 90 if (dvalue < _minimum.doubleValue()) 91 { 92 Object [] args = {_minimum,uiComponent.getId()}; 93 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args)); 94 } 95 } 96 else if (_maximum != null) 97 { 98 if (dvalue > _maximum.doubleValue()) 99 { 100 Object [] args = {_maximum,uiComponent.getId()}; 101 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args)); 102 } 103 } 104 } 105 106 private double parseDoubleValue(FacesContext facesContext, UIComponent uiComponent, Object value) 107 throws ValidatorException 108 { 109 if (value instanceof Number ) 110 { 111 return ((Number )value).doubleValue(); 112 } 113 else 114 { 115 try 116 { 117 return Double.parseDouble(value.toString()); 118 } 119 catch (NumberFormatException e) 120 { 121 Object [] args = {uiComponent.getId()}; 122 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args)); 123 } 124 } 125 } 126 127 128 public double getMaximum() 130 { 131 return _maximum != null ? _maximum.doubleValue() : Double.MAX_VALUE; 132 } 133 134 public void setMaximum(double maximum) 135 { 136 _maximum = new Double (maximum); 137 } 138 139 public double getMinimum() 140 { 141 return _minimum != null ? _minimum.doubleValue() : Double.MIN_VALUE; 142 } 143 144 public void setMinimum(double minimum) 145 { 146 _minimum = new Double (minimum); 147 } 148 149 150 public Object saveState(FacesContext context) 152 { 153 Object values[] = new Object [2]; 154 values[0] = _maximum; 155 values[1] = _minimum; 156 return values; 157 } 158 159 public void restoreState(FacesContext context, 160 Object state) 161 { 162 Object values[] = (Object [])state; 163 _maximum = (Double )values[0]; 164 _minimum = (Double )values[1]; 165 } 166 167 public boolean isTransient() 168 { 169 return _transient; 170 } 171 172 public void setTransient(boolean transientValue) 173 { 174 _transient = transientValue; 175 } 176 177 public boolean equals(Object o) 179 { 180 if (this == o) return true; 181 if (!(o instanceof DoubleRangeValidator)) return false; 182 183 final DoubleRangeValidator doubleRangeValidator = (DoubleRangeValidator)o; 184 185 if (_maximum != null ? !_maximum.equals(doubleRangeValidator._maximum) : doubleRangeValidator._maximum != null) return false; 186 if (_minimum != null ? !_minimum.equals(doubleRangeValidator._minimum) : doubleRangeValidator._minimum != null) return false; 187 188 return true; 189 } 190 191 } 192 | Popular Tags |