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>byte</tt>
22 * numeric message field.
23 * @author Jawaid Hakim.
24 */
25 public class RFldConstraintI8MinMax extends RFldConstraintI8
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 RFldConstraintI8MinMax valueOf(String key, String fldName, boolean minSet, Byte min, boolean maxSet, Byte max) throws FieldValidationException
39 {
40 RFldConstraintI8MinMax instance;
41 if (! instances_.containsKey(key))
42 {
43 instance = new RFldConstraintI8MinMax(fldName, minSet, min, maxSet, max);
44 synchronized (instances_)
45 {
46 instances_.put(key, instance);
47 }
48 }
49 else
50 instance = (RFldConstraintI8MinMax)instances_.get(key);
51
52 return instance;
53 }
54
55 /***
56 * Factory. Returns a <tt>RFldConstraintI8MinMax</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 RFldConstraintI8MinMax valueOf(String key, String fldName, boolean minSet, String min, boolean maxSet, String max) throws FieldValidationException
67 {
68 if (instances_.containsKey(key))
69 return (RFldConstraintI8MinMax)instances_.get(key);
70
71 if (minSet && maxSet)
72 return valueOf(key, fldName, minSet, Byte.valueOf(min), maxSet, Byte.valueOf(max));
73 else if (minSet)
74 return valueOf(key, fldName, minSet, Byte.valueOf(min), maxSet, null);
75 else if (maxSet)
76 return valueOf(key, fldName, minSet, null, maxSet, Byte.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 RFldConstraintI8MinMax(String fldName, boolean minSet, Byte minVal, boolean maxSet, Byte maxVal) throws FieldValidationException
90 {
91 if (! minSet && ! maxSet)
92 throw new FieldValidationException("Min and max cannot both be NULL: " + fldName);
93
94 byte minValue = (minSet) ? minVal.byteValue() : 0;
95 byte maxValue = (maxSet) ? maxVal.byteValue() : 0;
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, int 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, int 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 val Field value 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(byte 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 int minVal_;
176
177 /***
178 * Maximum value allowed.
179 */
180 protected int maxVal_;
181 }
This page was automatically generated by Maven