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 import java.util.Date;
21
22 /***
23 * Class to represent a minumum/maximum date constraint on a datetime message field.
24 * @author Jawaid Hakim.
25 */
26 public class RFldConstraintDatetimeMinMax extends RFldConstraintDatetime
27 {
28 /***
29 * Factory. Returns a <tt>RFldConstraintDatetimeMinMax</tt> instance based on the input parameters.
30 * @param key Key to use for caching constraint.
31 * @param fldName Name of field to which this constraint is bound.
32 * @param minSet Flag to indicate whether min value is set.
33 * @param min Minimum date allowed. If this is <tt>null</tt> then no
34 * constraint is placed on the minimum date.
35 * @param maxSet Flag to indicate whether max value is set.
36 * @param max Maximum date allowed. If this is <tt>null</tt> then no
37 * constraint is placed on the maximum date.
38 */
39 public static RFldConstraintDatetimeMinMax valueOf(String key, String fldName, boolean minSet, Date min, boolean maxSet, Date max) throws FieldValidationException
40 {
41 RFldConstraintDatetimeMinMax instance;
42 if (! instances_.containsKey(key))
43 {
44 instance = new RFldConstraintDatetimeMinMax(fldName, minSet, min, maxSet, max);
45 synchronized (instances_)
46 {
47 instances_.put(key, instance);
48 }
49 }
50 else
51 instance = (RFldConstraintDatetimeMinMax)instances_.get(key);
52
53 return instance;
54 }
55
56 /***
57 * Constructor.
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 minDate Minimum date allowed. If this is <tt>null</tt> then no
61 * constraint is placed on the minimum date.
62 * @param maxSet Flag to indicate whether max value is set.
63 * @param maxDate Maximum date allowed. If this is <tt>null</tt> then no
64 * constraint is placed on the maximum date.
65 */
66 private RFldConstraintDatetimeMinMax(String fldName, boolean minSet, Date minDate, boolean maxSet, Date maxDate) throws FieldValidationException
67 {
68 minSet_ = minSet;
69 maxSet_ = maxSet;
70
71 if (! minSet && ! maxSet)
72 throw new FieldValidationException("Min and max cannot both be NULL: " + fldName);
73
74 if (minSet && maxSet && minDate.after(maxDate))
75 throw new FieldValidationException("Minimum date is after maximum date: " + fldName);
76
77 if (minSet)
78 minDate_ = minDate;
79
80 if (maxSet)
81 maxDate_ = maxDate;
82 }
83
84 /***
85 * Get error description.
86 * @param fldName Field name.
87 * @return Error description.
88 */
89 public String errDesc(String fldName)
90 {
91 return "Field value outside min/max range: " + fldName;
92 }
93
94 /***
95 * Check if a value satisfies the constraint.
96 * @param val Data to validate.
97 * @return <tt>true</tt> if the data satisfied the
98 * constraint. Otherwise, <tt>false</tt> is returned.
99 */
100 public boolean isValid(Date val)
101 {
102 return (val != null && (minDate_ == null || ! val.before(minDate_)) && (maxDate_ == null || ! val.after(maxDate_)));
103 }
104
105 /***
106 * Cache of instances.
107 */
108 private static java.util.Map instances_ = new java.util.HashMap();
109
110 /***
111 * Minimum date allowed.
112 */
113 protected Date minDate_;
114
115 /***
116 * Maximum date allowed.
117 */
118 protected Date maxDate_;
119
120 protected boolean minSet_;
121
122 protected boolean maxSet_;
123 }
This page was automatically generated by Maven