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 an enumeration constraint on a <tt>String</tt> message field. 22 * @author Jawaid Hakim. 23 */ 24 public class RFldConstraintStringEnum extends RFldConstraintString 25 { 26 /*** 27 * Constructor. 28 * @param fldName Name of field to which this constraint is bound. 29 * @param enums Enumeration of allowed values. 30 * @param ignoreCase If <tt>true</tt> string compares are case-insensitive. 31 * Otherwise, compares are case-sensitive. 32 */ 33 public RFldConstraintStringEnum(String fldName, String[] enums, boolean ignoreCase) throws FieldValidationException 34 { 35 if (enums == null) 36 throw new FieldValidationException("NULL enums: " + fldName); 37 ignoreCase_ = ignoreCase; 38 enums_ = enums; 39 } 40 41 /*** 42 * Constructor. 43 * @param fldName Name of field to which this constraint is bound. 44 * @param enums Enumeration of allowed values. 45 */ 46 public RFldConstraintStringEnum(String fldName, String[] enums) throws FieldValidationException 47 { 48 if (enums == null) 49 throw new FieldValidationException("NULL enums: " + fldName); 50 enums_ = enums; 51 } 52 53 /*** 54 * Get error description. 55 * @param fldName Field name. 56 * @return Error description. 57 */ 58 public String errDesc(String fldName) 59 { 60 return "Field value not in enumeration: " + fldName; 61 } 62 63 /*** 64 * Check if a value satisfies the constraint. 65 * @param val Value to validate. 66 * @return <tt>true</tt> if the data satisfies the 67 * constraint. Otherwise, <tt>false</tt> is returned. 68 */ 69 public boolean isValid(String val) 70 { 71 if (enums_ == null) 72 return true; 73 else if (val == null) 74 return false; 75 76 if (ignoreCase_) 77 { 78 for (int i = enums_.length - 1; i >= 0; --i) 79 { 80 if (val.equalsIgnoreCase(enums_[i])) 81 return true; 82 } 83 } 84 else 85 { 86 for (int i = enums_.length - 1; i >= 0; --i) 87 { 88 if (val.equals(enums_[i])) 89 return true; 90 } 91 } 92 return false; 93 } 94 95 /*** 96 * Ignore case during string compares. 97 */ 98 boolean ignoreCase_; 99 100 /*** 101 * Enumeration of allowed values. 102 */ 103 protected String[] enums_; 104 }

This page was automatically generated by Maven