org.vladnput.types
Class VladType

java.lang.Object
  extended by org.vladnput.types.VladType
Direct Known Subclasses:
CreditCardType, DateType, EmailType, IntegerType, StringType, URLType

public abstract class VladType
extends java.lang.Object

The base class for all type validators. Typically, a subclass will define arguments that it handles as string constants and check them in

Author:
Patrick Higgins

Field Summary
static java.lang.String ALLOW_EMPTY_KEY
          Name of the allow empty argument.
static java.lang.String LEN_VAR
          Message variable for the length of the actual value.
static java.lang.String MAX_LEN_KEY
          Name of the maximum allowed length argument.
static java.lang.String MIN_LEN_KEY
          Name of the minimum required length argument.
static java.lang.String SENSITIVE_KEY
          Name of the sensitive argument.
static java.util.Set<java.lang.String> VALID_KEYS
          Set of valid argument names
 
Constructor Summary
VladType(java.util.Map<java.lang.String,java.lang.Object> args)
          Constructs VladType from args, removing any arguments related to VladMessages or that are recognized by this class (e.g.
 
Method Summary
 java.util.List<java.lang.String> addMessage(java.util.List<java.lang.String> msgs, java.lang.String name)
          Adds the name of the message for argument name to msgs.
 boolean allowEmpty()
           
protected  java.lang.Boolean booleanKey(java.util.Map<java.lang.String,java.lang.Object> args, java.lang.String key)
          Removes and returns the mapping associated with key in args, or null if there is not one.
static void checkArg(java.lang.String name, java.lang.Object value, java.util.Map<java.lang.String,java.lang.Object> args)
          Checks argument with name and value for validity.
protected  java.util.Date dateKey(java.util.Map<java.lang.String,java.lang.Object> args, java.lang.String key, java.text.DateFormat format)
          Removes and returns the string mapping associated with key in args after parsing it as a date, or null if there is not one.
protected  java.util.List<java.lang.String> doValidate(java.util.List<java.lang.String> msgs, java.lang.String value, VladMessageVars vars)
           
protected  java.lang.Double floatKey(java.util.Map<java.lang.String,java.lang.Object> args, java.lang.String key)
          Removes and returns the mapping associated with key in args, or null if there is not one.
 VladMessages getMessages()
          Gets the messages for this type.
protected  java.lang.Integer integerKey(java.util.Map<java.lang.String,java.lang.Object> args, java.lang.String key)
          Removes and returns the mapping associated with key in args, or null if there is not one.
 boolean isSensitive()
           
static java.lang.String msgFor(java.lang.String arg)
          Returns the name of the message argument for arg.
protected  java.lang.String stringKey(java.util.Map<java.lang.String,java.lang.Object> args, java.lang.String key)
          Removes and returns the mapping associated with key in args, or null if there is not one.
 java.lang.String toString()
           
 java.util.List<java.lang.String> validate(java.lang.String value, VladMessageVars vars)
          Checks value for validity.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LEN_VAR

public static final java.lang.String LEN_VAR
Message variable for the length of the actual value.

See Also:
Constant Field Values

MIN_LEN_KEY

public static final java.lang.String MIN_LEN_KEY
Name of the minimum required length argument. Also, name of the message variable for the minimum required length.

See Also:
Constant Field Values

MAX_LEN_KEY

public static final java.lang.String MAX_LEN_KEY
Name of the maximum allowed length argument. Also, name of the message variable for the maximum allowed length.

See Also:
Constant Field Values

SENSITIVE_KEY

public static final java.lang.String SENSITIVE_KEY
Name of the sensitive argument.

See Also:
Constant Field Values

ALLOW_EMPTY_KEY

public static final java.lang.String ALLOW_EMPTY_KEY
Name of the allow empty argument.

See Also:
Constant Field Values

VALID_KEYS

public static final java.util.Set<java.lang.String> VALID_KEYS
Set of valid argument names

Constructor Detail

VladType

public VladType(java.util.Map<java.lang.String,java.lang.Object> args)
Constructs VladType from args, removing any arguments related to VladMessages or that are recognized by this class (e.g. min_len and max_len). Any arguments not recognized are ignored.

Parameters:
args - Arguments for this type
Method Detail

checkArg

public static void checkArg(java.lang.String name,
                            java.lang.Object value,
                            java.util.Map<java.lang.String,java.lang.Object> args)
                     throws InvalidArgException,
                            InvalidArgValueException
Checks argument with name and value for validity. All other arguments for this type are given in args so that dependencies between arguments can be checked, for example that min_len is less than max_len. Subclasses will likely need to "override" this method, which seems confusing because this method is static and thus cannot be overridden. The method is called through reflection and thus defining a static method with the same name and signature in a subclass will cause the subclass method to be called. For examples, see StringType and DateType.

Parameters:
name - Argument name
value - Argument value
args - The other arguments for this type
Throws:
InvalidArgException - If the argument is invalid
InvalidArgValueException - If the argument value is invalid

msgFor

public static java.lang.String msgFor(java.lang.String arg)
Returns the name of the message argument for arg. This will always be arg with "_msg" appended.

Parameters:
arg - Argument name
Returns:
Name of corresponding message argument

addMessage

public java.util.List<java.lang.String> addMessage(java.util.List<java.lang.String> msgs,
                                                   java.lang.String name)
Adds the name of the message for argument name to msgs. If a null list is passed for msgs, a new list is constructed and returned with the message name added to it.

Parameters:
msgs - List of messages being accumulated
name - Argument name to add a message for
Returns:
List of message names. Will either be same as msgs or a new list if msgs is null.

getMessages

public VladMessages getMessages()
Gets the messages for this type.

Returns:
Messages for this type

validate

public final java.util.List<java.lang.String> validate(java.lang.String value,
                                                       VladMessageVars vars)
Checks value for validity. If it is valid, a null value will be returned. If it is invalid, a list of message names describing the error will be returned and the variables that may be used by those messages will be placed into vars.

Parameters:
value - Value to validate
vars - Variables to add values to if the value is invalid
Returns:
null if the value is valid, a list of message names otherwise

doValidate

protected java.util.List<java.lang.String> doValidate(java.util.List<java.lang.String> msgs,
                                                      java.lang.String value,
                                                      VladMessageVars vars)

isSensitive

public boolean isSensitive()

allowEmpty

public boolean allowEmpty()

stringKey

protected java.lang.String stringKey(java.util.Map<java.lang.String,java.lang.Object> args,
                                     java.lang.String key)
Removes and returns the mapping associated with key in args, or null if there is not one.

Parameters:
args - Arguments
key - Argument name
Returns:
Argument value, or null if there is not one.

integerKey

protected java.lang.Integer integerKey(java.util.Map<java.lang.String,java.lang.Object> args,
                                       java.lang.String key)
Removes and returns the mapping associated with key in args, or null if there is not one.

Parameters:
args - Arguments
key - Argument name
Returns:
Argument value, or null if there is not one.

floatKey

protected java.lang.Double floatKey(java.util.Map<java.lang.String,java.lang.Object> args,
                                    java.lang.String key)
Removes and returns the mapping associated with key in args, or null if there is not one.

Parameters:
args - Arguments
key - Argument name
Returns:
Argument value, or null if there is not one.

booleanKey

protected java.lang.Boolean booleanKey(java.util.Map<java.lang.String,java.lang.Object> args,
                                       java.lang.String key)
Removes and returns the mapping associated with key in args, or null if there is not one.

Parameters:
args - Arguments
key - Argument name
Returns:
Argument value, or null if there is not one.

dateKey

protected java.util.Date dateKey(java.util.Map<java.lang.String,java.lang.Object> args,
                                 java.lang.String key,
                                 java.text.DateFormat format)
                          throws java.lang.IllegalArgumentException
Removes and returns the string mapping associated with key in args after parsing it as a date, or null if there is not one.

Parameters:
args - Arguments
key - Argument name
format - Date format
Returns:
Argument value, or null if there is not one.
Throws:
java.lang.IllegalArgumentException - If the date is invalid

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2009 Patrick Higgins. All Rights Reserved.