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>float</tt> numeric message field.
22 * @author Jawaid Hakim.
23 */
24 public class RFldConstraintF32MinMax extends RFldConstraintF32
25 {
26 /***
27 * Factory. Returns a <tt>RFldConstraintF32MinMax</tt> instance based on the input parameters.
28 * @param key Key to use for caching constraint.
29 * @param fldName Name of field to which this constraint is bound.
30 * @param minSet Flag to indicate whether min value is set.
31 * @param min Minimum length allowed. If this is <tt>null</tt> then no
32 * constraint is placed on the minimum length.
33 * @param maxSet Flag to indicate whether max value is set.
34 * @param max Maximum length allowed. If this is <tt>null</tt> then no
35 * constraint is placed on the maximum length.
36 */
37 public static RFldConstraintF32MinMax valueOf(String key, String fldName, boolean minSet, Float min, boolean maxSet, Float max) throws FieldValidationException
38 {
39 RFldConstraintF32MinMax instance;
40 if (! instances_.containsKey(key))
41 {
42 instance = new RFldConstraintF32MinMax(fldName, minSet, min, maxSet, max);
43 synchronized (instances_)
44 {
45 instances_.put(key, instance);
46 }
47 }
48 else
49 instance = (RFldConstraintF32MinMax)instances_.get(key);
50
51 return instance;
52 }
53
54 /***
55 * Factory. Returns a <tt>RFldConstraintF32MinMax</tt> instance based on the input parameters.
56 * @param key Key to use for caching constraint.
57 * @param fldName Name of field to which this constraint is bound.
58 * @param minSet Flag to indicate whether min value is set.
59 * @param min Minimum length allowed. If this is <tt>null</tt> then no
60 * constraint is placed on the minimum length.
61 * @param maxSet Flag to indicate whether max value is set.
62 * @param max Maximum length allowed. If this is <tt>null</tt> then no
63 * constraint is placed on the maximum length.
64 */
65 public static RFldConstraintF32MinMax valueOf(String key, String fldName, boolean minSet, String min, boolean maxSet, String max) throws FieldValidationException
66 {
67 if (instances_.containsKey(key))
68 return (RFldConstraintF32MinMax)instances_.get(key);
69
70 if (minSet && maxSet)
71 return valueOf(key, fldName, minSet, Float.valueOf(min), maxSet, Float.valueOf(max));
72 else if (minSet)
73 return valueOf(key, fldName, minSet, Float.valueOf(min), maxSet, null);
74 else if (maxSet)
75 return valueOf(key, fldName, minSet, null, maxSet, Float.valueOf(max));
76 else
77 throw new FieldValidationException("Either the minimum or the maximum must be set");
78 }
79
80 /***
81 * Constructor.
82 * @param fldName Name of field to which this constraint is bound.
83 * @param minSet Flag to indicate whether min value is set.
84 * @param minVal Minimum value allowed.
85 * @param maxSet Flag to indicate whether max value is set.
86 * @param maxVal Maximum value allowed.
87 */
88 private RFldConstraintF32MinMax(String fldName, boolean minSet, Float minVal, boolean maxSet, Float maxVal) throws FieldValidationException
89 {
90 if (! minSet && ! maxSet)
91 throw new FieldValidationException("Min and max cannot both be NULL: " + fldName);
92
93 float minValue = (minSet) ? minVal.floatValue() : 0;
94 float maxValue = (maxSet) ? maxVal.floatValue() : 0;
95
96 if (minSet && maxSet && minValue > maxValue)
97 throw new FieldValidationException("Minimum value is after maximum value: " + fldName);
98
99 if (minSet)
100 setMinVal(fldName, minValue);
101
102 if (maxSet)
103 setMaxVal(fldName, maxValue);
104 }
105
106 /***
107 * Set minimum value.
108 * @param fldName Field name.
109 * @param minVal Minimum value.
110 */
111 private void setMinVal(String fldName, float minVal) throws FieldValidationException
112 {
113 if (maxValSet_ && minVal > maxVal_)
114 throw new FieldValidationException("Minimum value is greater than maximum value: " + fldName);
115
116 minValSet_ = true;
117 minVal_ = minVal;
118 }
119
120 /***
121 * Set maximum value.
122 * @param fldName Field name.
123 * @param maxVal Maximum value.
124 */
125 private void setMaxVal(String fldName, float maxVal) throws FieldValidationException
126 {
127 if (minValSet_ && maxVal < minVal_)
128 throw new FieldValidationException("Minimum value is greater than maximum value: " + fldName);
129
130 maxValSet_ = true;
131 maxVal_ = maxVal;
132 }
133
134 /***
135 * Get error description.
136 * @param fldName Field name.
137 * @return Error description.
138 */
139 public String errDesc(String fldName)
140 {
141 return "Field value outside min/max range: " + fldName;
142 }
143
144 /***
145 * Check if a value satisfies the constraint.
146 * @param fld Field to validate.
147 * @return <tt>true</tt> if the field satisfied the
148 * constraint. Otherwise, <tt>false</tt> is returned.
149 */
150 public boolean isValid(float val)
151 {
152 boolean ret = (! minValSet_ || val >= minVal_);
153 ret = (ret && (! maxValSet_ || val <= maxVal_));
154 return ret;
155 }
156
157 /***
158 * Cache of instances.
159 */
160 private static java.util.Map instances_ = new java.util.HashMap();
161
162 /***
163 * Flag to indicate if minimum value constraint is set.
164 */
165 protected boolean minValSet_;
166
167 /***
168 * Flag to indicate if maximum value constraint is set.
169 */
170 protected boolean maxValSet_;
171
172 /***
173 * Minimum value allowed.
174 */
175 protected float minVal_;
176
177 /***
178 * Maximum value allowed.
179 */
180 protected float maxVal_;
181 }
This page was automatically generated by Maven