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 LongRangeValidator 35 implements Validator, StateHolder 36 { 37 public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MAXIMUM"; 39 public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MINIMUM"; 40 public static final String TYPE_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.TYPE"; 41 public static final String VALIDATOR_ID = "javax.faces.LongRange"; 42 43 private Long _minimum = null; 44 private Long _maximum = null; 45 private boolean _transient = false; 46 47 public LongRangeValidator() 49 { 50 } 51 52 public LongRangeValidator(long maximum) 53 { 54 _maximum = new Long (maximum); 55 } 56 57 public LongRangeValidator(long maximum, 58 long minimum) 59 { 60 _maximum = new Long (maximum); 61 _minimum = new Long (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 = parseLongValue(facesContext, uiComponent,value); 79 if (_minimum != null && _maximum != null) 80 { 81 if (dvalue < _minimum.longValue() || 82 dvalue > _maximum.longValue()) 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.longValue()) 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.longValue()) 99 { 100 Object [] args = {_maximum,uiComponent.getId()}; 101 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args)); 102 } 103 } 104 } 105 106 private long parseLongValue(FacesContext facesContext, UIComponent uiComponent, Object value) 107 throws ValidatorException 108 { 109 if (value instanceof Number ) 110 { 111 return ((Number )value).longValue(); 112 } 113 else 114 { 115 try 116 { 117 return Long.parseLong(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 long getMaximum() 130 { 131 return _maximum != null ? _maximum.longValue() : Long.MAX_VALUE; 132 } 133 134 public void setMaximum(long maximum) 135 { 136 _maximum = new Long (maximum); 137 } 138 139 public long getMinimum() 140 { 141 return _minimum != null ? _minimum.longValue() : Long.MIN_VALUE; 142 } 143 144 public void setMinimum(long minimum) 145 { 146 _minimum = new Long (minimum); 147 } 148 149 public boolean isTransient() 150 { 151 return _transient; 152 } 153 154 public void setTransient(boolean transientValue) 155 { 156 _transient = transientValue; 157 } 158 159 public Object saveState(FacesContext context) 161 { 162 Object values[] = new Object [2]; 163 values[0] = _maximum; 164 values[1] = _minimum; 165 return values; 166 } 167 168 public void restoreState(FacesContext context, 169 Object state) 170 { 171 Object values[] = (Object [])state; 172 _maximum = (Long )values[0]; 173 _minimum = (Long )values[1]; 174 } 175 176 public boolean equals(Object o) 178 { 179 if (this == o) return true; 180 if (!(o instanceof LongRangeValidator)) return false; 181 182 final LongRangeValidator longRangeValidator = (LongRangeValidator)o; 183 184 if (_maximum != null ? !_maximum.equals(longRangeValidator._maximum) : longRangeValidator._maximum != null) return false; 185 if (_minimum != null ? !_minimum.equals(longRangeValidator._minimum) : longRangeValidator._minimum != null) return false; 186 187 return true; 188 } 189 190 } 191 | Popular Tags |