1 23 24 29 package com.sun.enterprise.admin.util; 30 31 37 38 public class StringValidator extends BaseValidator 39 { 40 private final int mMininumLength; 41 private static final int kDefaultMinimumLength = 1; 42 private static StringValidator sDefaultInstance = null; 43 private static final String badArgMessage = 44 "You can't call StringValidator.validate() with a non-String argument"; 45 46 49 public StringValidator(int minimumLength) 50 { 51 Assert.assertRange(minimumLength, 0, Integer.MAX_VALUE, "minimumLength"); 52 mMininumLength = minimumLength; 53 } 54 55 58 public static Validator getInstance() 59 { 60 if(sDefaultInstance == null) 62 { 63 sDefaultInstance = new StringValidator(kDefaultMinimumLength); 64 } 65 return sDefaultInstance; 66 } 67 68 73 public ValidatorResult validate(Object obj) 74 { 75 ValidatorResult result = super.validate(obj); 76 77 if (result.isValid()) 78 { 79 Assert.assertit( (obj instanceof String ), badArgMessage); 80 81 final String str = (String )obj; 82 final int len = str.length(); 83 84 if(len < mMininumLength) 85 { 86 result = makeBadResult(len); 87 } 88 } 89 return result; 90 } 91 92 private ValidatorResult makeBadResult(final int len) 93 { 94 return new ValidatorResult(false, 95 "The String argument is invalid. The minimum required length is " + 96 mMininumLength + " and the String's actual length is " + len); 97 } 98 } | Popular Tags |