Package net.sf.jguiraffe.gui.forms
Class DefaultFormValidatorResults
- java.lang.Object
-
- net.sf.jguiraffe.gui.forms.DefaultFormValidatorResults
-
- All Implemented Interfaces:
FormValidatorResults
public class DefaultFormValidatorResults extends Object implements FormValidatorResults
Default implementation of the
FormValidatorResults
interface.This class provides a fully functional implementation of the
FormValidatorResults
interface. Instances are initialized with a map that contains the names of the validated fields and their correspondingValidationResult
objects. They are immutable and thus can be shared between multiple threads.- Version:
- $Id: DefaultFormValidatorResults.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description DefaultFormValidatorResults(Map<String,? extends ValidationResult> fieldData)
Creates a new instance ofDefaultFormValidatorResults
and initializes it with a map holding information about fields and their validation status.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ValidationResult
createValidationErrorResult(Form form, String key, Object... params)
Creates aValidationResult
object with an error message for a validation message.static ValidationMessage
createValidationMessage(Form form, String key, Object... params)
Obtains aValidationMessage
object for the specified key.boolean
equals(Object obj)
Compares this object with another one.Set<String>
getErrorFieldNames()
Returns a set with the names of those fields that are invalid.Set<String>
getFieldNames()
Returns a set with all defined field names.ValidationResult
getResultsFor(String field)
Returns the validation results for the specified field or null if this field does not exist.int
hashCode()
Determines a hash code for this object.boolean
isValid()
Checks whether form validation was successful.static FormValidatorResults
merge(FormValidatorResults res1, FormValidatorResults res2)
Returns aFormValidatorResults
object with the combined information of the specifiedFormValidatorResults
objects.String
toString()
Returns a string representation for this object.static Map<String,ValidationResult>
validResultMapForForm(Form form)
Creates ajava.util.Map
with validation information for all fields of the specifiedForm
.static DefaultFormValidatorResults
validResultsForForm(Form form)
Creates a validDefaultFormValidatorResults
for the fields of the specifiedForm
.
-
-
-
Constructor Detail
-
DefaultFormValidatorResults
public DefaultFormValidatorResults(Map<String,? extends ValidationResult> fieldData)
Creates a new instance ofDefaultFormValidatorResults
and initializes it with a map holding information about fields and their validation status.- Parameters:
fieldData
- the map with the field data (must not be null- Throws:
IllegalArgumentException
- if the map is null or contains null values
-
-
Method Detail
-
validResultMapForForm
public static Map<String,ValidationResult> validResultMapForForm(Form form)
Creates ajava.util.Map
with validation information for all fields of the specifiedForm
. Each field is assigned a validValidationResult
object. This method can be used by customFormValidator
implementations to initialize a map with data about all form fields. Then, during validation, fields that were detected to be invalid can be updated in this map. Finally, aDefaultFormValidatorResults
object can be created based on this map.- Parameters:
form
- theForm
(must not be null)- Returns:
- a map with valid validation data for all fields of the form
- Throws:
IllegalArgumentException
- if theForm
is null
-
validResultsForForm
public static DefaultFormValidatorResults validResultsForForm(Form form)
Creates a validDefaultFormValidatorResults
for the fields of the specifiedForm
. The resulting object stores a validValidationResult
object for all fields that belong to the form.- Parameters:
form
- theForm
(must not be null)- Returns:
- a valid
DefaultFormValidatorResults
object for this form - Throws:
IllegalArgumentException
- if theForm
is null
-
createValidationMessage
public static ValidationMessage createValidationMessage(Form form, String key, Object... params)
Obtains aValidationMessage
object for the specified key. The message is obtained from the currentValidationMessageHandler
(which can be retrieved from the form'sTransformerContext
).- Parameters:
form
- theForm
(must not be null)key
- the key of the validation error messageparams
- additional parameters for the validation error message- Returns:
- a
ValidationMessage
object for the specified error message - Throws:
IllegalArgumentException
- if theForm
is null
-
createValidationErrorResult
public static ValidationResult createValidationErrorResult(Form form, String key, Object... params)
Creates aValidationResult
object with an error message for a validation message. This is a convenience method that obtains the validation error message from the currentValidationMessageHandler
and creates the correspondingValidationResult
object. It can be used for instance by aFormValidator
that determines an incorrect field.- Parameters:
form
- theForm
(must not be null)key
- the key of the validation error messageparams
- additional parameters for the validation error message- Returns:
- a
ValidationResult
object initialized with this error message - Throws:
IllegalArgumentException
- if theForm
is null
-
merge
public static FormValidatorResults merge(FormValidatorResults res1, FormValidatorResults res2)
Returns aFormValidatorResults
object with the combined information of the specifiedFormValidatorResults
objects. If one of the passed in objects is null, the other one is returned. Otherwise a newFormValidatorResults
instance is created and populated with theValidationResult
objects contained in both parameter objects. If necessary, theValidationResult
objects are also merged, so that the resulting object actually contains a union of all validation messages.- Parameters:
res1
- the firstFormValidatorResults
objectres2
- the secondFormValidatorResults
object- Returns:
- the merged results
-
isValid
public boolean isValid()
Checks whether form validation was successful.- Specified by:
isValid
in interfaceFormValidatorResults
- Returns:
- a flag if all fields are valid
-
getFieldNames
public Set<String> getFieldNames()
Returns a set with all defined field names.- Specified by:
getFieldNames
in interfaceFormValidatorResults
- Returns:
- a set with the field names
-
getErrorFieldNames
public Set<String> getErrorFieldNames()
Returns a set with the names of those fields that are invalid.- Specified by:
getErrorFieldNames
in interfaceFormValidatorResults
- Returns:
- a set with the names of the invalid fields
-
getResultsFor
public ValidationResult getResultsFor(String field)
Returns the validation results for the specified field or null if this field does not exist.- Specified by:
getResultsFor
in interfaceFormValidatorResults
- Parameters:
field
- the name of the desired field- Returns:
- the validation result for this field
-
equals
public boolean equals(Object obj)
Compares this object with another one. Two form validator result objects are considered equal if and only if they contain the same set of fields with corresponding result objects.
-
hashCode
public int hashCode()
Determines a hash code for this object.
-
toString
public String toString()
Returns a string representation for this object. This string contains the string representation for the internal map with the fields and their validation result objects. Provided that theValidationResult
objects have propertoString()
implementations, the string generated by the map is exactly what is needed to see which field has which validation status.
-
-