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 minumum/maximum length constraint on a <tt>String</tt> message field. 22 * @author Jawaid Hakim. 23 */ 24 public class RFldConstraintStringLength extends RFldConstraintString 25 { 26 /*** 27 * Factory. Returns a <tt>RFldConstraintStringLength</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 the minimum 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 minSet Flag to indicate whether the maximum 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 * @see #NOTSET 37 */ 38 public static RFldConstraintStringLength valueOf(String key, String fldName, boolean minSet, String min, boolean maxSet, String max) throws FieldValidationException 39 { 40 RFldConstraintStringLength instance; 41 if (! instances_.containsKey(key)) 42 { 43 instance = new RFldConstraintStringLength(fldName, minSet, min, maxSet, max); 44 synchronized (instances_) 45 { 46 instances_.put(key, instance); 47 } 48 } 49 else 50 instance = (RFldConstraintStringLength)instances_.get(key); 51 52 return instance; 53 } 54 55 /*** 56 * Constructor. 57 * @param fldName Name of field to which this constraint is bound. 58 * @param minLen Minimum length allowed. 59 * @param maxLen Maximum length allowed. 60 */ 61 private RFldConstraintStringLength(String fldName, boolean minSet, String minLen, boolean maxSet, String maxLen) throws FieldValidationException 62 { 63 if (! minSet && ! maxSet) 64 throw new FieldValidationException("Min and max cannot both be NULL: " + fldName); 65 66 int tmpMinLen = NOTSET; 67 if (minSet) 68 tmpMinLen = Integer.parseInt(minLen); 69 70 int tmpMaxLen = NOTSET; 71 if (maxSet) 72 tmpMaxLen = Integer.parseInt(maxLen); 73 74 init(fldName, minSet, tmpMinLen, maxSet, tmpMaxLen); 75 } 76 77 /*** 78 * Initialize. 79 * @param fldName Field name. 80 * @param minSet Flag to indicate whether the minimum value is set. 81 * @param minLen Minimum length allowed. If this is NOTSET then no 82 * constraint is placed on the minimum length. 83 * @param minSet Flag to indicate whether the maximum value is set. 84 * @param maxLen Maximum length allowed. If this is NOTSET then no 85 * constraint is placed on the maximum length. 86 * @see #NOTSET 87 */ 88 private void init(String fldName, boolean minSet, int minLen, boolean maxSet, int maxLen) throws FieldValidationException 89 { 90 minLen_ = maxLen_ = NOTSET; 91 92 if (minSet && minLen < 0) 93 throw new FieldValidationException("Invalid minimum length: " + fldName); 94 else if (maxSet && maxLen < 0) 95 throw new FieldValidationException("Invalid maximum length: " + fldName); 96 else if (minSet && maxSet && minLen > maxLen) 97 throw new FieldValidationException("Minimum length is greater than maximum length: " + fldName); 98 99 minValSet_ = minSet; 100 maxValSet_ = maxSet; 101 minLen_ = minLen; 102 maxLen_ = maxLen; 103 } 104 105 /*** 106 * Get error description. 107 * @param fldName Field name. 108 * @return Error description. 109 */ 110 public String errDesc(String fldName) 111 { 112 return "Field value outside min/max length range: " + fldName; 113 } 114 115 /*** 116 * Check if a message field satisfies the constraint. 117 * @param fld Field to validate. 118 * @return <tt>true</tt> if the field satisfied the 119 * constraint. Otherwise, <tt>false</tt> is returned. 120 */ 121 public boolean isValid(RFld fld) 122 { 123 if (fld instanceof RFldString) 124 return isValid(((RFldString)fld).getValue()); 125 else 126 return false; 127 } 128 129 /*** 130 * Check if a value satisfies the constraint. 131 * @param val Value to validate. 132 * @return <tt>true</tt> if the data satisfies the 133 * constraint. Otherwise, <tt>false</tt> is returned. 134 */ 135 public boolean isValid(String val) 136 { 137 int len = val.length(); 138 return ((! minValSet_ || len >= minLen_) && (! maxValSet_ || len <= maxLen_)); 139 } 140 141 /*** 142 * Cache of instances. 143 */ 144 private static java.util.Map instances_ = new java.util.HashMap(); 145 146 /*** 147 * Constraint not set. 148 */ 149 static public final int NOTSET = -1; 150 151 /*** 152 * Minimum length allowed. 153 */ 154 protected int minLen_; 155 156 /*** 157 * Maximum length allowed. 158 */ 159 protected int maxLen_; 160 161 protected boolean minValSet_; 162 163 protected boolean maxValSet_; 164 }

This page was automatically generated by Maven