1 19 20 package org.netbeans.modules.j2ee.sun.validation.constraints; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.text.MessageFormat ; 25 26 import org.netbeans.modules.j2ee.sun.validation.constraints.ConstraintFailure; 27 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader; 28 29 44 public class RangeConstraint extends ConstraintUtils 45 implements Constraint { 46 47 51 private Double startValue = null; 52 53 57 private Double endValue = null; 58 59 60 61 public RangeConstraint() { 62 startValue = null; 63 endValue = null; 64 } 65 66 67 68 public RangeConstraint(String startValue, String endValue) { 69 try { 70 this.startValue = new Double (startValue); 71 this.endValue = new Double (endValue); 72 } catch(NumberFormatException e) { 73 String format = 74 BundleReader.getValue("Error_failed_to_create"); Object [] arguments = 76 new Object []{"RangeConstaint"}; 78 System.out.println(MessageFormat.format(format, arguments)); 79 } 80 } 81 82 83 97 public Collection match(String value, String name) { 98 ArrayList failed_constrained_list = new ArrayList (); 99 100 if((startValue == null) || (endValue == null)) { 101 return failed_constrained_list; 102 } 103 104 if((value != null) && (value.length() != 0)){ 105 try { 106 Double val = new Double (value); 107 if((val.compareTo(startValue) < 0) || 108 (val.compareTo(endValue) > 0)){ 109 addFailure(failed_constrained_list, name, value); 110 } 111 } catch(NumberFormatException e) { 112 addFailure(failed_constrained_list, name, value); } 113 } 114 return failed_constrained_list; 115 } 116 117 118 124 public void setRangeStart(String value){ 125 try { 126 startValue = new Double (value); 127 } catch(NumberFormatException e) { 128 String format = 129 BundleReader.getValue("Error_failed_to_set"); Object [] arguments = 131 new Object []{this.toString(), "Range Start"}; 133 System.out.println(MessageFormat.format(format, arguments)); 134 } 135 } 136 137 138 144 public void setRangeEnd(String value){ 145 try { 146 endValue = new Double (value); 147 } catch(NumberFormatException e) { 148 String format = 149 BundleReader.getValue("Error_failed_to_set"); Object [] arguments = 151 new Object []{this.toString(), "Range End"}; 153 System.out.println(MessageFormat.format(format, arguments)); 154 } 155 } 156 157 158 161 public void print() { 162 super.print(); 163 164 String format = BundleReader.getValue("Name_Value_Pair_Format"); Object [] arguments = 166 new Object []{"Range Start", startValue}; System.out.println(MessageFormat.format(format, arguments)); 168 169 arguments = new Object []{"Range End", endValue}; System.out.println(MessageFormat.format(format, arguments)); 171 } 172 173 174 180 public void setRangeStart(Double value){ 181 startValue = value; 182 } 183 184 185 191 public void setRangeEnd(Double value){ 192 endValue = value; 193 } 194 195 196 private void addFailure(Collection failed_constrained_list, 197 String name, String value){ 198 String failureMessage = formatFailureMessage(toString(), value, name); 199 200 String format = BundleReader.getValue( 201 "MSG_RangeConstraint_Failure"); String range = startValue + " - " + endValue; 203 Object [] arguments = new Object []{range}; 204 String genericFailureMessage = 205 MessageFormat.format(format, arguments); 206 207 failed_constrained_list.add(new ConstraintFailure(toString(), 208 value, name, failureMessage, genericFailureMessage)); 209 } 210 } 211 | Popular Tags |