1 23 24 28 29 package com.sun.enterprise.tools.common.validation.constraints; 30 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.text.MessageFormat ; 34 35 import com.sun.enterprise.tools.common.validation.constraints.ConstraintFailure; 36 import com.sun.enterprise.tools.common.validation.util.BundleReader; 37 38 53 public class RangeConstraint extends ConstraintUtils 54 implements Constraint { 55 56 60 private Double startValue = null; 61 62 66 private Double endValue = null; 67 68 69 70 public RangeConstraint() { 71 startValue = null; 72 endValue = null; 73 } 74 75 76 77 public RangeConstraint(String startValue, String endValue) { 78 try { 79 this.startValue = new Double (startValue); 80 this.endValue = new Double (endValue); 81 } catch(NumberFormatException e) { 82 String format = 83 BundleReader.getValue("Error_failed_to_create"); Object [] arguments = 85 new Object []{"RangeConstaint"}; 87 System.out.println(MessageFormat.format(format, arguments)); 88 } 89 } 90 91 92 106 public Collection match(String value, String name) { 107 ArrayList failed_constrained_list = new ArrayList (); 108 109 if((startValue == null) || (endValue == null)) { 110 return failed_constrained_list; 111 } 112 113 if((value != null) && (value.length() != 0)){ 114 try { 115 Double val = new Double (value); 116 if((val.compareTo(startValue) < 0) || 117 (val.compareTo(endValue) > 0)){ 118 addFailure(failed_constrained_list, name, value); 119 } 120 } catch(NumberFormatException e) { 121 addFailure(failed_constrained_list, name, value); } 122 } 123 return failed_constrained_list; 124 } 125 126 127 133 public void setRangeStart(String value){ 134 try { 135 startValue = new Double (value); 136 } catch(NumberFormatException e) { 137 String format = 138 BundleReader.getValue("Error_failed_to_set"); Object [] arguments = 140 new Object []{this.toString(), "Range Start"}; 142 System.out.println(MessageFormat.format(format, arguments)); 143 } 144 } 145 146 147 153 public void setRangeEnd(String value){ 154 try { 155 endValue = new Double (value); 156 } catch(NumberFormatException e) { 157 String format = 158 BundleReader.getValue("Error_failed_to_set"); Object [] arguments = 160 new Object []{this.toString(), "Range End"}; 162 System.out.println(MessageFormat.format(format, arguments)); 163 } 164 } 165 166 167 170 public void print() { 171 super.print(); 172 173 String format = BundleReader.getValue("Name_Value_Pair_Format"); Object [] arguments = 175 new Object []{"Range Start", startValue}; System.out.println(MessageFormat.format(format, arguments)); 177 178 arguments = new Object []{"Range End", endValue}; System.out.println(MessageFormat.format(format, arguments)); 180 } 181 182 183 189 public void setRangeStart(Double value){ 190 startValue = value; 191 } 192 193 194 200 public void setRangeEnd(Double value){ 201 endValue = value; 202 } 203 204 205 private void addFailure(Collection failed_constrained_list, 206 String name, String value){ 207 String failureMessage = formatFailureMessage(toString(), value, name); 208 209 String format = BundleReader.getValue( 210 "MSG_RangeConstraint_Failure"); String range = startValue + " - " + endValue; 212 Object [] arguments = new Object []{range}; 213 String genericFailureMessage = 214 MessageFormat.format(format, arguments); 215 216 failed_constrained_list.add(new ConstraintFailure(toString(), 217 value, name, failureMessage, genericFailureMessage)); 218 } 219 } 220 | Popular Tags |