View Javadoc
1 /*** 2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p> 3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 4 * conditions are met:<p> 5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the 8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written 9 * permission.<p> 10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p> 16 */ 17 18 package com.codestreet.messageforge; 19 20 /*** 21 * Class to represent a minimum/maximum value constraint on a <tt>double</tt> 22 * numeric message field. 23 * @author Jawaid Hakim. 24 */ 25 public class RFldConstraintF64MinMax extends RFldConstraintF64 26 { 27 /*** 28 * Factory. Returns a <tt>RFldConstraintF32MinMax</tt> instance based on the input parameters. 29 * @param key Key to use for caching constraint. 30 * @param fldName Name of field to which this constraint is bound. 31 * @param minSet Flag to indicate whether min value is set. 32 * @param min Minimum length allowed. If this is <tt>null</tt> then no 33 * constraint is placed on the minimum length. 34 * @param maxSet Flag to indicate whether max value is set. 35 * @param max Maximum length allowed. If this is <tt>null</tt> then no 36 * constraint is placed on the maximum length. 37 */ 38 public static RFldConstraintF64MinMax valueOf(String key, String fldName, boolean minSet, Double min, boolean maxSet, Double max) throws FieldValidationException 39 { 40 RFldConstraintF64MinMax instance; 41 if (! instances_.containsKey(key)) 42 { 43 instance = new RFldConstraintF64MinMax(fldName, minSet, min, maxSet, max); 44 synchronized (instances_) 45 { 46 instances_.put(key, instance); 47 } 48 } 49 else 50 instance = (RFldConstraintF64MinMax)instances_.get(key); 51 52 return instance; 53 } 54 55 /*** 56 * Factory. Returns a <tt>RFldConstraintF64MinMax</tt> instance based on the input parameters. 57 * @param key Key to use for caching constraint. 58 * @param fldName Name of field to which this constraint is bound. 59 * @param minSet Flag to indicate whether min value is set. 60 * @param min Minimum length allowed. If this is <tt>null</tt> then no 61 * constraint is placed on the minimum length. 62 * @param maxSet Flag to indicate whether max value is set. 63 * @param max Maximum length allowed. If this is <tt>null</tt> then no 64 * constraint is placed on the maximum length. 65 */ 66 public static RFldConstraintF64MinMax valueOf(String key, String fldName, boolean minSet, String min, boolean maxSet, String max) throws FieldValidationException 67 { 68 if (instances_.containsKey(key)) 69 return (RFldConstraintF64MinMax)instances_.get(key); 70 71 if (minSet && maxSet) 72 return valueOf(key, fldName, minSet, Double.valueOf(min), maxSet, Double.valueOf(max)); 73 else if (minSet) 74 return valueOf(key, fldName, minSet, Double.valueOf(min), maxSet, null); 75 else if (maxSet) 76 return valueOf(key, fldName, minSet, null, maxSet, Double.valueOf(max)); 77 else 78 throw new FieldValidationException("Either the minimum or the maximum must be set"); 79 } 80 81 /*** 82 * Constructor. 83 * @param fldName Name of field to which this constraint is bound. 84 * @param minSet Flag to indicate whether min value is set. 85 * @param minVal Minimum value allowed. 86 * @param maxSet Flag to indicate whether max value is set. 87 * @param maxVal Maximum value allowed. 88 */ 89 private RFldConstraintF64MinMax(String fldName, boolean minSet, Double minVal, boolean maxSet, Double maxVal) throws FieldValidationException 90 { 91 if (! minSet && ! maxSet) 92 throw new FieldValidationException("Min and max cannot both be NULL: " + fldName); 93 94 double minValue = (minSet) ? minVal.doubleValue() : 0.0; 95 double maxValue = (maxSet) ? maxVal.doubleValue() : 0.0; 96 97 if (minSet && maxSet && minValue > maxValue) 98 throw new FieldValidationException("Minimum value is after maximum value: " + fldName); 99 100 if (minSet) 101 setMinVal(fldName, minValue); 102 103 if (maxSet) 104 setMaxVal(fldName, maxValue); 105 } 106 107 /*** 108 * Set minimum value. 109 * @param fldName Field name. 110 * @param minVal Minimum value. 111 */ 112 private void setMinVal(String fldName, double minVal) throws FieldValidationException 113 { 114 if (maxValSet_ && minVal > maxVal_) 115 throw new FieldValidationException("Minimum value is greater than maximum value: " + fldName); 116 117 minValSet_ = true; 118 minVal_ = minVal; 119 } 120 121 /*** 122 * Set maximum value. 123 * @param fldName Field name. 124 * @param maxVal Maximum value. 125 */ 126 private void setMaxVal(String fldName, double maxVal) throws FieldValidationException 127 { 128 if (minValSet_ && maxVal < minVal_) 129 throw new FieldValidationException("Minimum value is greater than maximum value: " + fldName); 130 131 maxValSet_ = true; 132 maxVal_ = maxVal; 133 } 134 135 /*** 136 * Get error description. 137 * @param fldName Field name. 138 * @return Error description. 139 */ 140 public String errDesc(String fldName) 141 { 142 return "Field value outside min/max range: " + fldName; 143 } 144 145 /*** 146 * Check if a value satisfies the constraint. 147 * @param val Field value to validate. 148 * @return <tt>true</tt> if the field satisfied the 149 * constraint. Otherwise, <tt>false</tt> is returned. 150 */ 151 public boolean isValid(double val) 152 { 153 boolean ret = (! minValSet_ || val >= minVal_); 154 ret = (ret && (! maxValSet_ || val <= maxVal_)); 155 return ret; 156 } 157 158 /*** 159 * Cache of instances. 160 */ 161 private static java.util.Map instances_ = new java.util.HashMap(); 162 163 /*** 164 * Flag to indicate if minimum value constraint is set. 165 */ 166 protected boolean minValSet_; 167 168 /*** 169 * Flag to indicate if maximum value constraint is set. 170 */ 171 protected boolean maxValSet_; 172 173 /*** 174 * Minimum value allowed. 175 */ 176 protected double minVal_; 177 178 /*** 179 * Maximum value allowed. 180 */ 181 protected double maxVal_; 182 }

This page was automatically generated by Maven